//! AN ACTOR IS NEVER BUILT FROM A BARE STRING. `cotton_store::actor` builds an //! `Actor` from typed ids, and a typed id from a row this file holds or a name //! written in code. A public constructor that took `&str` or `String` would //! let a route hand the fold whatever a request body said it was; this scan //! holds the rule over the module's public surface. //! //! What a public function there may take: typed ids and actors; `&Driver` //! together with the id it looks up (the file is the context the id is //! checked against); a `&'static str` (a name in the source); an //! [`cotton_store::mint::IdSource`] (a minted id). The one parser, //! `from_wire`, is `pub(crate)`. fn actor_source() -> String { let path = cotton_store::manifest_dir().join("src/actor.rs"); std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display())) } /// Every `pub fn` signature (not `pub(crate)`), with its parameter list. fn public_signatures(text: &str) -> Vec { let mut found = Vec::new(); for (index, line) in text.lines().enumerate() { let trimmed = line.trim_start(); if !trimmed.starts_with("pub fn ") { continue; } // The signature may wrap; take lines until the one that opens the body. let mut signature = String::new(); for more in text.lines().skip(index) { signature.push_str(more.trim()); signature.push(' '); if more.contains('{') || more.trim_end().ends_with(';') { break; } } found.push(signature); } found } fn takes_bare_text(signature: &str) -> bool { let params = signature .split_once('(') .map(|(_, rest)| rest) .unwrap_or(signature); let params = params.split_once(')').map(|(p, _)| p).unwrap_or(params); let without_static = params.replace("&'static str", ""); without_static.contains("&str") || without_static.contains("String") || without_static.contains("str>") } /// RULE: No public constructor in cotton_store::actor takes a bare string; an id is looked up in the file (with &Driver) or named in code, and an Actor is built only from typed ids. #[test] fn actor_cannot_be_built_from_a_string() { let text = actor_source(); let signatures = public_signatures(&text); assert!(!signatures.is_empty(), "the scanner must find actor constructors"); let mut offending = Vec::new(); for signature in &signatures { if !takes_bare_text(signature) { continue; } // A lookup is allowed to take the id it looks up, beside the file; a // carried token takes its home file's id and its own, checked against // the file at hand (which must not be the home). if signature.contains("&Driver") && (signature.contains("fn in_file(") || signature.contains("fn carried(")) { continue; } offending.push(signature.trim().to_string()); } assert!( offending.is_empty(), "a public constructor in actor.rs takes a bare string:\n {}", offending.join("\n ") ); // The parser exists, and it is crate-only. assert!( text.contains("pub(crate) fn from_wire(text: &str)"), "Actor::from_wire is the one parser, and it stays pub(crate)" ); assert!( !text.contains("pub fn from_wire"), "from_wire must not be public" ); } #[test] fn the_scan_refuses_what_it_says_it_refuses() { assert!(takes_bare_text("pub fn token(id: &str) -> Actor {")); assert!(takes_bare_text("pub fn token(id: String) -> Actor {")); assert!(takes_bare_text( "pub fn token(id: impl Into) -> Actor {" )); assert!(takes_bare_text( "pub fn token(id: impl AsRef) -> Actor {" )); assert!(!takes_bare_text( "pub fn named(name: &'static str) -> Result {" )); assert!(!takes_bare_text("pub fn token(id: TokenId) -> Actor {")); assert!(!takes_bare_text("pub fn as_str(&self) -> &str {")); assert_eq!( public_signatures("impl A {\n pub fn a(\n x: u8,\n ) -> A {\n }\n pub(crate) fn b() {}\n}").len(), 1 ); }