Skip to content
This repository was archived by the owner on Nov 24, 2023. It is now read-only.

Commit cb3229b

Browse files
committed
Expose an interface to apply fixes on-by-one
This commit exposes an interface that Cargo can use to apply fixes one-by-one and know which fixes failed and which succeeded.
1 parent ac3022e commit cb3229b

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

src/lib.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,22 +210,39 @@ pub fn collect_suggestions<S: ::std::hash::BuildHasher>(
210210
}
211211
}
212212

213-
pub fn apply_suggestions(code: &str, suggestions: &[Suggestion]) -> Result<String, Error> {
214-
use replace::Data;
213+
pub struct CodeFix {
214+
data: replace::Data,
215+
}
215216

216-
let mut fixed = Data::new(code.as_bytes());
217+
impl CodeFix {
218+
pub fn new(s: &str) -> CodeFix {
219+
CodeFix {
220+
data: replace::Data::new(s.as_bytes()),
221+
}
222+
}
217223

218-
for sug in suggestions.iter().rev() {
219-
for sol in &sug.solutions {
224+
pub fn apply(&mut self, suggestion: &Suggestion) -> Result<(), Error> {
225+
for sol in &suggestion.solutions {
220226
for r in &sol.replacements {
221-
fixed.replace_range(
227+
self.data.replace_range(
222228
r.snippet.range.start,
223229
r.snippet.range.end.saturating_sub(1),
224230
r.replacement.as_bytes(),
225231
)?;
226232
}
227233
}
234+
Ok(())
228235
}
229236

230-
Ok(String::from_utf8(fixed.to_vec())?)
237+
pub fn finish(&self) -> Result<String, Error> {
238+
Ok(String::from_utf8(self.data.to_vec())?)
239+
}
240+
}
241+
242+
pub fn apply_suggestions(code: &str, suggestions: &[Suggestion]) -> Result<String, Error> {
243+
let mut fix = CodeFix::new(code);
244+
for suggestion in suggestions.iter().rev() {
245+
fix.apply(suggestion)?;
246+
}
247+
fix.finish()
231248
}

0 commit comments

Comments
 (0)