use std::error::Error;
use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseErrorSite {
line: String,
byte_start: usize,
byte_end: usize,
}
impl ParseErrorSite {
fn whole(line: &str) -> Self {
Self { line: line.to_owned(), byte_start: 0, byte_end: line.len() }
}
fn span(line: &str, byte_start: usize, byte_end: usize) -> Self {
let end = byte_end.min(line.len());
let start = byte_start.min(end);
Self { line: line.to_owned(), byte_start: start, byte_end: end }
}
#[must_use]
pub fn line(&self) -> &str {
&self.line
}
#[must_use]
pub const fn byte_start(&self) -> usize {
self.byte_start
}
#[must_use]
pub const fn byte_end(&self) -> usize {
self.byte_end
}
#[must_use]
pub fn fragment(&self) -> &str {
self.line.get(self.byte_start..self.byte_end).unwrap_or(&self.line)
}
}
impl fmt::Display for ParseErrorSite {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "`{}`", self.line)
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParseErrorKind {
UnknownCommand,
MissingArgument(&'static str),
UnexpectedArgument,
InvalidMove,
InvalidNumber(&'static str),
InvalidField(&'static str),
UnexpectedLine,
NonCanonical {
canonical: String,
},
}
impl fmt::Display for ParseErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnknownCommand => f.write_str("unknown command"),
Self::MissingArgument(name) => write!(f, "missing argument: {name}"),
Self::UnexpectedArgument => f.write_str("unexpected argument"),
Self::InvalidMove => f.write_str("invalid csa move"),
Self::InvalidNumber(name) => write!(f, "invalid number in field: {name}"),
Self::InvalidField(name) => write!(f, "invalid field: {name}"),
Self::UnexpectedLine => f.write_str("unexpected line in block"),
Self::NonCanonical { canonical } => {
write!(f, "line is not canonical, expected `{canonical}`")
}
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseError {
kind: ParseErrorKind,
site: ParseErrorSite,
}
impl ParseError {
pub(crate) fn new(kind: ParseErrorKind, line: &str) -> Self {
Self { kind, site: ParseErrorSite::whole(line) }
}
pub(crate) fn at(kind: ParseErrorKind, line: &str, byte_start: usize, byte_end: usize) -> Self {
Self { kind, site: ParseErrorSite::span(line, byte_start, byte_end) }
}
#[must_use]
pub const fn kind(&self) -> &ParseErrorKind {
&self.kind
}
#[must_use]
pub const fn site(&self) -> &ParseErrorSite {
&self.site
}
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.kind, self.site)
}
}
impl Error for ParseError {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn site_clamps_out_of_range_spans() {
let site = ParseErrorSite::span("AGREE", 3, 99);
assert_eq!(site.byte_start(), 3);
assert_eq!(site.byte_end(), 5);
assert_eq!(site.fragment(), "EE");
}
#[test]
fn site_clamps_inverted_spans() {
let site = ParseErrorSite::span("AGREE", 4, 1);
assert_eq!(site.byte_start(), 1);
assert_eq!(site.byte_end(), 1);
assert_eq!(site.fragment(), "");
}
#[test]
fn display_includes_kind_and_line() {
let error = ParseError::new(ParseErrorKind::UnknownCommand, "NOPE");
assert_eq!(error.to_string(), "unknown command: `NOPE`");
}
}