use crate::Val;
use alloc::string::{String, ToString};
use core::fmt;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
Parse(String),
Val(Val),
Char(isize),
Str(Val),
Arr(Val),
Length(Val),
Round(Val),
FromJson(Val, String),
Has(Val, Val),
Keys(Val),
Iter(Val),
Neg(Val),
MathOp(Val, jaq_syn::MathOp, Val),
Index(Val),
IndexWith(Val, Val),
IndexOutOfBounds(isize),
Int(Val),
Float(Val),
SliceAssign(Val),
PathExp,
Regex(String),
RegexFlag(char),
Custom(String),
}
impl Error {
pub fn as_val(self) -> Val {
match self {
Error::Val(ev) => ev,
_ => Val::str(self.to_string()),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match self {
Self::Parse(s) => s.fmt(f),
Self::Val(Val::Str(s)) => s.fmt(f),
Self::Val(v) => v.fmt(f),
Self::Char(i) => write!(f, "cannot use {i} as character"),
Self::Str(v) => write!(f, "cannot use {v} as string"),
Self::Arr(v) => write!(f, "cannot use {v} as array"),
Self::Length(v) => write!(f, "{v} has no length"),
Self::Round(v) => write!(f, "cannot round {v}"),
Self::FromJson(v, why) => write!(f, "cannot parse {v} as JSON: {why}"),
Self::Keys(v) => write!(f, "{v} has no keys"),
Self::Has(v, k) => write!(f, "cannot check whether {v} has key {k}"),
Self::Iter(v) => write!(f, "cannot iterate over {v}"),
Self::Neg(v) => write!(f, "cannot negate {v}"),
Self::MathOp(l, op, r) => write!(f, "cannot calculate {l} {op} {r}"),
Self::Index(v) => write!(f, "cannot index {v}"),
Self::IndexWith(v, i) => write!(f, "cannot index {v} with {i}"),
Self::IndexOutOfBounds(i) => write!(f, "index {i} is out of bounds"),
Self::Int(v) => write!(f, "cannot use {v} as integer"),
Self::Float(v) => write!(f, "cannot use {v} as float"),
Self::SliceAssign(v) => write!(f, "cannot assign non-array ({v}) to an array slice"),
Self::PathExp => write!(f, "invalid path expression"),
Self::Regex(e) => write!(f, "invalid regex: {e}"),
Self::RegexFlag(c) => write!(f, "invalid regex flag '{c}'"),
Self::Custom(e) => write!(f, "custom filter error: {e}"),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {}