// Copyright (c) 2021 Soni L. // // Licensed under the MIT license. // Documentation and comments licensed under CC BY-SA 4.0. use ::std::marker::PhantomData; use ::std::ops::Bound; use ::std::ops::RangeBounds; use ::iosonism::error::RangeError; use ::iosonism::error::ReadError; use ::iosonism::strcursor::StringReader; /// An error callback. pub trait ErrorFunc<'a, C: StringReader<'a>> { fn call<'b>(context: &C, ty: ErrorType<'b>); } /// An implementation of various Iosonism errors that calls T. pub struct ErrorCall(PhantomData); #[non_exhaustive] #[derive(PartialEq, Debug)] pub enum ErrorType<'a> { InvalidInteger(&'a str), ExpectedInteger, InvalidFloat(&'a str), ExpectedFloat, InvalidBool(&'a str), ExpectedBool, ExpectedStartOfQuote, ExpectedEndOfQuote, InvalidEscape(&'a str), ExpectedSymbol(&'a str), RangeErrori32(RangeErrorT), RangeErrori64(RangeErrorT), RangeErrorf32(RangeErrorT), RangeErrorf64(RangeErrorT), } #[derive(PartialEq, Debug)] pub struct RangeErrorT { pub value: T, pub start: Bound, pub end: Bound, } impl ::std::fmt::Display for ErrorCall { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { write!(f, "error!") } } impl ::std::fmt::Debug for ErrorCall { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { write!(f, "ErrorCall") } } impl ::std::error::Error for ErrorCall { } impl<'a, C, R, T> RangeError<'a, C, i32, R> for ErrorCall where C: StringReader<'a>, R: RangeBounds, T: ErrorFunc<'a, C> { fn value_not_in_range(context: &C, from: &i32, range: &R) -> Self { T::call(context, ErrorType::RangeErrori32(RangeErrorT { value: *from, start: range.start_bound().cloned(), end: range.end_bound().cloned(), })); Self(PhantomData) } } impl<'a, C, R, T> RangeError<'a, C, i64, R> for ErrorCall where C: StringReader<'a>, R: RangeBounds, T: ErrorFunc<'a, C> { fn value_not_in_range(context: &C, from: &i64, range: &R) -> Self { T::call(context, ErrorType::RangeErrori64(RangeErrorT { value: *from, start: range.start_bound().cloned(), end: range.end_bound().cloned(), })); Self(PhantomData) } } impl<'a, C, R, T> RangeError<'a, C, f32, R> for ErrorCall where C: StringReader<'a>, R: RangeBounds, T: ErrorFunc<'a, C> { fn value_not_in_range(context: &C, from: &f32, range: &R) -> Self { T::call(context, ErrorType::RangeErrorf32(RangeErrorT { value: *from, start: range.start_bound().cloned(), end: range.end_bound().cloned(), })); Self(PhantomData) } } impl<'a, C, R, T> RangeError<'a, C, f64, R> for ErrorCall where C: StringReader<'a>, R: RangeBounds, T: ErrorFunc<'a, C> { fn value_not_in_range(context: &C, from: &f64, range: &R) -> Self { T::call(context, ErrorType::RangeErrorf64(RangeErrorT { value: *from, start: range.start_bound().cloned(), end: range.end_bound().cloned(), })); Self(PhantomData) } } impl<'a, C: StringReader<'a>, T> ReadError<'a, C> for ErrorCall where T: ErrorFunc<'a, C> { fn invalid_integer(context: &C, from: &str) -> Self { T::call(context, ErrorType::InvalidInteger(from)); Self(PhantomData) } fn expected_integer(context: &C) -> Self { T::call(context, ErrorType::ExpectedInteger); Self(PhantomData) } fn invalid_float(context: &C, from: &str) -> Self { T::call(context, ErrorType::InvalidFloat(from)); Self(PhantomData) } fn expected_float(context: &C) -> Self { T::call(context, ErrorType::ExpectedFloat); Self(PhantomData) } fn invalid_bool(context: &C, from: &str) -> Self { T::call(context, ErrorType::InvalidBool(from)); Self(PhantomData) } fn expected_bool(context: &C) -> Self { T::call(context, ErrorType::ExpectedBool); Self(PhantomData) } fn expected_start_of_quote(context: &C) -> Self { T::call(context, ErrorType::ExpectedStartOfQuote); Self(PhantomData) } fn expected_end_of_quote(context: &C) -> Self { T::call(context, ErrorType::ExpectedEndOfQuote); Self(PhantomData) } fn invalid_escape(context: &C, from: &str) -> Self { T::call(context, ErrorType::InvalidEscape(from)); Self(PhantomData) } fn expected_symbol(context: &C, from: &str) -> Self { T::call(context, ErrorType::ExpectedSymbol(from)); Self(PhantomData) } }