use std::collections::BTreeSet;
use std::path::PathBuf;
use quote::ToTokens;
pub type Module = (String, String);
#[must_use]
pub fn crate_dir(crate_name: &str) -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("..")
.join(crate_name)
}
#[must_use]
pub fn read(path: &std::path::Path) -> String {
std::fs::read_to_string(path).unwrap_or_else(|error| panic!("read {}: {error}", path.display()))
}
#[must_use]
pub fn parse(source: &str) -> syn::File {
syn::parse_file(source).expect("the source parses as Rust")
}
#[must_use]
pub fn crate_modules(crate_name: &str) -> Vec<Module> {
let dir = crate_dir(crate_name).join("src");
let mut paths: Vec<PathBuf> = std::fs::read_dir(&dir)
.unwrap_or_else(|error| panic!("read {}: {error}", dir.display()))
.map(|entry| entry.expect("a readable directory entry").path())
.filter(|path| path.extension().is_some_and(|extension| extension == "rs"))
.collect();
paths.sort();
paths
.into_iter()
.map(|path| {
let name = path
.file_stem()
.expect("a file stem")
.to_string_lossy()
.into_owned();
(name, read(&path))
})
.collect()
}
#[must_use]
pub fn crate_test_sources(crate_name: &str) -> Vec<Module> {
let dir = crate_dir(crate_name).join("tests");
let mut found = Vec::new();
walk_rust_files(&dir, &mut found);
found.sort_by(|left, right| left.0.cmp(&right.0));
found
}
fn walk_rust_files(dir: &std::path::Path, found: &mut Vec<Module>) {
let Ok(entries) = std::fs::read_dir(dir) else {
return;
};
for entry in entries {
let path = entry.expect("a readable directory entry").path();
if path.is_dir() {
walk_rust_files(&path, found);
} else if path.extension().is_some_and(|extension| extension == "rs") {
found.push((path.to_string_lossy().into_owned(), read(&path)));
}
}
}
#[must_use]
pub fn tokens_without_literals(source: &str) -> String {
strip_literals(&parse(source).to_token_stream().to_string())
}
#[must_use]
pub fn strip_literals(rendered: &str) -> String {
let mut kept = String::new();
let mut inside = false;
let mut escaped = false;
for character in rendered.chars() {
if inside {
if escaped {
escaped = false;
} else if character == '\\' {
escaped = true;
} else if character == '"' {
inside = false;
}
continue;
}
if character == '"' {
inside = true;
continue;
}
kept.push(character);
}
kept
}
#[must_use]
pub fn words(tokens: &str) -> Vec<String> {
let mut spaced = tokens.to_owned();
for delimiter in [
'(', ')', '[', ']', '{', '}', ',', ';', '<', '>', '&', '#', '!',
] {
spaced = spaced.replace(delimiter, &format!(" {delimiter} "));
}
spaced.split_whitespace().map(str::to_owned).collect()
}
#[must_use]
pub fn references(tokens: &str, names: &[String]) -> Vec<String> {
let words = words(tokens);
let mut found = Vec::new();
for pair in words.windows(2) {
if (pair[0] == "." || pair[0] == "::") && names.contains(&pair[1]) {
found.push(pair[1].clone());
}
}
found
}
#[must_use]
pub fn mentions(tokens: &str, identifier: &str) -> usize {
words(tokens)
.iter()
.filter(|word| *word == identifier)
.count()
}
fn spelling<T: ToTokens>(node: &T) -> String {
node.to_token_stream().to_string().replace([' ', '\n'], "")
}
fn type_tokens<T: ToTokens>(node: &T) -> String {
node.to_token_stream().to_string()
}
#[must_use]
pub fn trait_method_names(file: &syn::File, trait_name: &str) -> Vec<String> {
let mut names = Vec::new();
for item in &file.items {
let syn::Item::Trait(declaration) = item else {
continue;
};
if declaration.ident != trait_name {
continue;
}
for entry in &declaration.items {
if let syn::TraitItem::Fn(function) = entry {
names.push(function.sig.ident.to_string());
}
}
}
names
}
#[must_use]
pub fn trait_method_params(file: &syn::File, trait_name: &str) -> Vec<(String, String, String)> {
let mut found = Vec::new();
for item in &file.items {
let syn::Item::Trait(declaration) = item else {
continue;
};
if declaration.ident != trait_name {
continue;
}
for entry in &declaration.items {
let syn::TraitItem::Fn(function) = entry else {
continue;
};
for input in &function.sig.inputs {
if let syn::FnArg::Typed(typed) = input {
found.push((
function.sig.ident.to_string(),
spelling(&typed.pat),
spelling(&typed.ty),
));
}
}
}
}
found
}
#[must_use]
pub fn enum_variant_names(file: &syn::File, enum_name: &str) -> Vec<String> {
let mut names = Vec::new();
for item in &file.items {
let syn::Item::Enum(declaration) = item else {
continue;
};
if declaration.ident != enum_name {
continue;
}
for variant in &declaration.variants {
names.push(variant.ident.to_string());
}
}
names
}
#[must_use]
pub fn impls_of_trait(file: &syn::File, trait_name: &str) -> usize {
file.items
.iter()
.filter(|item| match item {
syn::Item::Impl(block) => block.trait_.as_ref().is_some_and(|(path, _)| {
path.segments
.last()
.is_some_and(|segment| segment.ident == trait_name)
}),
_ => false,
})
.count()
}
#[must_use]
pub fn functions(file: &syn::File) -> Vec<(String, String)> {
let mut found = Vec::new();
collect_functions(&file.items, &mut found);
found
}
fn collect_functions(items: &[syn::Item], found: &mut Vec<(String, String)>) {
for item in items {
match item {
syn::Item::Fn(function) => {
found.push((
function.sig.ident.to_string(),
function.to_token_stream().to_string(),
));
}
syn::Item::Impl(block) => {
for entry in &block.items {
if let syn::ImplItem::Fn(function) = entry {
found.push((
function.sig.ident.to_string(),
function.to_token_stream().to_string(),
));
}
}
}
syn::Item::Trait(declaration) => {
for entry in &declaration.items {
if let syn::TraitItem::Fn(function) = entry
&& function.default.is_some()
{
found.push((
function.sig.ident.to_string(),
function.to_token_stream().to_string(),
));
}
}
}
syn::Item::Mod(module) => {
if let Some((_, items)) = &module.content {
collect_functions(items, found);
}
}
_ => {}
}
}
}
#[must_use]
pub fn fields_mentioning(file: &syn::File, identifier: &str) -> Vec<(String, String, bool)> {
let mut found = Vec::new();
for item in &file.items {
let syn::Item::Struct(declaration) = item else {
continue;
};
for field in &declaration.fields {
if mentions(&type_tokens(&field.ty), identifier) == 0 {
continue;
}
found.push((
declaration.ident.to_string(),
field
.ident
.as_ref()
.map_or_else(|| "0".to_owned(), ToString::to_string),
matches!(field.vis, syn::Visibility::Public(_)),
));
}
}
found
}
#[must_use]
pub fn returns_mentioning(file: &syn::File, identifier: &str) -> Vec<String> {
let mut found = Vec::new();
collect_signatures(&file.items, &mut found);
found
.into_iter()
.filter_map(|(name, returns, _)| (mentions(&returns, identifier) > 0).then_some(name))
.collect()
}
fn collect_signatures(items: &[syn::Item], found: &mut Vec<(String, String, Vec<String>)>) {
let mut push = |signature: &syn::Signature| {
let returns = match &signature.output {
syn::ReturnType::Default => "()".to_owned(),
syn::ReturnType::Type(_, ty) => type_tokens(ty),
};
let params = signature
.inputs
.iter()
.filter_map(|input| match input {
syn::FnArg::Typed(typed) => Some(type_tokens(&typed.ty)),
syn::FnArg::Receiver(_) => None,
})
.collect();
found.push((signature.ident.to_string(), returns, params));
};
for item in items {
match item {
syn::Item::Fn(function) => push(&function.sig),
syn::Item::Impl(block) => {
for entry in &block.items {
if let syn::ImplItem::Fn(function) = entry {
push(&function.sig);
}
}
}
syn::Item::Trait(declaration) => {
for entry in &declaration.items {
if let syn::TraitItem::Fn(function) = entry {
push(&function.sig);
}
}
}
_ => {}
}
}
}
pub struct Universe {
sources: Vec<syn::File>,
}
#[derive(Debug, Default, PartialEq, Eq)]
pub struct Findings {
pub strings: Vec<String>,
pub bytes: Vec<String>,
pub reached: BTreeSet<String>,
}
const PRIMITIVES: [&str; 18] = [
"bool",
"char",
"f32",
"f64",
"i8",
"i16",
"i32",
"i64",
"i128",
"isize",
"u8",
"u16",
"u32",
"u64",
"u128",
"usize",
"PathBuf",
"Timestamp",
];
const CONTAINERS: [&str; 8] = [
"Option", "Vec", "Box", "Arc", "Rc", "BTreeMap", "HashMap", "Result",
];
impl Universe {
#[must_use]
pub const fn new(sources: Vec<syn::File>) -> Self {
Self { sources }
}
fn struct_fields(&self, name: &str) -> Option<Vec<(String, String)>> {
for source in &self.sources {
for item in &source.items {
let syn::Item::Struct(declaration) = item else {
continue;
};
if declaration.ident == name {
return Some(
declaration
.fields
.iter()
.enumerate()
.map(|(index, field)| {
(
field
.ident
.as_ref()
.map_or_else(|| index.to_string(), ToString::to_string),
spelling(&field.ty),
)
})
.collect(),
);
}
}
}
None
}
fn enum_fields(&self, name: &str) -> Option<Vec<(String, String)>> {
for source in &self.sources {
for item in &source.items {
let syn::Item::Enum(declaration) = item else {
continue;
};
if declaration.ident == name {
let mut fields = Vec::new();
for variant in &declaration.variants {
for (index, field) in variant.fields.iter().enumerate() {
fields.push((
format!(
"{}::{}",
variant.ident,
field
.ident
.as_ref()
.map_or_else(|| index.to_string(), ToString::to_string)
),
spelling(&field.ty),
));
}
}
return Some(fields);
}
}
}
None
}
fn walk(
&self,
path: &str,
spelling: &str,
findings: &mut Findings,
seen: &mut BTreeSet<String>,
) {
if spelling.contains("Vec<u8>")
|| spelling.contains("[u8]")
|| spelling.contains("RawBytes")
{
findings.bytes.push(path.to_owned());
}
for name in identifiers(spelling) {
if name == "String" || name == "str" {
findings.strings.push(path.to_owned());
continue;
}
if PRIMITIVES.contains(&name.as_str()) || CONTAINERS.contains(&name.as_str()) {
continue;
}
findings.reached.insert(name.clone());
if !seen.insert(name.clone()) {
continue;
}
let resolved = self
.struct_fields(&name)
.or_else(|| self.enum_fields(&name));
if let Some(fields) = resolved {
for (field, ty) in fields {
self.walk(&format!("{path}/{name}.{field}"), &ty, findings, seen);
}
}
}
}
#[must_use]
pub fn closure(&self, roots: &[(String, String)]) -> Findings {
let mut findings = Findings::default();
let mut seen = BTreeSet::new();
for (path, spelling) in roots {
self.walk(path, spelling, &mut findings, &mut seen);
}
findings
}
}
fn identifiers(spelling: &str) -> Vec<String> {
let mut names = Vec::new();
let mut current = String::new();
for character in spelling.chars() {
if character.is_alphanumeric() || character == '_' {
current.push(character);
} else if !current.is_empty() {
names.push(core::mem::take(&mut current));
}
}
if !current.is_empty() {
names.push(current);
}
names
}
#[must_use]
pub fn chokepoint_findings(
modules: &[Module],
chokepoint: &str,
action_methods: &[String],
) -> Vec<String> {
let mut findings = Vec::new();
let mut claimed: Vec<(String, String)> = Vec::new();
for (name, source) in modules {
for (function, tokens) in functions(&parse(source)) {
if function == chokepoint {
claimed.push((name.clone(), strip_literals(&tokens)));
}
}
}
if claimed.len() != 1 {
findings.push(format!(
"{} functions claim the chokepoint role `{chokepoint}`, and exactly one may",
claimed.len()
));
}
let inside: usize = claimed
.iter()
.map(|(_, tokens)| references(tokens, action_methods).len())
.sum();
let mut total = 0;
for (name, source) in modules {
let found = references(&tokens_without_literals(source), action_methods);
let here = found.len();
total += here;
let owned = claimed
.iter()
.filter(|(module, _)| module == name)
.map(|(_, tokens)| references(tokens, action_methods).len())
.sum::<usize>();
if here > owned {
findings.push(format!(
"`{name}` references {found:?} outside the chokepoint"
));
}
}
if total != inside {
findings.push(format!(
"{total} action-method references in the tree and {inside} inside the chokepoint"
));
}
findings
}
#[must_use]
pub fn handle_findings(modules: &[Module], chokepoint_module: &str, handle: &str) -> Vec<String> {
let mut findings = Vec::new();
let mut fields = Vec::new();
for (name, source) in modules {
let file = parse(source);
if name != chokepoint_module && mentions(&tokens_without_literals(source), handle) > 0 {
findings.push(format!(
"`{name}` names `{handle}`, and only the chokepoint's module may"
));
}
for (holder, field, public) in fields_mentioning(&file, handle) {
fields.push((name.clone(), holder, field, public));
}
for function in returns_mentioning(&file, handle) {
findings.push(format!("`{name}::{function}` hands `{handle}` back out"));
}
}
if fields.len() != 1 {
findings.push(format!(
"{} fields hold `{handle}`, and exactly one may",
fields.len()
));
}
for (module, holder, field, public) in fields {
if module != chokepoint_module {
findings.push(format!(
"`{module}::{holder}.{field}` holds `{handle}` outside the chokepoint's module"
));
}
if public {
findings.push(format!("`{holder}.{field}` is visible beyond its module"));
}
}
findings
}
#[must_use]
pub fn manifest_dependencies(crate_name: &str, table: &str) -> Vec<String> {
let manifest = read(&crate_dir(crate_name).join("Cargo.toml"));
let mut names = Vec::new();
let mut inside = false;
for line in manifest.lines() {
let trimmed = line.trim();
if trimmed.starts_with('[') {
inside = trimmed == format!("[{table}]");
continue;
}
if !inside || trimmed.is_empty() || trimmed.starts_with('#') {
continue;
}
if let Some((name, _)) = trimmed.split_once('=') {
names.push(name.trim().to_owned());
}
}
names.sort();
names
}