morphix/
error.rs

1use std::error::Error;
2use std::fmt::Display;
3
4use crate::Path;
5
6/// Error types for mutation operations.
7#[derive(Debug, Clone, PartialEq, Eq)]
8#[non_exhaustive]
9pub enum MutationError {
10    /// The specified path does not exist.
11    IndexError { path: Path<false> },
12    /// Mutation could not be performed at the specified path.
13    OperationError { path: Path<false> },
14    /// Error applying a truncate operation.
15    #[cfg(feature = "truncate")]
16    TruncateError {
17        path: Path<false>,
18        actual_len: usize,
19        truncate_len: usize,
20    },
21}
22
23impl Display for MutationError {
24    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25        match self {
26            Self::IndexError { path } => {
27                write!(f, "path {path} does not exist or is malformed")
28            }
29            Self::OperationError { path } => {
30                write!(f, "operation could not be performed at {path}")
31            }
32            Self::TruncateError {
33                path,
34                actual_len,
35                truncate_len,
36            } => {
37                write!(
38                    f,
39                    "cannot truncate at {path}: actual length {actual_len} is less than truncate length {truncate_len}"
40                )
41            }
42        }
43    }
44}
45
46impl Error for MutationError {}