use std::path::Path;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Kind {
Identifiers,
Docs,
Strings,
}
impl Kind {
pub const ALL: [Kind; 3] = [Kind::Identifiers, Kind::Docs, Kind::Strings];
pub fn file(self) -> &'static str {
match self {
Kind::Identifiers => "identifiers.txt",
Kind::Docs => "docs.txt",
Kind::Strings => "strings.txt",
}
}
fn of_file(name: &str) -> Option<Kind> {
Kind::ALL.into_iter().find(|kind| kind.file() == name || kind.file().strip_suffix(".txt") == Some(name))
}
}
#[derive(Default)]
pub struct Ruleset {
pub name: String,
pub identifiers: Option<String>,
pub docs: Option<String>,
pub strings: Option<String>,
pub identifiers_advice: String,
pub strings_advice: String,
}
impl Ruleset {
pub fn rules(&self, kind: Kind) -> Option<&str> {
match kind {
Kind::Identifiers => self.identifiers.as_deref(),
Kind::Docs => self.docs.as_deref(),
Kind::Strings => self.strings.as_deref(),
}
}
}
const BUILT_IN: &[(&str, &[(&str, &str)])] = &[(
"claude@5",
&[
("identifiers.txt", include_str!("claude@5/identifiers.txt")),
("docs.txt", include_str!("claude@5/docs.txt")),
("strings.txt", include_str!("claude@5/strings.txt")),
("identifiers-advice.txt", include_str!("claude@5/identifiers-advice.txt")),
("strings-advice.txt", include_str!("claude@5/strings-advice.txt")),
],
)];
pub fn resolve(arg: &str) -> Result<(Ruleset, Option<Kind>), String> {
let path = Path::new(arg);
let in_dir = path.parent().is_some_and(|p| p.is_dir()) && path.file_name().is_some_and(|n| n != "..");
if path.exists() || in_dir && !arg.ends_with('/') {
let (dir, kind) = match path.is_dir() {
true => (path, None),
false => (path.parent().unwrap_or(Path::new(".")), Some(kind_of(arg, path)?)),
};
let read = |name: &str| {
let file = dir.join(name);
file.exists().then(|| std::fs::read_to_string(&file).map_err(|e| format!("{}: {e}", file.display())))
};
let ruleset = Ruleset {
name: dir.display().to_string(),
identifiers: read(Kind::Identifiers.file()).transpose()?,
docs: read(Kind::Docs.file()).transpose()?,
strings: read(Kind::Strings.file()).transpose()?,
identifiers_advice: read("identifiers-advice.txt").transpose()?.unwrap_or_default(),
strings_advice: read("strings-advice.txt").transpose()?.unwrap_or_default(),
};
if Kind::ALL.into_iter().all(|kind| ruleset.rules(kind).is_none()) {
return Err(format!("{arg} has no rules: expected {}", files()));
}
return Ok((ruleset, kind));
}
let (name, file) = arg.split_once('/').map_or((arg, None), |(n, f)| (n, Some(f)));
let Some((name, files)) = BUILT_IN.iter().find(|(n, _)| *n == name) else {
let names: Vec<&str> = BUILT_IN.iter().map(|(n, _)| *n).collect();
return Err(format!("{arg} is neither a built-in ruleset, which are {}, nor a path", names.join(", ")));
};
let kind = file.map(|f| kind_of(arg, Path::new(f))).transpose()?;
let text = |file: &str| files.iter().find(|(f, _)| *f == file).map(|(_, text)| text.to_string());
let ruleset = Ruleset {
name: name.to_string(),
identifiers: text(Kind::Identifiers.file()),
docs: text(Kind::Docs.file()),
strings: text(Kind::Strings.file()),
identifiers_advice: text("identifiers-advice.txt").unwrap_or_default(),
strings_advice: text("strings-advice.txt").unwrap_or_default(),
};
Ok((ruleset, kind))
}
fn kind_of(arg: &str, path: &Path) -> Result<Kind, String> {
let name = path.file_name().map(|n| n.to_string_lossy()).unwrap_or_default();
Kind::of_file(&name).ok_or_else(|| format!("{arg}: a rules file is {}", files()))
}
fn files() -> String {
let names: Vec<&str> = Kind::ALL.into_iter().map(Kind::file).collect();
match names.split_last() {
Some((last, rest)) if !rest.is_empty() => format!("{} or {last}", rest.join(", ")),
_ => names.concat(),
}
}