Skip to main content

java_diff_utils_rs/patch/
patch_failed_exception.rs

1use std::error::Error;
2use std::fmt;
3
4use super::error::DiffError;
5
6/// Error type thrown whenever a delta or patch cannot be applied to a given sequence.
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct PatchFailedException {
9    message: String,
10}
11
12impl PatchFailedException {
13    /// Creates a new `PatchFailedException` with an empty error message.
14    pub fn new() -> Self {
15        Self {
16            message: String::new(),
17        }
18    }
19
20    /// Creates a new `PatchFailedException` with a descriptive message.
21    pub fn with_message(msg: impl Into<String>) -> Self {
22        Self {
23            message: msg.into(),
24        }
25    }
26
27    /// Returns the descriptive message associated with this exception.
28    #[inline]
29    pub fn message(&self) -> &str {
30        &self.message
31    }
32}
33
34impl Default for PatchFailedException {
35    fn default() -> Self {
36        Self::new()
37    }
38}
39
40impl fmt::Display for PatchFailedException {
41    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42        if self.message.is_empty() {
43            write!(f, "Patch failed to apply")
44        } else {
45            write!(f, "Patch failed: {}", self.message)
46        }
47    }
48}
49
50impl Error for PatchFailedException {}
51
52// Seamless coercion between PatchFailedException and base DiffError
53impl From<PatchFailedException> for DiffError {
54    fn from(err: PatchFailedException) -> Self {
55        DiffError::PatchFailed(err.message)
56    }
57}
58
59impl From<DiffError> for PatchFailedException {
60    fn from(err: DiffError) -> Self {
61        Self::with_message(err.to_string())
62    }
63}
64
65impl From<&str> for PatchFailedException {
66    fn from(msg: &str) -> Self {
67        Self::with_message(msg)
68    }
69}
70
71impl From<String> for PatchFailedException {
72    fn from(msg: String) -> Self {
73        Self::with_message(msg)
74    }
75}