Skip to main content

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 {
12        /// The path that could not be found.
13        path: Path<false>,
14    },
15    /// Mutation could not be performed at the specified path.
16    OperationError {
17        /// The path where the operation could not be performed.
18        path: Path<false>,
19    },
20    /// Error applying a truncate operation.
21    #[cfg(feature = "truncate")]
22    TruncateError {
23        /// The path where the truncation failed.
24        path: Path<false>,
25        /// The actual length of the value being truncated.
26        actual_len: usize,
27        /// The requested truncation length.
28        truncate_len: usize,
29    },
30}
31
32impl Display for MutationError {
33    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34        match self {
35            Self::IndexError { path } => {
36                write!(f, "path {path} does not exist or is malformed")
37            }
38            Self::OperationError { path } => {
39                write!(f, "operation could not be performed at {path}")
40            }
41            #[cfg(feature = "truncate")]
42            Self::TruncateError {
43                path,
44                actual_len,
45                truncate_len,
46            } => {
47                write!(
48                    f,
49                    "cannot truncate at {path}: actual length {actual_len} is less than truncate length {truncate_len}"
50                )
51            }
52        }
53    }
54}
55
56impl Error for MutationError {}