//! RULE: a transform never names a table or a column (contract 2, programme//! transforms, 22 Oct 2026). A transform is a pure function over a column//! TYPE; the moment it names `cotton_news_settings` or reads `"next_edition_at"`//! it is one tool's rule wearing a generic name, and belongs in `rules/`.//!//! So no file under `transforms/` — code, comments or tests — holds the name of//! a built-in table, and no string literal in one is a built-in column whose//! name has an underscore. A one-word column (`at`, `source`, `key`) is as often//! an English word or a URL parameter as a column, so it would only make this//! guard cry wolf; a snake_case one can only be a column.use std::collections::BTreeSet;use std::path::Path;/// Every built-in table name, and every built-in column name with an underscore.fn built_in_names() -> (BTreeSet<String>, BTreeSet<String>) {let path = cotton_store::manifest_dir().join("../cotton-store/src/schema/builtin_tables.json");let text = std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display()));let builtins: serde_json::Value = serde_json::from_str(&text).expect("the built-in tables parse");let (mut tables, mut columns) = (BTreeSet::new(), BTreeSet::new());for builtin in builtins.as_object().expect("an object of built-ins").values() {for table in builtin["tables"].as_array().into_iter().flatten() { tables.insert(table["name"].as_str().expect("a table name").to_string());for column in table["columns"].as_array().into_iter().flatten() {let name = column["name"].as_str().expect("a column name");if name.contains('_') { columns.insert(name.to_string()); } } } } assert!(!tables.is_empty() && !columns.is_empty(), "the guard read no built-in tables"); (tables, columns)}/// The string literals in Rust source, comments skipped; raw strings included.fn string_literals(source: &str) -> Vec<String> {let chars: Vec<char> = source.chars().collect();let (mut found, mut i) = (Vec::new(), 0);while i < chars.len() {match chars[i] { '/' if chars.get(i + 1) == Some(&'/') => {while i < chars.len() && chars[i] != '\n' { i += 1; } } '/' if chars.get(i + 1) == Some(&'*') => { i += 2;while i + 1 < chars.len() && !(chars[i] == '*' && chars[i + 1] == '/') { i += 1; } i += 2; }// A char literal such as '"' is not the start of a string. '\'' if chars.get(i + 2) == Some(&'\'') => i += 3, '\'' if chars.get(i + 1) == Some(&'\\') && chars.get(i + 3) == Some(&'\'') => i += 4, 'r' if matches!(chars.get(i + 1), Some('"') | Some('#')) && (i == 0 || !(chars[i - 1].is_alphanumeric() || chars[i - 1] == '_')) => {letmut j = i + 1;letmut hashes = 0;while chars.get(j) == Some(&'#') { hashes += 1; j += 1; }if chars.get(j) != Some(&'"') { i += 1;continue; }let start = j + 1;letmut end = start;while end < chars.len() {if chars[end] == '"' && (1..=hashes).all(|k| chars.get(end + k) == Some(&'#')) {break; } end += 1; } found.push(chars[start..end.min(chars.len())].iter().collect()); i = end + 1 + hashes; } '"' => {letmut literal = String::new(); i += 1;while i < chars.len() && chars[i] != '"' {if chars[i] == '\\' { i += 1; }ifletSome(&c) = chars.get(i) { literal.push(c); } i += 1; } found.push(literal); i += 1; } _ => i += 1, } } found}#[test]fn a_transform_never_names_a_table_or_a_column() {let (tables, columns) = built_in_names();let dir = cotton_store::manifest_dir().join("src/transforms");letmut offences = Vec::new();letmut scanned = 0;for entry in std::fs::read_dir(&dir).unwrap_or_else(|e| panic!("{}: {e}", dir.display())) {let path = entry.expect("a directory entry").path();if path.extension().is_none_or(|ext| ext != "rs") || path.file_name() == Some("guard_tests.rs".as_ref()) {continue; }let source = std::fs::read_to_string(&path).expect("a readable source file");let name = Path::new(&path).file_name().unwrap().to_string_lossy().to_string();for word in source.split(|c: char| !(c.is_ascii_alphanumeric() || c == '_')) {if tables.contains(word) { offences.push(format!("{name} names the table {word}")); } }for literal in string_literals(&source) {if literal.starts_with("cotton_") || columns.contains(&literal) { offences.push(format!("{name} holds the literal \"{literal}\"")); } } scanned += 1; } assert!(scanned > 0, "the guard read no transform"); offences.sort(); offences.dedup(); assert!(offences.is_empty(), "a transform names a table or a column:\n{}", offences.join("\n"));}#[test]fn the_literal_reader_finds_plain_and_raw_strings_but_not_comments() {let source = "// \"in_comment\"\nlet a = \"plain_one\"; let b = r#\"raw \"q\" two\"#; let c = '\"'; /* \"block\" */"; assert_eq!(string_literals(source), vec!["plain_one".to_string(), "raw \"q\" two".to_string()]);}