2.4 KiB
2.4 KiB
Rust Style Guide
This guide defines Rust conventions for the MXAccess Gateway Rust client crate, CLI, and tests.
Baseline
- Run
cargo fmton every changed Rust file. - Run
cargo clippyfor non-trivial changes when the crate is available. - Use the current stable Rust toolchain unless the project pins a version.
- Keep generated protobuf modules isolated from handwritten API wrappers.
Source Documentation
- Maintain the existing documentation style in the file, crate, and surrounding component.
- Write comments that include business-specific or domain-specific context when that context is available from the code, surrounding docs, or naming.
- Use
///documentation for public APIs when behavior, parity constraints, or security requirements are not obvious from the type signature. - Avoid comments that restate syntax or control flow.
Crate Structure
- Keep the reusable client library separate from the CLI binary.
- Use small modules for
client,session,options,auth,value, anderror. - Re-export public types intentionally from
lib.rs. - Keep generated modules private unless raw protobuf access is part of the API.
Naming
- Use
snake_casefor functions, variables, modules, and fields. - Use
UpperCamelCasefor types, traits, and enum variants. - Use
SCREAMING_SNAKE_CASEfor constants. - Keep protocol names aligned with the protobuf contract where exactness matters.
Async And Ownership
- Use
asyncAPIs withtokiofor network operations. - Prefer explicit
closemethods for sessions. Do not rely onDropfor async cleanup. - Avoid unbounded channels and background tasks without a shutdown path.
- Use borrowed parameters such as
&strwhere ownership is not needed.
Errors
- Use
thiserrorfor library error enums. - Preserve
tonic::Status, transport errors, command replies, HRESULTs, and MXAccess status details. - Redact API keys and secured values in
Debug,Display, and tracing output. - Avoid string-only errors for public API failures.
Security
- Use a secret wrapper for API keys when adding a dependency is acceptable.
- Do not derive
Debugon types that contain unredacted secrets. - Prefer explicit redacted display implementations for options and errors.
Tests
- Use
#[tokio::test]for async tests. - Use fake
tonicservices or trait-backed clients for unit tests. - Keep live gateway tests behind
MXGATEWAY_INTEGRATION=1.