|
| 1 | +# Code Style Guide |
| 2 | + |
| 3 | +## Module Organization |
| 4 | + |
| 5 | +- **Module names**: Shortened, clear (e.g., `bluetooth_le` → `btle`, `device_logical_unit` → `dlu`) |
| 6 | +- **Module structure**: One primary struct per sub-type, implementation in same file |
| 7 | +- **File naming**: Match shortened module names |
| 8 | + |
| 9 | +## Import Grouping |
| 10 | + |
| 11 | +```rust |
| 12 | +// 1. External crates (if any) |
| 13 | +use core::net::Ipv4Addr; |
| 14 | + |
| 15 | +// 2. Internal crate imports |
| 16 | +use crate::{Error, Head}; |
| 17 | +use crate::parser::{ByteOrder, Parser}; |
| 18 | + |
| 19 | +// 3. Relative imports |
| 20 | +use super::ipv4::Protocol; |
| 21 | +``` |
| 22 | + |
| 23 | +## Field Naming |
| 24 | + |
| 25 | +- **Brief names**: `vendor_id` → `vid`, `mac_address` → `mac`, `baud_rate` → `baud` |
| 26 | +- **Documentation**: Add doc comments for any ambiguous shortened names |
| 27 | +- **Standard abbreviations**: `nsid`, `lun`, `wwn`, `eui`, `vid`, `pid`, `hba_port`, `pm_port` |
| 28 | + |
| 29 | +## Data Types |
| 30 | + |
| 31 | +### String Handling |
| 32 | +- **UTF-8 strings**: Use `&CStr` for null-terminated UTF-8 strings |
| 33 | +- **UTF-16 strings**: Use `String` for UTF-16 strings (converted from parser) |
| 34 | +- **Raw strings**: Use `&str` for non-null-terminated UTF-8 strings (rare) |
| 35 | + |
| 36 | +### Newtypes |
| 37 | +- **Single field structs**: Use newtype pattern |
| 38 | +```rust |
| 39 | +pub struct Vlan(pub u16); |
| 40 | +pub struct Protocol(pub u16); |
| 41 | +``` |
| 42 | + |
| 43 | +### Enums |
| 44 | +- **Small known value sets**: Use proper enums (no repr guarantees) |
| 45 | +- **Parser trait**: Implement for validation |
| 46 | +```rust |
| 47 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 48 | +pub enum Origin { |
| 49 | + Manual = 0x00, |
| 50 | + StatelessAutoConfiguration = 0x01, |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +### Structs |
| 55 | +- **Multi-field types**: Regular structs with brief field names |
| 56 | +- **Derive**: Always include `Debug, Clone, Copy, PartialEq, Eq, Hash` |
| 57 | + |
| 58 | +## Reserved Fields |
| 59 | + |
| 60 | +- **Storage**: Do NOT store reserved fields in structs |
| 61 | +- **Validation**: Parse and validate in `TryFrom` implementation |
| 62 | +- **Pattern**: `let _reserved: u32 = node.data.parse(ByteOrder::Little)?;` |
| 63 | + |
| 64 | +## Error Handling |
| 65 | + |
| 66 | +- **Invalid values**: Return `Error::Invalid` for spec violations |
| 67 | +- **Validation**: Check reserved field values, invalid NSIDs, etc. |
| 68 | + |
| 69 | +## Documentation |
| 70 | + |
| 71 | +- **Struct docs**: Include UEFI sub-type number and byte length |
| 72 | +- **Field docs**: Required for abbreviated field names |
| 73 | +- **Example**: |
| 74 | +```rust |
| 75 | +/// SATA Device Path (SubType 0x12) |
| 76 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 77 | +pub struct Sata { |
| 78 | + /// HBA port number |
| 79 | + pub hba_port: u16, |
| 80 | + /// Port multiplier port number |
| 81 | + pub pm_port: u16, |
| 82 | + pub lun: u16, |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +## Implementation Patterns |
| 87 | + |
| 88 | +### Parse Order |
| 89 | +- **Last field**: Always use `finish()` method to guarantee end of data |
| 90 | +- **Earlier fields**: Use regular `parse()` method |
| 91 | + |
| 92 | +### TryFrom Implementation |
| 93 | +```rust |
| 94 | +impl<'a> TryFrom<Head<'a>> for TypeName<'a> { |
| 95 | + type Error = Error; |
| 96 | + |
| 97 | + fn try_from(mut node: Head<'a>) -> Result<Self, Self::Error> { |
| 98 | + // Parse reserved fields (don't store) |
| 99 | + let _reserved: u32 = node.data.parse(ByteOrder::Little)?; |
| 100 | + |
| 101 | + // Parse fields (use finish() for last field) |
| 102 | + let field1 = node.data.parse(ByteOrder::Little)?; |
| 103 | + let utf8_string = node.data.finish(())?; // &CStr for UTF-8 |
| 104 | + |
| 105 | + Ok(TypeName { field1, utf8_string }) |
| 106 | + } |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +### Byte Order |
| 111 | +- **Multi-byte integers**: Always specify `ByteOrder::Little` |
| 112 | +- **Single bytes**: Use `()` argument |
| 113 | +- **Arrays**: Use `()` argument |
| 114 | + |
| 115 | +## Constants |
| 116 | + |
| 117 | +- **Protocol numbers**: Define as associated constants |
| 118 | +- **Magic values**: Use descriptive constant names |
| 119 | +- **Grouping**: Related constants together with docs |
0 commit comments