use alloc::string::String;
use core::fmt;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum RuleError {
UnknownKind {
name: String,
},
DuplicateNode {
name: String,
},
IndentTooWide {
columns: u8,
},
SeparatorIsDelimiter {
node: String,
},
TrailingNeedsDelimiters {
node: String,
},
}
impl fmt::Display for RuleError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnknownKind { name } => {
write!(f, "format rule names unknown kind `{name}`")
}
Self::DuplicateNode { name } => {
write!(f, "two format rules for node kind `{name}`; merge them")
}
Self::IndentTooWide { columns } => write!(
f,
"indentation step of {columns} columns is wider than the maximum of {}",
crate::MAX_INDENT_STEP
),
Self::SeparatorIsDelimiter { node } => write!(
f,
"the rule for `{node}` uses the same kind as separator and delimiter"
),
Self::TrailingNeedsDelimiters { node } => write!(
f,
"the rule for `{node}` sets a trailing-separator policy without delimiters"
),
}
}
}
impl core::error::Error for RuleError {}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum FormatError {
OutOfBounds {
start: u32,
end: u32,
len: usize,
},
NotCharBoundary {
offset: u32,
},
NotContiguous {
expected: u32,
found: u32,
},
}
impl fmt::Display for FormatError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::OutOfBounds { start, end, len } => write!(
f,
"token span {start}..{end} lies outside the {len}-byte source"
),
Self::NotCharBoundary { offset } => write!(
f,
"token boundary at byte {offset} is inside a multi-byte character"
),
Self::NotContiguous { expected, found } => write!(
f,
"tree is not lossless: a token starts at byte {found}, but the previous one ended at byte {expected}"
),
}
}
}
impl core::error::Error for FormatError {}