use std::fmt;
use techy::error::{DiagnosticInfo, DiagnosticValue, ToDiagnosticValue};
use techy::latexlike::Mode;
#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
#[diagnostic(
id = "flm.userdefs.recursion-limit",
message = "Recursion limit reached ({limit} levels) while expanding ‘{name}’"
)]
pub struct RecursionLimit {
pub name: String,
pub limit: usize,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PlaceholderProblem {
OutOfRange {
arity: usize,
},
UnknownName,
NoBody,
SubstitutionNotAccepted,
Unresolvable,
DefaultCycle,
}
impl PlaceholderProblem {
pub fn kind(&self) -> &'static str {
match self {
PlaceholderProblem::OutOfRange { .. } => "out-of-range",
PlaceholderProblem::UnknownName => "unknown-name",
PlaceholderProblem::NoBody => "no-body",
PlaceholderProblem::SubstitutionNotAccepted => "substitution-not-accepted",
PlaceholderProblem::Unresolvable => "unresolvable",
PlaceholderProblem::DefaultCycle => "default-cycle",
}
}
}
impl ToDiagnosticValue for PlaceholderProblem {
fn to_diagnostic_value(&self) -> DiagnosticValue {
let mut fields = vec![(String::from("kind"), self.kind().to_diagnostic_value())];
if let PlaceholderProblem::OutOfRange { arity } = self {
fields.push((String::from("arity"), arity.to_diagnostic_value()));
}
DiagnosticValue::Map(fields)
}
}
#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
#[diagnostic(id = "flm.userdefs.bad-placeholder")]
pub struct BadPlaceholder {
pub name: String,
pub reference: String,
pub reason: PlaceholderProblem,
}
impl fmt::Display for BadPlaceholder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let BadPlaceholder { name, reference, reason } = self;
match reason {
PlaceholderProblem::OutOfRange { arity: 0 } => write!(
f,
"Invalid argument number ‘#{reference}’: ‘{name}’ has no numbered arguments"
),
PlaceholderProblem::OutOfRange { arity: 1 } => write!(
f,
"Invalid argument number ‘#{reference}’: ‘{name}’ has only argument number ‘#1’"
),
PlaceholderProblem::OutOfRange { arity } => write!(
f,
"Invalid argument number ‘#{reference}’: ‘{name}’ has arguments ‘#1’..‘#{arity}’"
),
PlaceholderProblem::UnknownName => write!(
f,
"Invalid argument or placeholder name ‘#{reference}’ in ‘{name}’"
),
PlaceholderProblem::NoBody => write!(
f,
"Invalid placeholder ‘#{reference}’; ‘{name}’ has no body"
),
PlaceholderProblem::SubstitutionNotAccepted => write!(
f,
"Placeholder ‘#{reference}’ in definition of ‘{name}’ does not accept any \
bracketed modifiers (‘[…]’)"
),
PlaceholderProblem::Unresolvable => write!(
f,
"No value for argument or placeholder ‘#{reference}’ in the definition of ‘{name}’"
),
PlaceholderProblem::DefaultCycle => write!(
f,
"Cyclic recursion in default argument values for argument or placeholder \
‘#{reference}’ in the definition of ‘{name}’"
),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DefinitionProblem {
NoTemplate,
NotCompiled,
NotInForceInMode {
mode: Mode,
},
UnknownDefault {
key: String,
},
Minting {
detail: String,
},
ParseFailed {
detail: String,
},
TackOnPlaceholder {
reference: String,
},
}
impl DefinitionProblem {
pub fn kind(&self) -> &'static str {
match self {
DefinitionProblem::NoTemplate => "no-template",
DefinitionProblem::NotCompiled => "not-compiled",
DefinitionProblem::NotInForceInMode { .. } => "not-in-force-in-mode",
DefinitionProblem::UnknownDefault { .. } => "unknown-default",
DefinitionProblem::Minting { .. } => "minting",
DefinitionProblem::ParseFailed { .. } => "parse-failed",
DefinitionProblem::TackOnPlaceholder { .. } => "tack-on-placeholder",
}
}
}
impl ToDiagnosticValue for DefinitionProblem {
fn to_diagnostic_value(&self) -> DiagnosticValue {
let mut fields = vec![(String::from("kind"), self.kind().to_diagnostic_value())];
match self {
DefinitionProblem::UnknownDefault { key } => {
fields.push((String::from("key"), key.to_diagnostic_value()));
}
DefinitionProblem::Minting { detail } | DefinitionProblem::ParseFailed { detail } => {
fields.push((String::from("detail"), detail.to_diagnostic_value()));
}
DefinitionProblem::TackOnPlaceholder { reference } => {
fields.push((String::from("reference"), reference.to_diagnostic_value()));
}
DefinitionProblem::NotInForceInMode { mode } => {
fields.push((String::from("mode"), mode_token(*mode).to_diagnostic_value()));
}
DefinitionProblem::NoTemplate | DefinitionProblem::NotCompiled => {}
}
DiagnosticValue::Map(fields)
}
}
fn mode_token(mode: Mode) -> &'static str {
match mode {
Mode::Math => "math",
_ => "text",
}
}
fn mode_phrase(mode: Mode) -> &'static str {
match mode {
Mode::Math => "math mode",
_ => "text mode",
}
}
#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
#[diagnostic(id = "flm.userdefs.bad-definition")]
pub struct BadDefinition {
pub name: String,
pub problem: DefinitionProblem,
}
impl fmt::Display for BadDefinition {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let BadDefinition { name, problem } = self;
match problem {
DefinitionProblem::NoTemplate => {
write!(f, "Missing definition template for ‘{name}’")
}
DefinitionProblem::NotCompiled => write!(
f,
"Failed to compile definition of ‘{name}’"
),
DefinitionProblem::NotInForceInMode { mode } => write!(
f,
"Definition of ‘{name}’ fails to parse in {}",
mode_phrase(*mode)
),
DefinitionProblem::UnknownDefault { key } => write!(
f,
"Default value given for invalid argument ‘{key}’ in definition of ‘{name}’"
),
DefinitionProblem::Minting { detail } => write!(
f,
"Parsing the definition of ‘{name}’ failed (minting failed): {detail}"
),
DefinitionProblem::ParseFailed { detail } => write!(
f,
"Parsing the definition of ‘{name}’ failed: {detail}"
),
DefinitionProblem::TackOnPlaceholder { reference } => write!(
f,
"the definition of ‘{name}’ cannot be used: a template cannot refer to \
‘#{reference}’: that argument collects the commands written after a call, such \
as ‘\\label’"
),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
#[diagnostic(
id = "flm.userdefs.expansion-failed",
message = "Definition expansion pass failed: {detail}"
)]
pub struct ExpansionFailed {
pub detail: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_placeholder_problem_projects_as_a_token_with_its_payload() {
let problem = PlaceholderProblem::OutOfRange { arity: 2 };
assert_eq!(
problem.to_diagnostic_value(),
DiagnosticValue::Map(vec![
(String::from("kind"), DiagnosticValue::Str(String::from("out-of-range"))),
(String::from("arity"), DiagnosticValue::Int(2)),
])
);
}
#[test]
fn a_definition_problem_projects_as_a_token_with_its_payload() {
let problem = DefinitionProblem::UnknownDefault { key: String::from("zz") };
assert_eq!(
problem.to_diagnostic_value(),
DiagnosticValue::Map(vec![
(String::from("kind"), DiagnosticValue::Str(String::from("unknown-default"))),
(String::from("key"), DiagnosticValue::Str(String::from("zz"))),
])
);
}
}