use std::ffi::OsString;
use std::fmt;
use std::path::Path;
use crumbkit::{Document, Failure, Prose};
use schemars::JsonSchema;
use serde::Serialize;
use starmap::{Mode, Name};
use crate::Failed;
use crate::cli::CheckArgs;
use crate::codes::VARIABLE_UNDECLARED;
use crate::mode;
use crate::scan;
use crate::spec;
pub(crate) const CHECK: Document<Prose<Checked, Undeclared>> = Document::new(
"starmap",
"check",
1,
"A spec that follows every rule of the file: its path, digest, number of variables, the reads in src and the mode.",
);
#[derive(Debug, Serialize, JsonSchema)]
pub(crate) struct Checked {
spec: String,
digest: String,
variables: u64,
read: u64,
undeclared: Vec<String>,
#[serde(skip)]
src: String,
mode: ModeWord,
}
#[derive(Debug, Serialize, JsonSchema)]
#[serde(rename_all = "lowercase")]
pub(crate) enum ModeWord {
Local,
Production,
}
impl From<Mode> for ModeWord {
fn from(mode: Mode) -> Self {
match mode {
Mode::Local => Self::Local,
Mode::Production => Self::Production,
}
}
}
impl fmt::Display for Checked {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
let (declared, read) = (self.variables, self.read);
let noun = if declared == 1 {
"variable"
} else {
"variables"
};
let reads = match read {
1 if declared == 1 => "it".to_owned(),
0 => "none of them".to_owned(),
_ if read == declared => format!("all {read}"),
_ => format!("{read} of them"),
};
write!(
formatter,
"{} declares {declared} {noun}, and {} reads {reads}.",
self.spec, self.src
)
}
}
#[derive(Debug, Failure)]
#[failure(
code = VARIABLE_UNDECLARED,
hint = "declare {name} in {file}, or stop reading it",
field(name = name.as_str()),
field(at = at.as_str()),
)]
pub(crate) struct Undeclared {
name: Name,
at: String,
file: String,
}
impl fmt::Display for Undeclared {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
formatter,
"{} reads {}, which {} does not declare",
self.at, self.name, self.file
)
}
}
impl std::error::Error for Undeclared {}
pub(crate) fn check(
args: &CheckArgs,
vars: Vec<(OsString, OsString)>,
) -> Result<Prose<Checked, Undeclared>, Failed> {
let path = &args.file.spec;
let file = path.display().to_string();
let spec = spec::load(path)?;
let src = path.parent().unwrap_or(Path::new("")).join("src");
let reads = scan::scan(&src)?;
let bindings = starmap::capture(&spec, vars);
let (mode, note) = mode::decide(&spec, &bindings);
let mut read = 0;
let mut undeclared = Vec::new();
for (name, places) in reads {
if spec.variable(&name).is_some() {
read += 1;
} else {
let at = places.into_iter().next().unwrap_or_default();
undeclared.push(Undeclared {
name,
at,
file: file.clone(),
});
}
}
let checked = Checked {
digest: spec.digest().to_string(),
variables: u64::try_from(spec.variables().count()).unwrap_or(u64::MAX),
read,
undeclared: undeclared.iter().map(|u| u.name.to_string()).collect(),
src: src.display().to_string(),
spec: file,
mode: mode.into(),
};
Ok(Prose::with_failures(checked, undeclared).note(note))
}