// PART 11
Coding standards
The standards below are the repository’s working rules. They are enforced by CI where a machine can check them and in review where only a human can. The common thread: errors are handled, not suppressed; files stay small; the schema has one home; the user never sees an internal error.
Error handling — no panics in production
botserver serves thousands of simultaneous sessions; any panic crashes the
process for everyone. unwrap, expect, panic!, todo!,
unimplemented! are forbidden outside tests.
| Forbidden | Replacement |
|---|---|
value.unwrap() |
value?, or `value.ok_or_else( |
value.expect("msg") |
value.context("msg")? or map_err |
panic!("...") |
return Err(Error::X.into()); |
todo!() / unimplemented!() |
a real body, or Err(Error::NotImplemented.into()) |
match x { Ok(v) => v, Err(_) => default } |
log the error, then fall back — never silently |
Error responses go through log_and_sanitize: the detail is logged, the
client gets a safe message.
Security-mandatory patterns
| Instead of | Use |
|---|---|
Command::new(user_input) |
SafeCommand::new("allowed_command")?.arg("safe_arg")?.execute() |
format!("SELECT * FROM {}", table) |
sql_guard::sanitize_identifier + validate_table_name |
Json(json!({ "error": e.to_string() })) |
log_and_sanitize(&e, "context", None) |
Style rules
impl MyStruct { fn new() -> Self { Self { } } } // Self, not the type name
#[derive(PartialEq, Eq)] // always both
format!("Hello {name}") // inline args
match x { A | B => do_thing(), C => other() } // combine identical arms
- No
#[allow()]— fix the code; never add lint exceptions in code orCargo.toml. - No unused parameters — delete or use them; no
_prefixes for ignored values. - No commented-out code — fix it or delete it.
- Files under 450 lines — split proactively at ~350 into submodules
(
types.rs,handlers.rs,operations.rs,utils.rs) and updatemod.rs. - No CDN links, no new files at the repository root.
- Formatting (
cargo fmt) and clippy (0 warnings) are gates, not suggestions.
Diesel schema ownership
table!/joinable! live only in the botschema crate. Change schema via
a migration → diesel migration run → diesel print-schema → update
botschema → commit together. Consume with
botschema = { workspace = true, features = [...] } and re-export
(pub use botschema::<table>;).
Frontend rules
- Design system: every color, font, radius, shadow comes from the
--id-*tokens (botui/ui/suite/identity/tokens.css); the default theme is identical toDESIGN-SYSTEM.md; other themes vary only the accent. - i18n: no hardcoded user-facing strings. HTML:
data-i18nattributes; JS:gbT("key", "English fallback"). Every new key goes into all locale catalogs (botlib/locales/{en,it,ru,pt-BR,es,fr,de,ja,ko,zh-CN}/*.ftl). Noauto-*keys; no reusing a key with a different meaning. - JS modules: ordered plain scripts (
01_state.js…99_init.js),"use strict";, under 450 lines each, globally visible via load order. - HTMX first: server returns HTML fragments, not JSON, for UI interactions.
- Absolute asset paths in subdirectory apps (
/suite/<app>/<app>.css).
Git and delivery
- One concern per commit, split by area; never
git add -A. - Push to ALM and origin; CI builds and deploys — never copy binaries to production by hand.
- Plans live in GitHub issues, not in docs.
- Update
botbook/when user-facing behavior changed.
Dependency management
Application crates track Cargo.lock; library crates do not. Critical
crypto/security dependencies use exact versions. cargo audit weekly;
cargo tree --duplicates and cargo machete as hygiene. Features are
opt-in (default-features = false where sensible).