Repository navigation
Expand file tree
/
Copy patherror.rs
More file actions
190 lines (158 loc) · 5.34 KB
/
Copy patherror.rs
File metadata and controls
190 lines (158 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//! Error types for patch operations.
use std::path::PathBuf;
use thiserror::Error;
/// Result type for patch operations.
pub type PatchResult<T> = Result<T, PatchError>;
/// Errors that can occur during patch parsing and application.
#[derive(Debug, Error)]
pub enum PatchError {
/// Failed to parse the patch format.
#[error("Failed to parse patch: {message}")]
ParseError {
message: String,
line_number: Option<usize>,
},
/// Invalid patch header.
#[error("Invalid patch header at line {line_number}: {message}")]
InvalidHeader { message: String, line_number: usize },
/// Invalid hunk header.
#[error("Invalid hunk header: {header}")]
InvalidHunkHeader { header: String },
/// File not found for patching.
#[error("File not found: {path}")]
FileNotFound { path: PathBuf },
/// Failed to read file.
#[error("Failed to read file {path}: {source}")]
ReadError {
path: PathBuf,
#[source]
source: std::io::Error,
},
/// Failed to write file.
#[error("Failed to write file {path}: {source}")]
WriteError {
path: PathBuf,
#[source]
source: std::io::Error,
},
/// Failed to create directory.
#[error("Failed to create directory {path}: {source}")]
CreateDirError {
path: PathBuf,
#[source]
source: std::io::Error,
},
/// Failed to delete file.
#[error("Failed to delete file {path}: {source}")]
DeleteError {
path: PathBuf,
#[source]
source: std::io::Error,
},
/// Hunk application failed - context mismatch.
#[error("Context mismatch at line {line}: expected '{expected}', found '{found}'")]
ContextMismatch {
line: usize,
expected: String,
found: String,
},
/// Hunk could not be located in the file.
#[error("Could not locate hunk starting at line {original_line} in file {file}")]
HunkNotFound { file: String, original_line: usize },
/// Conflict detected - the file has been modified.
#[error("Conflict detected in {file} at line {line}: {message}")]
Conflict {
file: String,
line: usize,
message: String,
},
/// Multiple hunks conflict with each other.
#[error("Overlapping hunks detected in {file}")]
OverlappingHunks { file: String },
/// Backup operation failed.
#[error("Backup failed for {path}: {message}")]
BackupError { path: PathBuf, message: String },
/// Restore operation failed.
#[error("Restore failed for {path}: {message}")]
RestoreError { path: PathBuf, message: String },
/// Empty patch provided.
#[error("Empty patch provided")]
EmptyPatch,
/// Invalid file path in patch.
#[error("Invalid file path: {path}")]
InvalidPath { path: String },
/// General I/O error.
#[error("I/O error: {0}")]
IoError(#[from] std::io::Error),
/// Multiple errors occurred.
#[error("Multiple errors occurred:\n{}", .0.iter().map(|e| format!(" - {e}")).collect::<Vec<_>>().join("\n"))]
MultipleErrors(Vec<PatchError>),
}
impl PatchError {
/// Create a parse error with optional line number.
pub fn parse(message: impl Into<String>, line_number: Option<usize>) -> Self {
Self::ParseError {
message: message.into(),
line_number,
}
}
/// Create a file not found error.
pub fn file_not_found(path: impl Into<PathBuf>) -> Self {
Self::FileNotFound { path: path.into() }
}
/// Create a context mismatch error.
pub fn context_mismatch(
line: usize,
expected: impl Into<String>,
found: impl Into<String>,
) -> Self {
Self::ContextMismatch {
line,
expected: expected.into(),
found: found.into(),
}
}
/// Create a hunk not found error.
pub fn hunk_not_found(file: impl Into<String>, original_line: usize) -> Self {
Self::HunkNotFound {
file: file.into(),
original_line,
}
}
/// Create a conflict error.
pub fn conflict(file: impl Into<String>, line: usize, message: impl Into<String>) -> Self {
Self::Conflict {
file: file.into(),
line,
message: message.into(),
}
}
/// Check if this is a recoverable error (e.g., context mismatch that might work with fuzzy matching).
pub fn is_recoverable(&self) -> bool {
matches!(
self,
Self::ContextMismatch { .. } | Self::HunkNotFound { .. }
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let err = PatchError::parse("unexpected token", Some(42));
assert!(err.to_string().contains("unexpected token"));
let err = PatchError::file_not_found("/some/path");
assert!(err.to_string().contains("/some/path"));
let err = PatchError::context_mismatch(10, "expected line", "actual line");
assert!(err.to_string().contains("expected line"));
assert!(err.to_string().contains("actual line"));
}
#[test]
fn test_is_recoverable() {
let recoverable = PatchError::context_mismatch(1, "a", "b");
assert!(recoverable.is_recoverable());
let not_recoverable = PatchError::file_not_found("/path");
assert!(!not_recoverable.is_recoverable());
}
}