use super::prompt_lines::{hazards, members, pedantic, replacements};
use crate::charset::{CharSet, SetCatalog, builtin};
use crate::judge::{Checker, Config};
use crate::rules::{self, ASCII, Origin, Rule, TEXT, describe};
pub(super) struct Scope {
pub path: String,
pub label: String,
pub grants: Vec<String>,
pub names: Vec<String>,
pub family: String,
pub fidelity: String,
pub allowed: CharSet,
}
pub(super) fn render(
config: &Config,
path: Option<&str>,
) -> Result<String, String> {
let rules = config.rules()?;
let catalog = config.catalog()?;
let scope = match path {
Some(path) => one_path(path, &rules, &catalog)?,
None => whole(&rules),
};
let mut out = vec![intro(&scope)];
out.extend(members(&scope, &catalog)?);
out.extend(replacements(&config.map()?, &scope.allowed)?);
out.push(hazards(&scope.allowed));
let (_, levels) = Checker::configured(config)?.shared_law(&scope.path)?;
out.extend(pedantic(&levels));
Ok(out.join("\n"))
}
fn intro(scope: &Scope) -> String {
let grants = scope.grants.join("\n");
format!(
"Write text that `ctrm check` accepts in {}. Use only the \
characters the sets below allow; any other character fails the \
check.\n\nAllowed:\n{grants}\n\nFidelity: {}.",
scope.label, scope.fidelity
)
}
fn one_path(
path: &str,
rules: &[Rule],
catalog: &SetCatalog,
) -> Result<Scope, String> {
let found = rules::resolve(path, rules, &rules::matches);
let names = found.sets.join("+");
let why = found.winner.map_or_else(no_rule, decided_by);
let allowed = catalog.resolve_union(&names, &found.sets, &found.family);
Ok(Scope {
path: path.to_owned(),
label: format!("`{path}`"),
grants: vec![format!("- {names}{why}")],
names: found.sets.clone(),
family: found.family.clone(),
fidelity: found.family.clone(),
allowed: allowed.map_err(|bad| bad.to_string())?,
})
}
fn no_rule() -> String {
String::from(" (no rule matches, so the default)")
}
fn decided_by(rule: &Rule) -> String {
match rule.origin {
Origin::File { .. } => {
format!(" (`{}` at {})", rule.pattern, describe(&rule.origin))
}
Origin::Argument { .. } | Origin::Builtin { .. } => {
format!(" (pattern `{}`)", rule.pattern)
}
}
}
fn whole(rules: &[Rule]) -> Scope {
let granting: Vec<&Rule> =
rules.iter().filter(|rule| !rule.sets.is_empty()).collect();
let mut grants = vec![format!("- a path no pattern matches: {ASCII}")];
grants.extend(granting.iter().map(|rule| grant_line(rule)));
grants.push(String::from("(the last matching pattern wins)"));
Scope {
path: String::new(),
names: named(&granting),
label: String::from("this repository"),
grants,
family: String::from(TEXT),
fidelity: fidelities(rules),
allowed: builtin::ascii(),
}
}
fn grant_line(rule: &Rule) -> String {
format!("- `{}`: {}", rule.pattern, granted(rule))
}
fn named(granting: &[&Rule]) -> Vec<String> {
let mut names = vec![String::from(ASCII)];
for set in granting.iter().flat_map(|rule| &rule.sets) {
if !names.contains(set) {
names.push(set.clone());
}
}
names
}
fn fidelities(rules: &[Rule]) -> String {
let named: Vec<String> = rules
.iter()
.filter_map(|rule| {
let family = rule.family.as_ref()?;
Some(format!("`{}` @{family}", rule.pattern))
})
.collect();
if named.is_empty() {
return String::from(TEXT);
}
format!("{TEXT}; by pattern, last match wins: {}", named.join(", "))
}
fn granted(rule: &Rule) -> String {
let named = rule.sets.iter().filter(|set| *set != ASCII);
let all: Vec<&str> = [ASCII]
.into_iter()
.chain(named.map(String::as_str))
.collect();
all.join("+")
}
#[cfg(test)]
#[path = "prompt_test.rs"]
mod tests;