|
1 | | -# Rust Coding Guidelines |
| 1 | +# Rust Coding Guidelines: Documentation |
2 | 2 |
|
3 | 3 | > [!IMPORTANT] |
4 | 4 | > |
5 | | -> Follow these guidelines strictly. Other Rust coding guidelines can be enforced |
6 | | -> by running `rust-lint`. |
7 | | -
|
8 | | -1. Avoid unnecessary parameter passing. Don't pass intermediate results used |
9 | | - only by one function. Let the callee generate what it needs internally. |
10 | | -2. Test business logic, not language features. Don't test trait implementations, |
11 | | - method existence, or type properties. Focus on behavior, error handling, and |
12 | | - edge cases. |
13 | | -3. Extract common test setup into helper functions to avoid duplication. |
14 | | -4. Explain what and why in doc comments, not how. Comments must describe intent |
15 | | - and purpose, not implementation details. |
16 | | -5. Use simple, clear language in documentation. Avoid jargon. Replace "utilize" |
17 | | - with "use", "facilitate" with "enable", "leverage" with "use". |
18 | | -6. Document behavior and error conditions. Focus on what functions do and when |
19 | | - they fail. Include examples for non-obvious APIs. |
20 | | -7. Chain error context at multiple levels in binary applications. Add context at |
21 | | - each layer to build clear error chains that help debugging. |
22 | | -8. Write LLM-friendly error messages. Distinguish user errors from internal bugs |
23 | | - clearly. For user errors, explain what went wrong and suggest solutions. For |
24 | | - internal errors, state it's a bug and provide version, path, and details for |
25 | | - reporting. |
26 | | -9. Use anyhow for binary error handling and thiserror for library error |
27 | | - handling. Binaries use dynamic errors and context chaining. Libraries use |
28 | | - structured error types as part of their public API. |
29 | | -10. Define structured error types for libraries. Use explicit error enums as |
30 | | - part of your public API instead of dynamic errors. |
31 | | -11. Provide static context in error variant fields. Include file paths, field |
32 | | - names, and operation details in error variant fields rather than building |
33 | | - them dynamically. |
34 | | -12. Use three-level error hierarchy for libraries. Top-level errors group |
35 | | - operations with context. Low-level errors are context-free and reusable. |
36 | | -13. Document error variants. Add doc comments to error types and variants |
37 | | - explaining when each error occurs. |
38 | | -14. Avoid panics in public API. Never use unwrap(), expect(), or panic!() in |
39 | | - public functions. Always return Result<T> for fallible operations. |
| 5 | +> Follow these guidelines strictly |
| 6 | +
|
| 7 | +## 1. Doc Comments as API References |
| 8 | + |
| 9 | +Doc comments must explain **what** and **why** (intent), not **how** |
| 10 | +(implementation). Every public item needs a doc comment. Every module file needs |
| 11 | +top-level `//!` documentation. |
| 12 | + |
| 13 | +## 2. Write Simple, Clear Language |
| 14 | + |
| 15 | +Use plain English. Avoid jargon and complex words. Focus on clarity. |
| 16 | + |
| 17 | +Words to avoid: |
| 18 | + |
| 19 | +- "constitutes" → use "is", "represents", or "defines" |
| 20 | +- "utilize" → use "use" |
| 21 | +- "facilitate" → use "enable" or "help" |
| 22 | +- "in order to" → use "to" |
| 23 | +- "subsequently" → use "later" or "then" |
| 24 | +- "comprise" → use "include" or "contain" |
| 25 | +- "leverage" → use "use" |
| 26 | + |
| 27 | +## 3. Include Module Documentation |
| 28 | + |
| 29 | +Every module file must start with top-level `//!` documentation: |
| 30 | + |
| 31 | +```rust |
| 32 | +//! Cargo documentation generation utilities. |
| 33 | +//! |
| 34 | +//! This module provides functions for generating documentation from |
| 35 | +//! Cargo packages, including parsing rustdoc JSON output and converting |
| 36 | +//! it to markdown format. |
| 37 | +``` |
| 38 | + |
| 39 | +## 4. Avoid Bullets and Unnecessary Headers |
| 40 | + |
| 41 | +Avoid "Arguments", "Returns", "Architecture" headers. Write as paragraphs. Use |
| 42 | +lists sparingly. No bold labels like "**Important:**" or "**Note:**". |
| 43 | + |
| 44 | +🛑 Bad (Headers & Bullets): |
| 45 | + |
| 46 | +```rust |
| 47 | +/// A handler for network requests. |
| 48 | +/// |
| 49 | +/// This trait defines the interface for processing requests. |
| 50 | +/// |
| 51 | +/// - **Transport-agnostic**: Works with any handler |
| 52 | +/// - **Protocol-agnostic**: Don't understand structure |
| 53 | +/// - **Bidirectional**: Same handler for read/write |
| 54 | +``` |
| 55 | + |
| 56 | +✅ Good (Paragraph Style): |
| 57 | + |
| 58 | +```rust |
| 59 | +/// A handler for network requests. |
| 60 | +/// |
| 61 | +/// This trait defines the interface for processing requests and generating |
| 62 | +/// responses. The design is transport-agnostic, allowing both producers and |
| 63 | +/// consumers to work with any handler implementation. Handlers are |
| 64 | +/// bidirectional and can be used for both read and write roles. |
| 65 | +``` |
| 66 | + |
| 67 | +🛑 Bad (Arguments Header): |
| 68 | + |
| 69 | +```rust |
| 70 | +/// Send a data packet. |
| 71 | +/// |
| 72 | +/// This is a low-level I/O operation. |
| 73 | +/// |
| 74 | +/// # Arguments |
| 75 | +/// |
| 76 | +/// * `packet` - The data packet to send |
| 77 | +``` |
| 78 | + |
| 79 | +✅ Good (Integrated Description): |
| 80 | + |
| 81 | +```rust |
| 82 | +/// Send a data packet. |
| 83 | +/// |
| 84 | +/// This is a low-level I/O operation. The packet should be properly formatted, |
| 85 | +/// but the handler doesn't validate this. |
| 86 | +``` |
| 87 | + |
| 88 | +## 5. Use Lists When Appropriate |
| 89 | + |
| 90 | +Simple lists are OK for clarity, especially for error codes or options. |
| 91 | + |
| 92 | +✅ Good (Error Codes): |
| 93 | + |
| 94 | +```rust |
| 95 | +/// A protocol error. |
| 96 | +/// |
| 97 | +/// Standard error codes: |
| 98 | +/// - 1001: Parse error |
| 99 | +/// - 1002: Invalid request |
| 100 | +/// - 1003: Operation not found |
| 101 | +/// - 2000 to 2999: Application error |
| 102 | +``` |
| 103 | + |
| 104 | +## 6. Document Behavior and Errors |
| 105 | + |
| 106 | +Focus on what the function does and when it fails. |
| 107 | + |
| 108 | +✅ Good (Function with Errors): |
| 109 | + |
| 110 | +```rust |
| 111 | +/// Parses rustdoc JSON output and extracts item documentation. |
| 112 | +/// |
| 113 | +/// This function reads rustdoc JSON generated by `cargo rustdoc -- --output-format json` |
| 114 | +/// and extracts documentation items including functions, structs, and enums. |
| 115 | +/// The output is organized by crate and item type for easy navigation. |
| 116 | +pub fn parse_rustdoc(path: &Path) -> Result<Documentation> { |
| 117 | + // ... |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +## 7. Include Examples for Complex APIs |
| 122 | + |
| 123 | +Use `# Example` header for non-obvious APIs. |
| 124 | + |
| 125 | +✅ Good: |
| 126 | + |
| 127 | +````rust |
| 128 | +/// Builds a documentation index from parsed items. |
| 129 | +/// |
| 130 | +/// The index groups items by type and provides quick access to |
| 131 | +/// frequently used documentation. |
| 132 | +/// |
| 133 | +/// # Example |
| 134 | +/// |
| 135 | +/// ``` |
| 136 | +/// use doc_generator::Documentation; |
| 137 | +/// |
| 138 | +/// let docs = Documentation::new(); |
| 139 | +/// let index = docs.build_index(); |
| 140 | +/// |
| 141 | +/// assert_eq!(index.functions.len(), 42); |
| 142 | +/// ``` |
| 143 | +pub fn build_index(&self) -> Index { |
| 144 | + // ... |
| 145 | +} |
| 146 | +```` |
| 147 | + |
| 148 | +## 8. Use Intra-Doc Links |
| 149 | + |
| 150 | +Reference other items using [`ItemName`] syntax. |
| 151 | + |
| 152 | +✅ Good: |
| 153 | + |
| 154 | +```rust |
| 155 | +/// Processes documentation items and generates markdown output. |
| 156 | +/// |
| 157 | +/// See [`Documentation`] for the input structure and [`MarkdownWriter`] |
| 158 | +/// for the output format. |
| 159 | +/// |
| 160 | +/// This function is called by [`generate_docs`] after parsing is complete. |
| 161 | +pub fn process_items(docs: &Documentation) -> String { |
| 162 | + // ... |
| 163 | +} |
| 164 | +``` |
0 commit comments