use pest;
use crate::de::Rule;
use anyhow::{anyhow};
use std::fmt::{Display, Formatter, Debug};
pub type Result<T> = std::result::Result<T, MyError>;
#[derive(Debug)]
pub struct MyError{
pub message : String,
pub source : String,
pub start : Option<usize>,
pub end : Option<usize>,
pub e : anyhow::Error,
}
impl MyError{
pub fn new(s : String, start : usize) -> MyError{
MyError{ message : s.to_string(), source : s.to_string(), start : Some(start), end : None,
e : anyhow!("{}",s.to_string()) }
}
}
impl From<pest::error::Error<Rule>> for MyError {
fn from(err: pest::error::Error<Rule>) -> Self {
match err.location{
pest::error::InputLocation::Pos(start) =>{
MyError{ message : err.to_string(), source : err.to_string(), start : Some(start), end : None,
e : anyhow!("{}", err.to_string()) }
},
pest::error::InputLocation::Span((start, end)) =>{
MyError{ message : err.to_string(), source : err.to_string(), start : Some(start), end : Some(end),
e : anyhow!("{}", err.to_string())}
}
}
}
}
impl Display for MyError{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Debug::fmt(self, f)
}
}
impl std::error::Error for MyError{
}