Cotton code
145 lines · 5.8 KB · RustRaw
//! The one rule this crate exists to keep: it depends on `cotton-store` and//! `cotton-core` and nothing that only a server has.//!//! Design P3. The tools left `cotton-server` because the PHONE authors through//! them, and a phone that linked the server would link axum, hyper, `reqwest`'s//! http2 stack, `rustls` and mupdf's vendored C tree to write a todo. Nothing//! about a handler resists the move — it is a synchronous function over a//! driver and an injected clock — so the only thing that can quietly undo it is//! somebody adding a dependency here, which is what this reads.//!//! An ALLOW list, not a ban list. A new dependency has to be added here on//! purpose, with a reason written beside it in `Cargo.toml`, because "does this//! pull a runtime in" is a question a regex cannot answer and a reader can —//! and because a ban list only ever catches the names somebody already thought//! of.//!//! **This reads the manifest, so it sees DIRECT dependencies only.** The other//! half — "no server crate reaches a phone binary however it arrives" — is//! `cotton-ffi/tests/phone_boundary.rs`, over the RESOLVED graph that//! `cargo tree` prints; it is the stronger statement, because it sees a crate//! that arrives TRANSITIVELY and a manifest reader cannot. The ban list below//! is the weaker half and is kept anyway: it names the crates whose arrival//! here would be the specific mistake, so a red run says which. The runnable//! proof a person can see for themselves is still://!//!   cargo tree -p cotton-tools -e normal | grep -E "axum|tokio|reqwest|mupdf|rustls"//!//! must print nothing.use std::collections::BTreeSet;/// Every crate `cotton-tools` may name in `[dependencies]`.////// `tracing` is a FACADE and belongs here: it compiles to nothing without a/// subscriber, and the subscriber on a phone is whatever the app installs./// `rand` brings no executor. Everything else is data, SQLite, or a domain/// crate that itself keeps this rule.const ALLOWED: &[&str] = &[    "base64",    "chrono",    "cotton-core",    "cotton-store",    "hex",    "miniz_oxide",    "rand",    "regex",    "rusqlite",    "serde",    "serde_json",    "sha2",    "thiserror",    "tracing",    // A URL parser and nothing else: `connections::judge` reads a server's    // host; the crate still opens no socket (cotton-core links it already).    "url",];/// What may never appear, however it arrives — named so a red run says which.////// Redundant with `cotton-ffi/tests/phone_boundary.rs` for most of these, and/// kept anyway: that guard reads the RESOLVED graph and is the stronger/// statement, but this one names the crates whose arrival here would be the/// specific mistake moving the handlers out of `cotton-server` was for, and it/// says so in the failure. A guard that passes and whose rule is still true is/// not a guard to delete.const BANNED: &[&str] = &[    "axum",    "cotton-ai",    "cotton-server",    "hyper",    "mupdf",    "reqwest",    "rustls",    "tokio",    "tower",    "zip",];/// The `[dependencies]` section's own keys, read out of the manifest.////// A hand-rolled reader rather than a TOML crate: this test must not itself add/// a dependency to the crate whose dependencies it is guarding, and the section/// it reads is a flat list of `name = …` lines.fn declared_dependencies() -> BTreeSet<String> {    let manifest = std::fs::read_to_string(cotton_store::manifest_dir().join("Cargo.toml"))        .expect("cotton-tools/Cargo.toml is readable");    let mut found = BTreeSet::new();    let mut inside = false;    for line in manifest.lines() {        let line = line.trim();        if line.starts_with('[') {            inside = line == "[dependencies]";            continue;        }        if !inside || line.is_empty() || line.starts_with('#') {            continue;        }        if let Some((key, _)) = line.split_once('=') {            // `base64.workspace = true` names `base64`: the crate is the part            // before the first dot, and everything after it is how the version            // is stated rather than which crate it is.            let name = key.trim().split('.').next().unwrap_or("").trim();            if !name.is_empty() {                found.insert(name.to_string());            }        }    }    assert!(        !found.is_empty(),        "cotton-tools/Cargo.toml has no [dependencies] section — the manifest moved or its shape \         changed, and this guard would have gone quiet rather than red"    );    found}/// RULE: The tools depend only on the database format and the domain, so the phone runs the same tools the server does.#[test]fn cotton_tools_depends_on_nothing_a_phone_cannot_link() {    let declared = declared_dependencies();    let allowed: BTreeSet<String> = ALLOWED.iter().map(|name| name.to_string()).collect();    let unexpected: Vec<&String> = declared.difference(&allowed).collect();    assert!(        unexpected.is_empty(),        "cotton-tools declares {unexpected:?}, which this guard has not been told about. If it is \         genuinely phone-safe — no runtime, no network stack, no vendored C tree — add it to \         ALLOWED here WITH ITS REASON in Cargo.toml. If it is not, the handler that wanted it \         needs a rail instead (see src/rails.rs)."    );}#[test]fn the_server_only_crates_are_named_and_absent() {    let declared = declared_dependencies();    for banned in BANNED {        assert!(            !declared.contains(*banned),            "cotton-tools declares {banned}, which is exactly what moving the handlers out of \             cotton-server was for"        );        assert!(            !ALLOWED.contains(banned),            "{banned} is in both ALLOWED and BANNED — one of the two lists is wrong"        );    }}