Skip to main content

java_diff_utils_rs/patch/
error.rs

1use std::error::Error;
2use std::fmt;
3
4/// Base error type for all diff and patch operations in this library.
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub enum DiffError {
7    /// A general patch processing exception with a descriptive error message.
8    General(String),
9    /// Indicates that a patch application or verification failed.
10    PatchFailed(String),
11    /// Indicates that an unsupported or invalid operation was attempted.
12    UnsupportedOperation(String),
13}
14
15impl DiffError {
16    /// Constructs a general error with the given message.
17    pub fn new(msg: impl Into<String>) -> Self {
18        Self::General(msg.into())
19    }
20
21    /// Helper constructor for patch failure errors.
22    pub fn patch_failed(msg: impl Into<String>) -> Self {
23        Self::PatchFailed(msg.into())
24    }
25
26    /// Helper constructor for unsupported operation errors.
27    pub fn unsupported(msg: impl Into<String>) -> Self {
28        Self::UnsupportedOperation(msg.into())
29    }
30}
31
32impl fmt::Display for DiffError {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        match self {
35            Self::General(msg) => write!(f, "{msg}"),
36            Self::PatchFailed(msg) => write!(f, "Patch failed: {msg}"),
37            Self::UnsupportedOperation(msg) => write!(f, "Unsupported operation: {msg}"),
38        }
39    }
40}
41
42impl Error for DiffError {}
43
44impl From<String> for DiffError {
45    fn from(msg: String) -> Self {
46        Self::General(msg)
47    }
48}
49
50impl From<&str> for DiffError {
51    fn from(msg: &str) -> Self {
52        Self::General(msg.to_string())
53    }
54}
55
56/// Type aliases for module parity.
57pub type PatchError = DiffError;
58pub type PatchFailedException = DiffError;