use xur_runtime::doc::{DocFn, dump};
const ANY_ALLOWED: &[&str] = &[
"std.panic",
"std::any",
"std::assert",
"std::fmt",
"std::io",
"std::log",
"std::json",
"std::cli",
"std::mem",
"std::reflect",
"std::http::RequestBuilder.json_body",
"std::http::Response.json",
"record",
];
fn problems(owner: &str, f: &DocFn, out: &mut Vec<String>) {
let at = format!("{owner}.{}", f.name);
if f.raw {
out.push(format!(
"{at}: raw function without a signature (use *_sig)"
));
}
for (i, p) in f.params.iter().enumerate() {
if p.name.is_none() {
out.push(format!("{at}: parameter {i} has no name"));
}
}
let types = f
.params
.iter()
.filter_map(|p| p.ty.as_deref())
.chain(f.ret.as_deref());
for ty in types {
let words: Vec<_> = ty
.split(|c: char| !c.is_alphanumeric() && c != '_')
.collect();
if words.contains(&"_") || ty.contains('&') {
out.push(format!("{at}: unknown or reference type `{ty}`"));
}
if words
.iter()
.any(|w| matches!(*w, "String" | "Vec" | "Box" | "Arc" | "HashMap"))
{
out.push(format!("{at}: Rust type name in `{ty}`"));
}
let allowed = ANY_ALLOWED.iter().any(|a| {
at == *a || at.starts_with(&format!("{a}.")) || at.starts_with(&format!("{a}::"))
});
if words.contains(&"any") && !allowed {
out.push(format!(
"{at}: `any` in `{ty}` (say what it is, or allowlist it here)"
));
}
}
}
#[test]
fn every_std_function_documents_its_signature() {
let tree = dump(&xur_std::registry(), &["std::test"]);
let mut out = Vec::new();
for b in &tree.builtins {
b.methods
.iter()
.for_each(|f| problems(&b.name, f, &mut out));
}
for m in &tree.modules {
m.functions
.iter()
.for_each(|f| problems(&m.path, f, &mut out));
for t in &m.types {
let owner = format!("{}::{}", m.path, t.name);
t.functions
.iter()
.chain(&t.methods)
.for_each(|f| problems(&owner, f, &mut out));
}
}
assert!(
out.is_empty(),
"{} problem(s):\n{}",
out.len(),
out.join("\n")
);
}