use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParseError {
Empty,
InvalidUtf8 {
valid_up_to: usize,
},
MissingMsh {
found: String,
},
MalformedMsh {
reason: &'static str,
},
MalformedSegment {
line: usize,
text: String,
},
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ParseError::Empty => write!(f, "message is empty"),
ParseError::InvalidUtf8 { valid_up_to } => {
write!(
f,
"message is not valid UTF-8 (first bad byte at offset {valid_up_to})"
)
}
ParseError::MissingMsh { found } => {
write!(f, "message must start with MSH, found {found:?}")
}
ParseError::MalformedMsh { reason } => write!(f, "malformed MSH segment: {reason}"),
ParseError::MalformedSegment { line, text } => {
write!(f, "line {line}: not a segment: {text:?}")
}
}
}
}
impl std::error::Error for ParseError {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PathError {
Empty,
BadSegment(String),
BadIndex(String),
Trailing(String),
}
impl fmt::Display for PathError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PathError::Empty => write!(f, "path is empty"),
PathError::BadSegment(s) => write!(f, "invalid segment name {s:?}"),
PathError::BadIndex(s) => write!(f, "invalid index {s:?} (indices are 1-based)"),
PathError::Trailing(s) => write!(f, "unexpected trailing text {s:?}"),
}
}
}
impl std::error::Error for PathError {}