rsql_rs/
error.rs

1use crate::{ParserResult, QueryType};
2use std::backtrace::Backtrace;
3use thiserror::Error;
4
5#[derive(Error, Debug)]
6pub enum ParserError {
7    #[error("Invalid Pair Rule found")]
8    InvalidPairRule(Backtrace),
9
10    #[error("Invalid Comparison found: {0}")]
11    InvalidComparison(String),
12    #[error("Invalid Query found: {0}")]
13    InvalidQuery(QueryType, Backtrace),
14
15    #[error("Invalid Constraint arguments: expect: {0}, found: {1}")]
16    InvalidConstraintArgs(String, usize),
17    #[error("Cannot find {field} when constructing {ty}")]
18    LackOfField { ty: String, field: String },
19
20    #[error("Unhandled Error: {0}")]
21    Unhandled(#[source] anyhow::Error),
22}
23
24impl ParserError {
25    pub fn invalid_pair_rule<T>() -> ParserResult<T> {
26        Err(ParserError::InvalidPairRule(Backtrace::capture()))
27    }
28}
29
30impl From<anyhow::Error> for ParserError {
31    fn from(err: anyhow::Error) -> Self {
32        match err.downcast::<ParserError>() {
33            Ok(par_err) => par_err,
34            Err(any_err) => ParserError::Unhandled(any_err),
35        }
36    }
37}
38
39impl From<pest::error::Error<crate::parser::fiql::Rule>> for ParserError {
40    fn from(err: pest::error::Error<crate::parser::fiql::Rule>) -> Self {
41        ParserError::Unhandled(anyhow::Error::from(err))
42    }
43}
44
45impl From<pest::error::Error<crate::parser::rsql::Rule>> for ParserError {
46    fn from(err: pest::error::Error<crate::parser::rsql::Rule>) -> Self {
47        ParserError::Unhandled(anyhow::Error::from(err))
48    }
49}