use std::{io, path::PathBuf};
use crate::span::Span;
#[derive(Debug, thiserror::Error)]
pub enum ParseError {
#[error("I/O error: {0}")]
Io(#[from] io::Error),
#[error("{inner}")]
Parse {
file: Option<PathBuf>,
#[source]
inner: DirectiveError,
},
#[error("Maximum include level reached at {file}:{line}")]
IncludeLevelExceeded { file: PathBuf, line: usize },
#[error("Line too long ({len} bytes) at {}:{line}", file.as_deref().map_or("?", |p| p.to_str().unwrap_or("?")))]
LineTooLong {
file: Option<PathBuf>,
line: usize,
len: usize,
},
}
impl PartialEq for ParseError {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Io(a), Self::Io(b)) => a.kind() == b.kind(),
(
Self::Parse {
file: f1,
inner: i1,
},
Self::Parse {
file: f2,
inner: i2,
},
) => f1 == f2 && i1 == i2,
(
Self::IncludeLevelExceeded {
file: f1,
line: l1,
},
Self::IncludeLevelExceeded {
file: f2,
line: l2,
},
) => f1 == f2 && l1 == l2,
(
Self::LineTooLong {
file: f1,
line: l1,
len: n1,
},
Self::LineTooLong {
file: f2,
line: l2,
len: n2,
},
) => f1 == f2 && l1 == l2 && n1 == n2,
_ => false,
}
}
}
#[derive(Debug, thiserror::Error, PartialEq)]
pub enum DirectiveError {
#[error("Invalid directive `{name}` at {span}")]
InvalidDirective { name: String, span: Span },
#[error("Invalid option `{option}` in `{directive}` at {span}")]
InvalidOption {
directive: String,
option: String,
span: Span,
},
#[error("Invalid value in `{directive}`: expected {expected}, got `{got}` at {span}")]
InvalidValue {
directive: String,
expected: &'static str,
got: String,
span: Span,
},
#[error("Value `{value}` out of range (min {min}, max {max}) in `{directive}` at {span}")]
ValueOutOfRange {
directive: String,
value: String,
min: String,
max: String,
span: Span,
},
#[error("Missing arguments in `{directive}`: expected {expected}, got {got} at {span}")]
MissingArgument {
directive: String,
expected: usize,
got: usize,
span: Span,
},
#[error("Too many arguments in `{directive}`: expected {expected}, got {got} at {span}")]
TooManyArguments {
directive: String,
expected: usize,
got: usize,
span: Span,
},
}
#[derive(Debug, thiserror::Error, PartialEq)]
pub enum ValueError {
#[error("Value {value} out of range ({min}..{max})")]
OutOfRange { value: i64, min: i64, max: i64 },
#[error("Invalid format `{value}`: expected {expected}")]
InvalidFormat { value: String, expected: &'static str },
#[error("Invalid keyword `{value}`, expected one of: {}", valid.join(", "))]
InvalidKeyword {
value: String,
valid: &'static [&'static str],
},
}