1use std::error::Error;
2use std::fmt::Display;
3
4use crate::Path;
5
6#[derive(Debug, Clone, PartialEq, Eq)]
8#[non_exhaustive]
9pub enum MutationError {
10 IndexError {
12 path: Path<false>,
14 },
15 OperationError {
17 path: Path<false>,
19 },
20 #[cfg(feature = "truncate")]
22 TruncateError {
23 path: Path<false>,
25 actual_len: usize,
27 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 {}