vtcode_core/tools/editing/patch/
error.rs1use std::path::PathBuf;
2
3use thiserror::Error;
4
5#[derive(Debug, Error)]
7pub enum PatchError {
8 #[error("cannot parse empty patch input")]
9 EmptyInput,
10
11 #[error("patch does not contain any operations")]
12 NoOperations,
13
14 #[error("invalid patch format: {0}")]
15 InvalidFormat(String),
16
17 #[error("invalid patch hunk on line {line}: {message}")]
18 InvalidHunk { line: usize, message: String },
19
20 #[error("invalid patch operation for '{path}': {reason}")]
21 InvalidOperation { path: String, reason: String },
22
23 #[error(
24 "patch chunks for '{path}' match overlapping file ranges. Fix: disambiguate the @@ context lines so each hunk targets a distinct range."
25 )]
26 OverlappingChunks { path: String },
27
28 #[error(
29 "patch target '{path}' already exists. Fix: use '*** Update File' to modify it, or choose a different path."
30 )]
31 TargetAlreadyExists { path: String },
32
33 #[error("cannot apply a text diff to directory '{path}'. Fix: point the hunk at a file instead.")]
34 NotAFile { path: String },
35
36 #[error(
37 "patch move destination '{path}' {reason}. Fix: choose a different '*** Move to:' path or remove the existing target."
38 )]
39 DestinationExists { path: String, reason: &'static str },
40
41 #[error("invalid path for {operation}: {path} ({reason})")]
42 InvalidPath {
43 operation: &'static str,
44 path: String,
45 reason: String,
46 },
47
48 #[error("file '{path}' not found for update")]
49 MissingFile { path: String },
50
51 #[error(
52 "failed to locate context '{context}' in '{path}'. Fix: Use read_file to get exact content, then copy the context lines precisely."
53 )]
54 ContextNotFound { path: String, context: String },
55
56 #[error(
57 "failed to locate expected lines in '{path}':\n{snippet}\n\nFix: Use complete current context/deletion lines; internal whitespace and full-line content must match. Use read_file first, then use those exact lines in your patch."
58 )]
59 SegmentNotFound { path: String, snippet: String },
60
61 #[error(
62 "patch context in '{path}' matches redacted source; repeated reads show the same redaction. Split the edit around the hidden line when possible. Editing the hidden line requires user assistance."
63 )]
64 RedactedContext { path: String },
65
66 #[error(
67 "semantic patch anchor '{anchor}' for '{path}' could not be resolved safely: {reason}\n\nFix: Use a more specific @@ anchor such as a function/class name, or copy exact context lines from read_file."
68 )]
69 SemanticResolutionFailed {
70 path: String,
71 anchor: String,
72 reason: String,
73 },
74
75 #[error(
76 "semantic patch anchor '{anchor}' for '{path}' matched {candidate_count} possible locations. Use a more specific @@ anchor or smaller hunk."
77 )]
78 SemanticAmbiguous {
79 path: String,
80 anchor: String,
81 candidate_count: usize,
82 },
83
84 #[error("I/O error while {action} '{path}': {source}")]
85 Io {
86 action: &'static str,
87 path: PathBuf,
88 #[source]
89 source: std::io::Error,
90 },
91
92 #[error("failed to generate temporary path for '{path}': {source}")]
93 TempPath {
94 path: PathBuf,
95 #[source]
96 source: std::time::SystemTimeError,
97 },
98
99 #[error("failed to rollback patch after error ({original}): {rollback}")]
100 Recovery {
101 original: Box<PatchError>,
102 #[source]
103 rollback: Box<PatchError>,
104 },
105
106 #[error("{original}")]
107 RolledBack {
108 #[source]
109 original: Box<PatchError>,
110 },
111}
112
113impl PatchError {
114 pub fn redacted_context_path(&self) -> Option<&str> {
116 match self {
117 Self::RedactedContext { path } => Some(path),
118 Self::RolledBack { original } => original.redacted_context_path(),
119 _ => None,
120 }
121 }
122
123 pub fn context_mismatch(&self) -> Option<(&str, &str)> {
125 match self {
126 Self::ContextNotFound { path, context } => Some((path, context)),
127 Self::SegmentNotFound { path, snippet } => Some((path, snippet)),
128 Self::RolledBack { original } => original.context_mismatch(),
129 _ => None,
131 }
132 }
133}