mirror of
https://github.com/forkless/NotAlterra.git
synced 2026-08-16 08:36:37 +02:00
2.8 KiB
2.8 KiB
Rust Standards
Coding conventions and patterns for the NotAlterra project.
Error Handling
- Use
anyhow::Resultfor fallible functions — no custom error types anyhow::bail!for early returns with context.with_context(|| format!(...))onResultfrom external crates- Avoid
unwrap()andexpect()outside of tests and examples - Use
let _ = fallible_fn()when deliberately ignoring errors
File I/O
- Prefer
std::fsconvenience functions for simple operations (fs::read(),fs::write(),fs::read_dir()) - Use
PathBuffor owned paths,&Pathfor borrowed references - Validate paths early with
path.exists()before operations - Use
saturating_subfor all arithmetic that could underflow
String Handling
- Use
format!()for construction, avoid+concatenation - Use
to_string_lossy()for OsStr → String conversion - Strip control characters from user input before storage or logging
Data Structures
- Prefer
Vecover linked lists or custom containers - Use
HashSetfor deduplication - Use
Option<T>for nullable values — nevernullor sentinel values - Use
structwith named fields for complex return types - Derive
Debug, Cloneon all data structures,Defaultwhere meaningful
Pattern: Property Extraction (GVAS parser)
All four extractors follow the same pattern:
- Scan for property name using
windows(target.len()).position() - Validate the FName header (length dword + null terminator)
- Skip past the property type name
- Validate bounds before every index access
- Return
OptionorResult— never panic
When adding a new property type, follow this exact pattern and add a bounds check on every array access.
Pattern: TUI Dialogs
All dialogs follow the same structure:
- Define popup area with
centered_rect_size()orcentered_rect() - Render
Clearwidget, then a borderedBlock - Compute
innerarea with margin - Render title, body, buttons in sequence
- Call
draw_whale_separator(f, bar, app)at the bottom
Pattern: Menu Actions
All menu actions:
- Validate preconditions (save folder set, backup exists)
- Show status/spinner if the operation takes time
- Call through to
ops::*for actual file operations - Log via
guard::log_action()with sanitized paths - Show result dialog (success or error)
- Refresh dashboard stats
Clippy
-D warningsenforced in CI — no exceptions- Fix lints immediately when CI catches new ones
- Common lints that have fired:
collapsible_match,manual_is_multiple_of,empty_line_after_doc_comments,needless_borrows_for_generic_args
Dependencies
- Prefer crates with 50M+ downloads for core functionality
- Avoid platform-specific FFI — prefer pure Rust where possible
- Keep dependency count minimal — each new crate is a review point
- Run
cargo auditandcargo denybefore each release