#[derive(Debug, Clone)]
pub struct InconsistentEdn(pub(super) &'static str);
impl From<core::num::TryFromIntError> for InconsistentEdn {
fn from(_: core::num::TryFromIntError) -> Self {
InconsistentEdn("Numeric range exceeded")
}
}
impl core::fmt::Display for InconsistentEdn {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
f.write_str(self.0)
}
}
impl std::error::Error for InconsistentEdn {}
#[derive(Debug, Clone)]
pub struct ParseError(pub(super) peg::error::ParseError<peg::str::LineCol>);
impl ParseError {
pub fn line(&self) -> usize {
self.0.location.line
}
pub fn column(&self) -> usize {
self.0.location.column
}
pub fn offset(&self) -> usize {
self.0.location.offset
}
}
impl core::fmt::Display for ParseError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
writeln!(
f,
"Invalid EDN in line {} column {} (byte {}). Expected any of:",
self.line(),
self.column(),
self.offset()
)?;
for t in self.0.expected.tokens() {
writeln!(f, "* {}", t)?;
}
Ok(())
}
}
impl std::error::Error for ParseError {}
#[derive(Debug, Clone)]
pub struct TypeMismatch {
expected: &'static str,
}
impl TypeMismatch {
pub(crate) fn expecting(expected: &'static str) -> Self {
TypeMismatch { expected }
}
}
impl core::fmt::Display for TypeMismatch {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
write!(f, "Expected {}", self.expected)
}
}
impl std::error::Error for TypeMismatch {}
#[derive(Debug, Clone)]
pub struct CborError(CborErrorInner);
impl CborError {
pub(crate) fn out_of_data(text: &'static str) -> Self {
Self(CborErrorInner::OuOfData(text))
}
pub(crate) fn invalid(text: &'static str) -> Self {
Self(CborErrorInner::InvalidData(text))
}
pub(crate) fn is_out_of_data(&self) -> bool {
match self.0 {
CborErrorInner::OuOfData(_) => true,
CborErrorInner::InvalidData(_) => false,
}
}
}
#[derive(Debug, Clone)]
enum CborErrorInner {
OuOfData(&'static str),
InvalidData(&'static str),
}
impl core::fmt::Display for CborError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
match self.0 {
CborErrorInner::OuOfData(s) => write!(f, "Out of data: {s}"),
CborErrorInner::InvalidData(s) => write!(f, "Invalid data: {s}"),
}
}
}
impl std::error::Error for CborError {}