//! The plain columns of `cotton_secrets`, and how long each may be. //! //! **DECLARED ONCE, HERE.** The table holds these as CHECKs //! (`cotton-store/src/schema/ddl.rs`, `SECRETS_TABLE`); every writer that wants //! to refuse — or fit a value — with a sentence rather than surface SQLite's //! own constraint message reads them from this file: the MCP tools //! (`cotton_tools::mcp::handlers::secrets`), the import parsers //! ([`super::import`]) and the import writer. `columns_tests.rs` holds these //! numbers to the DDL, so a schema change that moves one fails a test here. //! //! Lengths are CHARACTERS — Unicode scalar values, which is what SQLite's //! `length()` counts on a TEXT value and what `str::chars().count()` counts — //! never bytes. /// `name`: 1 to 120 characters. pub const NAME_MAX: usize = 120; /// `service`: 1 to 60 characters, lowercase letters, digits, `.`, `_`, `-`. pub const SERVICE_MAX: usize = 60; /// `site`: absent, or 1 to 255 characters. pub const SITE_MAX: usize = 255; /// `username`: absent, or 1 to 255 characters. pub const USERNAME_MAX: usize = 255; /// How many characters a value is, counted the way the table's CHECK counts. pub fn char_len(value: &str) -> usize { value.chars().count() } /// Whether a value is 1 to `max` characters — the shape of every one of /// these columns' CHECKs. pub fn fits(value: &str, max: usize) -> bool { let len = char_len(value); (1..=max).contains(&len) } /// `value` cut to at most `max` characters, on a character boundary, with `…` /// as the last character when anything was cut — so a shortened name reads as /// shortened rather than as a name somebody chose. /// /// For a NAME only: a name is a label a person scans a list for. A username or /// a site that does not fit is a different question (the value would no /// longer be the value), which is why nothing shortens those. pub fn shorten(value: &str, max: usize) -> String { if char_len(value) <= max { return value.to_string(); } if max == 0 { return String::new(); } let mut out: String = value.chars().take(max - 1).collect(); // A cut that lands after a space would leave "word …"; trim it. let trimmed = out.trim_end().len(); out.truncate(trimmed); out.push('…'); out } #[cfg(test)] #[path = "columns_tests.rs"] mod tests;