# Coding Standards (Rust & Linux)
## 1. Toolchain & Verification
Rust ships with formatting, linting, and static type checking built in:
| Formatting | `ruff format` | `cargo fmt --all -- --check` |
| Linting | `ruff check` | `cargo clippy --all-targets --all-features -- -D warnings` |
| Type checking | `mypy` | Built into `rustc` (`cargo check`) |
| Pre-commit | `pre-commit` | `.pre-commit-config.yaml` |
## 2. Size & Structural Limits
- **Functions:** Aim for `<= 40` lines.
- **Structs / `impl` blocks:** Aim for `<= 200` lines.
- **Files / Modules:** Aim for `<= 300` lines of non-test production code (excluding inline `#[cfg(test)] mod tests`); when a module grows beyond one responsibility, split it into focused submodules inside its folder (e.g. `src/core/xray/vless.rs`, `src/core/xray/benchmark.rs`).
- **No Catch-All Modules:** Never add `utils.rs`, `helpers.rs`, or `common.rs`.
- **Trait Boundaries:** `src/engine/` must depend **only** on traits (`ProxyCore`, `HealthProbe`, `Notifier`) and `config`/`error`.
## 3. Error Handling & Ownership
- **No Panics in Runtime Code:** Never use `.unwrap()`, `.expect()`, `panic!()`, `todo!()`, or `unimplemented!()` in non-test code (`src/`). Propagate typed errors via `PilotError` (`thiserror`).
- **Borrow Over Own:** Prefer `&str`, `&Path`, and `&[T]` in function parameters over owned `String`, `PathBuf`, or `Vec<T>`.
- **Minimize Cloning:** Avoid `.clone()` in hot loops; use references or `Arc` for shared read-only state.
## 4. HTTP Clients, Async & Logging
- **Reuse HTTP Clients:** Build one `reqwest::Client` per service/probe and reuse it for connection pooling. Always set explicit `connect_timeout` and `timeout`.
- **Explicit Status Handling:** Classify HTTP status codes explicitly (`200`/`204` vs `403`/`429` geo-block vs `5xx`).
- **Never Block Tokio:** Never call `std::fs` or `std::thread::sleep` inside async functions; use `tokio::fs`, `tokio::time`, and async `reqwest`.
- **Graceful Shutdown:** Handle `SIGINT` / `SIGTERM` (`tokio::signal`) cleanly.
- **Structured Logging (`tracing`):** Use structured fields (`tracing::warn!(profile = %name, status, "probe failed")`). Never use `println!` outside `src/cli/`.
- **ASCII Identifiers:** Use English ASCII identifiers for code and logs; keep user-facing Persian alert messages separate.
## 5. Linux Server & Daemon Standards
- **Linux FHS Paths:** Binary at `/usr/local/bin/proxy-pilot`, config at `/etc/proxy-pilot/config.yaml`, managed cores/state in `/var/lib/proxy-pilot/`, `systemd` unit at `/etc/systemd/system/proxy-pilot.service`.
- **Static `musl` Builds:** Release binaries target `x86_64-unknown-linux-musl` and `aarch64-unknown-linux-musl` for zero-dependency execution on any Linux distro.
- **No TUN by Default:** Expose a fixed local proxy (`127.0.0.1`) so SSH, Iranian traffic, and Bale alerts stay direct.
- **`journald` Logs:** Emit logs to stdout/stderr; let `systemd`/`journald` handle log rotation.