rsonpath/input/
error.rs

1//! Error types for the [`input`](`crate::input`) module.
2
3use crate::error::InternalRsonpathError;
4use thiserror::Error;
5
6/// Errors raised when constructing [`Input`](super::Input) implementations.
7#[derive(Debug, Error)]
8pub enum InputError {
9    /// Error that occurs when an unbounded-sized implementation
10    /// (e.g. [`BufferedInput`](super::BufferedInput)) would allocate more than the global limit of [`isize::MAX`].
11    #[error("owned buffer size exceeded the hard system limit of isize::MAX")]
12    AllocationSizeExceeded,
13    /// Error when reading input from an underlying IO handle.
14    #[error(transparent)]
15    IoError(#[from] std::io::Error),
16    /// Irrecoverable error due to a broken invariant or assumption.
17    /// Preferred over panicking.
18    #[error("InputError: {0}")]
19    InternalError(
20        #[source]
21        #[from]
22        InternalRsonpathError,
23    ),
24}
25
26/// Hack to convert errors to [`InputError`] easily.
27///
28/// The bound on errors in [`Input`](super::Input) is [`Into<InputError>`].
29/// This doesn't work with the usual `?` Rust operator, as that requires the reverse
30/// bound (for [`InputError`] to be `From` the source). This is not easily expressible
31/// as a bound on [`Input`](super::Input). Instead we use this small function to perform
32/// the same conversion.
33pub(crate) trait InputErrorConvertible<T>: Sized {
34    /// Convert to [`InputError`] result.
35    ///
36    /// Instead of
37    /// ```rust,ignore
38    /// err.map_err(|x| x.into())?;
39    /// ```
40    /// you can write
41    /// ```rust,ignore
42    /// err.e()?;
43    /// ```
44    /// as a shorthand.
45    fn e(self) -> Result<T, InputError>;
46}
47
48impl<T, E: Into<InputError>> InputErrorConvertible<T> for Result<T, E> {
49    #[inline(always)]
50    fn e(self) -> Result<T, InputError> {
51        self.map_err(std::convert::Into::into)
52    }
53}
54
55/// Error type for [`Input`](`super::Input`) implementations that never fail
56/// when reading more input.
57#[derive(Debug, Error)]
58pub enum Infallible {}
59
60impl From<Infallible> for InputError {
61    #[inline(always)]
62    fn from(_value: Infallible) -> Self {
63        unreachable!()
64    }
65}