use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use fig::Value;
use fig_schema::{Constraint, Finding, Schema, load_schema};
use super::{Failure, source};
pub fn read(path: &Path) -> Result<Value, String> {
let path = path
.to_str()
.ok_or_else(|| format!("{}: not a UTF-8 path", path.display()))?;
source::load(path, None)
}
pub fn load_named(paths: &[String]) -> Result<(Schema<Constraint>, Vec<Finding>), Failure> {
let mut rules = Vec::new();
let mut findings = Vec::new();
for path in paths {
let loaded = load_schema(path, read).map_err(Failure::invocation)?;
rules.extend(loaded.schema.into_rules());
findings.extend(loaded.findings);
}
Ok((Schema::new(rules), findings))
}
pub fn discover(document: &str) -> Result<Option<PathBuf>, Failure> {
let absolute = std::path::absolute(document)
.map_err(|error| Failure::invocation(format!("{document}: {error}")))?;
let Some(start) = absolute.parent() else {
return Ok(None);
};
for directory in start.ancestors() {
let candidates: Vec<PathBuf> = source::extensions()
.flat_map(|ext| {
[
directory.join(format!(".fig-schema.{ext}")),
directory.join(".config").join(format!("fig-schema.{ext}")),
]
})
.filter(|candidate| candidate.is_file())
.collect();
match candidates.as_slice() {
[] => continue,
[one] => return Ok(Some(from_here(one))),
several => {
let names: Vec<String> = several.iter().map(|p| p.display().to_string()).collect();
return Err(Failure::invocation(format!(
"{} holds more than one schema — {} — and which governs {document} \
is not for this program to choose; keep one, or pass --schema",
directory.display(),
names.join(", "),
)));
}
}
}
Ok(None)
}
fn from_here(path: &Path) -> PathBuf {
std::env::current_dir()
.ok()
.and_then(|here| path.strip_prefix(here).ok().map(Path::to_path_buf))
.unwrap_or_else(|| path.to_path_buf())
}
pub struct Finder {
named: Option<Rc<Schema<Constraint>>>,
discovered: BTreeMap<PathBuf, Rc<Schema<Constraint>>>,
}
impl Finder {
pub fn new(named: &[String]) -> Result<Self, Failure> {
let named = if named.is_empty() {
None
} else {
let (schema, _) = load_named(named)?;
Some(Rc::new(schema))
};
Ok(Self {
named,
discovered: BTreeMap::new(),
})
}
pub fn for_document(&mut self, document: &str) -> Result<Rc<Schema<Constraint>>, Failure> {
if let Some(schema) = &self.named {
return Ok(Rc::clone(schema));
}
if document == "-" {
return Err(Failure::usage(
"standard input has no directory to find a schema from; pass --schema",
));
}
let Some(path) = discover(document)? else {
return Err(Failure::invocation(format!(
"no schema governs {document}: no .fig-schema.<ext> or \
.config/fig-schema.<ext> in its directory or any above it; write \
one, or pass --schema"
)));
};
if let Some(schema) = self.discovered.get(&path) {
return Ok(Rc::clone(schema));
}
let loaded = load_schema(&path, read).map_err(Failure::invocation)?;
let schema = Rc::new(loaded.schema);
self.discovered.insert(path, Rc::clone(&schema));
Ok(schema)
}
}