use std::fmt;
use std::ops::Range;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SourceSpan {
start: usize,
end: usize,
}
impl SourceSpan {
pub(crate) const fn new(start: usize, end: usize) -> Self {
Self { start, end }
}
#[must_use]
pub const fn start(self) -> usize {
self.start
}
#[must_use]
pub const fn end(self) -> usize {
self.end
}
#[must_use]
pub fn as_range(self) -> Range<usize> {
self.start..self.end
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum LatexErrorKind {
Lexical,
Syntax,
Unsupported,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LatexError {
kind: LatexErrorKind,
span: SourceSpan,
message: String,
}
impl LatexError {
pub(crate) fn new(kind: LatexErrorKind, span: SourceSpan, message: impl Into<String>) -> Self {
Self {
kind,
span,
message: message.into(),
}
}
#[must_use]
pub const fn kind(&self) -> &LatexErrorKind {
&self.kind
}
#[must_use]
pub const fn span(&self) -> SourceSpan {
self.span
}
#[must_use]
pub fn message(&self) -> &str {
&self.message
}
}
impl fmt::Display for LatexError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.message)
}
}
impl std::error::Error for LatexError {}