use std::{error::Error, fmt::Display};
#[derive(Debug)]
pub enum TableError {
MissingColumn {
line: usize,
},
InvalidField {
line: usize,
field: &'static str,
value: String,
},
SelfMapping {
line: usize,
ch: char,
},
DuplicateCandidate {
line: usize,
candidate: char,
owner: char,
},
DuplicateSource {
line: usize,
ch: char,
},
ChainedCandidate {
line: usize,
candidate: char,
},
}
impl Display for TableError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::MissingColumn { line } => write!(
f,
"line {line}: missing second column, expected two columns separated by a tab"
),
Self::InvalidField { line, field, value } => write!(
f,
"line {line}: {field} must be exactly one character, got {value:?}"
),
Self::SelfMapping { line, ch } => write!(
f,
"line {line}: candidate equals its source ({ch:?}), mapping would change nothing"
),
Self::DuplicateCandidate {
line,
candidate,
owner,
} => write!(
f,
"line {line}: candidate {candidate:?} is already claimed by source {owner:?}"
),
Self::DuplicateSource { line, ch } => {
write!(f, "line {line}: source {ch:?} is defined more than once")
}
Self::ChainedCandidate { line, candidate } => write!(
f,
"line {line}: candidate {candidate:?} is also a source, forming an ambiguous chain"
),
}
}
}
impl Error for TableError {}
#[derive(Debug)]
pub enum MarspeakError {
InvalidIntensity(f32),
}
impl Display for MarspeakError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::InvalidIntensity(intensity) => write!(f, "invalid intensity: {intensity}"),
}
}
}
impl Error for MarspeakError {}