Skip to content

Commit d6f3955

Browse files
committed
refine: explicate more details from the style guide
Include much of the details from eka's style guide directly in the EEP to be a bit more self-contained.
1 parent 0488e95 commit d6f3955

File tree

1 file changed

+77
-1
lines changed

1 file changed

+77
-1
lines changed

eeps/0031-org-wide-rust-style-guide.md

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,83 @@ This EEP proposes the adoption of a standardized style guide and formatting conf
2626

2727
### 1. Official Style Guide
2828

29-
The existing [STYLE_GUIDE.md](https://github.com/ekala-project/eka/blob/master/STYLE_GUIDE.md) will be adopted as the canonical style guide for all Rust projects. It codifies conventions for file structure, item ordering, sorting rules, and documentation, which are not covered by automated tooling.
29+
The following style guide (inspired by eka's [`STYLE_GUIDE.md`](https://github.com/ekala-project/eka/blob/master/STYLE_GUIDE.md)) will be adopted as the canonical style guide for all Rust projects. It codifies conventions for file structure, item ordering, sorting rules, and documentation, which are not covered by automated tooling.
30+
31+
#### 1.1. File Structure and Item Order
32+
33+
All Rust module files (`.rs`) must follow a strict top-level item order to ensure predictability and ease of navigation. The canonical order is as follows:
34+
35+
1. **Module-level documentation (`//!`)**: Explains the purpose and scope of the module.
36+
2. **Outer attributes (`#![...]`)**: Compiler directives like `#![deny(missing_docs)]`.
37+
3. **`use` declarations**: External and internal imports.
38+
4. **Public re-exports (`pub use`)**: Items re-exported from other modules.
39+
5. **Submodules (`mod`)**: Child module declarations.
40+
6. **Constants (`const`)**: Compile-time constants.
41+
7. **Static variables (`static`)**: Globally allocated variables.
42+
8. **Types**: `struct`, `enum`, and `type` aliases.
43+
9. **Traits**: Trait definitions.
44+
10. **Trait implementations and `impl` blocks**: Implementations of traits and inherent methods.
45+
11. **Free-standing functions**: Module-level functions.
46+
12. **Tests (`#[cfg(test)]` modules)**: Unit and integration tests for the module.
47+
48+
#### 1.2. Sorting and Grouping Rules
49+
50+
Within each category, items must be sorted to maintain a consistent structure.
51+
52+
##### `use` Declarations
53+
54+
`use` declarations are grouped in the following order, with each group sorted alphabetically:
55+
56+
1. **`std`**: Standard library imports.
57+
2. **External Crates**: Third-party dependencies.
58+
3. **Local Modules**: Project-internal imports, starting with `crate::` or `super::`.
59+
60+
Example:
61+
62+
```rust
63+
use std::collections::HashMap;
64+
use std::path::PathBuf;
65+
66+
use anyhow::Result;
67+
use log::info;
68+
69+
use crate::core::Atom;
70+
use super::utils::helper_function;
71+
```
72+
73+
##### Other Items
74+
75+
All other top-level items—including modules, constants, types, traits, and functions—must be sorted alphabetically by their identifier.
76+
77+
##### Visibility
78+
79+
Within any given category, **public (`pub`) items must always be placed before private items**. This rule applies before alphabetical sorting. For example, a public function `alpha` would come before a private function `beta`, but also before a public function `zeta`.
80+
81+
Example:
82+
83+
```rust
84+
// Public items first, sorted alphabetically
85+
pub const MAX_RETRIES: u32 = 3;
86+
pub fn get_config() -> Config { /* ... */ }
87+
88+
// Private items next, sorted alphabetically
89+
const DEFAULT_TIMEOUT: u64 = 10;
90+
fn process_data() { /* ... */ }
91+
```
92+
93+
#### 1.3. Documentation Comments
94+
95+
Clear and comprehensive documentation is mandatory for maintaining a high-quality codebase.
96+
97+
- **All public items** (modules, functions, types, traits, constants) must have descriptive documentation comments (`///`).
98+
- **Module-level documentation (`//!`)** is required for every module. It should provide a high-level overview of the module's responsibilities and how it fits into the larger system.
99+
- Comments should be clear, concise, and sufficient for a developer to understand the item's purpose and usage without needing to read the underlying source code.
100+
101+
#### 1.4. General Guidelines
102+
103+
- **Use `rustfmt`**: This project uses an opinionated `rustfmt.toml` configuration to enforce a consistent code style. Running `cargo fmt` will automatically handle much of the formatting for you. However, the guidelines in this document (especially regarding item order and documentation) must still be followed manually.
104+
- **Preserve Semantics**: Never alter the meaning or behavior of the code purely for the sake of conforming to style.
105+
- **Preserve Comments and Attributes**: When reordering items, ensure that all associated documentation, comments (`//`), and attributes (`#[...]`) are moved along with the item they describe.
30106

31107
### 2. Standard `rustfmt` Configuration
32108

0 commit comments

Comments
 (0)