use std::fs;
use std::path::{Path, PathBuf};
use jsonvet::{Config, Dialect, Rule, Severity};
use toml::{Table, Value};
use crate::glob;
use crate::output::printable;
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct DialectPattern {
pub(crate) files: Option<String>,
pub(crate) dialect: Option<Dialect>,
pub(crate) base: Option<String>,
}
impl DialectPattern {
pub(crate) fn parse(value: &str) -> Result<Self, String> {
let (name, files) = match value.split_once(':') {
Some((name, files)) => (name, Some(files.to_owned())),
None => (value, None),
};
Ok(DialectPattern {
files,
dialect: parse_dialect(name)?,
base: None,
})
}
}
fn parse_dialect(name: &str) -> Result<Option<Dialect>, String> {
if name == "auto" {
return Ok(None);
}
name.parse()
.map(Some)
.map_err(|e: jsonvet::UnknownDialect| format!("{e}, or auto"))
}
pub(crate) fn parse_severity(name: &str) -> Result<Severity, String> {
match name {
"error" => Ok(Severity::Error),
"warning" | "warn" => Ok(Severity::Warning),
"ignore" | "off" => Ok(Severity::Off),
_ => Err(format!(
"unknown severity `{name}`; expected error, warning or ignore"
)),
}
}
pub(crate) fn parse_rule(id: &str) -> Result<Rule, String> {
if id == crate::sarif::SCHEMA {
return Err("the `schema` rule is always an error and cannot be set".to_owned());
}
id.parse().map_err(|e| format!("{e}"))
}
#[derive(Clone, Debug, Default)]
pub(crate) struct Settings {
pub(crate) dialects: Vec<DialectPattern>,
pub(crate) rules: Vec<(Rule, Severity)>,
}
impl Settings {
pub(crate) fn overridden_by(mut self, later: Settings) -> Settings {
self.dialects.extend(later.dialects);
self.rules.extend(later.rules);
self
}
pub(crate) fn config_for(&self, path: &str) -> Config {
let auto = || Dialect::for_path(Path::new(path)).unwrap_or(Dialect::Json5);
let absolute = std::cell::OnceCell::new();
let matches = |rule: &DialectPattern| match (&rule.files, &rule.base) {
(None, _) => true,
(Some(pattern), None) => glob::matches(pattern, path),
(Some(pattern), Some(base)) => absolute
.get_or_init(|| std::path::absolute(path).ok())
.as_deref()
.map(Path::to_string_lossy)
.and_then(|absolute| glob::relative(&absolute, base))
.is_some_and(|relative| glob::matches(pattern, &relative)),
};
let dialect = self
.dialects
.iter()
.rev()
.find(|rule| matches(rule))
.and_then(|rule| rule.dialect)
.unwrap_or_else(auto);
self.rules
.iter()
.fold(Config::new(dialect), |config, &(rule, severity)| {
config.rule(rule, severity)
})
}
}
pub(crate) fn discover(start: &Path) -> Option<PathBuf> {
for dir in start.ancestors() {
let own = dir.join("jsonvet.toml");
if own.is_file() {
return Some(own);
}
let pyproject = dir.join("pyproject.toml");
if fs::read_to_string(&pyproject).is_ok_and(|text| has_jsonvet_table(&text)) {
return Some(pyproject);
}
if dir.join(".git").exists() {
return None;
}
}
None
}
fn has_jsonvet_table(text: &str) -> bool {
match text.parse::<Table>() {
Ok(table) => table
.get("tool")
.and_then(|tool| tool.get("jsonvet"))
.is_some(),
Err(_) => text.contains("[tool.jsonvet"),
}
}
pub(crate) fn load(path: &Path) -> Result<Settings, String> {
let in_file = |e: &dyn std::fmt::Display| printable(&format!("{}: {e}", path.display()));
let text = fs::read_to_string(path).map_err(|e| in_file(&e))?;
let table: Table = text
.parse()
.map_err(|e: toml::de::Error| in_file(&e.message()))?;
let file = if path.file_name().is_some_and(|n| n == "pyproject.toml") {
match table.get("tool").and_then(|tool| tool.get("jsonvet")) {
Some(Value::Table(file)) => file,
Some(_) => return Err(in_file(&"`tool.jsonvet` must be a table")),
None => return Err(in_file(&"no [tool.jsonvet] table")),
}
} else {
&table
};
let base = std::path::absolute(path).map_err(|e| in_file(&e))?;
let base = base.parent().map(|dir| dir.to_string_lossy().into_owned());
let mut settings = Settings::default();
for (key, value) in file {
match (key.as_str(), value) {
("rules", Value::Table(rules)) => {
for (id, severity) in rules {
let rule = parse_rule(id).map_err(|e| in_file(&e))?;
let severity = severity
.as_str()
.ok_or_else(|| in_file(&format!("`rules.{id}` must be a string")))?;
let severity = parse_severity(severity).map_err(|e| in_file(&e))?;
settings.rules.push((rule, severity));
}
}
("dialects", Value::Array(entries)) => {
for entry in entries {
let mut pattern = dialect_entry(entry).map_err(|e| in_file(&e))?;
pattern.base.clone_from(&base);
settings.dialects.push(pattern);
}
}
("rules", _) => return Err(in_file(&"`rules` must be a table")),
("dialects", _) => return Err(in_file(&"`dialects` must be an array of tables")),
_ => {
return Err(in_file(&format!(
"unknown key `{key}`; expected rules or dialects"
)));
}
}
}
Ok(settings)
}
fn dialect_entry(entry: &Value) -> Result<DialectPattern, String> {
let entry = entry
.as_table()
.ok_or("each `dialects` entry must be a table")?;
if let Some(key) = entry
.keys()
.find(|k| !matches!(k.as_str(), "files" | "dialect"))
{
return Err(format!(
"unknown key `{key}` in a `dialects` entry; expected files and dialect"
));
}
let string = |key: &str| {
entry
.get(key)
.and_then(Value::as_str)
.ok_or_else(|| format!("each `dialects` entry needs a `{key}` string"))
};
Ok(DialectPattern {
files: Some(string("files")?.to_owned()),
dialect: parse_dialect(string("dialect")?)?,
base: None,
})
}