Skip to content

Commit c738b3e

Browse files
committed
make to_string a proper method of replace::Data
1 parent 84d83ca commit c738b3e

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

crates/rustfix/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ impl<'orig> CodeFix<'orig> {
253253

254254
/// Gets the result of the "fixed" code.
255255
pub fn finish(&self) -> Result<String, Error> {
256-
Ok(String::from_utf8(self.data.to_vec())?)
256+
self.data.to_string()
257257
}
258258

259259
/// Returns whether or not the data has been modified.

crates/rustfix/src/replace.rs

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
//! which will validate that the changes do not conflict with one another.
88
//! At any time, you can "checkpoint" the current changes with [`Data::commit`]
99
//! or roll them back (perhaps due to a conflict) with [`Data::restore`].
10-
//! When you're done, use [`Data::to_vec`]
10+
//! When you're done, use [`Data::to_vec`] or [`Data::to_string`]
1111
//! to merge the original data with the changes.
1212
//!
1313
//! # Notes
1414
//!
15-
//! The [`Data::to_vec`] method includes uncommitted changes, if present.
15+
//! The [`Data::to_vec`] and [`Data::to_string`] methods include uncommitted changes, if present.
1616
//! The reason for including uncommitted changes is that typically, once you're calling those,
1717
//! you're done with edits and will be dropping the [`Data`] struct in a moment.
1818
//! In this case, requiring an extra call to `commit` would be unnecessary work.
@@ -138,6 +138,14 @@ impl<'orig> Data<'orig> {
138138
s
139139
}
140140

141+
/// Merge the original data with changes, **including** uncommitted changes,
142+
/// and validate that the result is a valid UTF-8 string.
143+
///
144+
/// See the module-level documentation for more information on why uncommitted changes are included.
145+
pub fn to_string(&self) -> Result<String, Error> {
146+
Ok(String::from_utf8(self.to_vec())?)
147+
}
148+
141149
/// Record a provisional change.
142150
///
143151
/// If committed, the original data in the given `range` will be replaced by the given data.
@@ -198,61 +206,57 @@ mod tests {
198206
use super::*;
199207
use proptest::prelude::*;
200208

201-
fn str(i: &[u8]) -> &str {
202-
::std::str::from_utf8(i).unwrap()
203-
}
204-
205209
#[test]
206210
fn insert_at_beginning() {
207211
let mut d = Data::new("foo bar baz");
208212
d.replace_range(0..0, "oh no ").unwrap();
209-
assert_eq!("oh no foo bar baz", str(&d.to_vec()));
213+
assert_eq!("oh no foo bar baz", &d.to_string().unwrap());
210214
}
211215

212216
#[test]
213217
fn insert_at_end() {
214218
let mut d = Data::new("foo bar baz");
215219
d.replace_range(11..11, " oh no").unwrap();
216-
assert_eq!("foo bar baz oh no", str(&d.to_vec()));
220+
assert_eq!("foo bar baz oh no", &d.to_string().unwrap());
217221
}
218222

219223
#[test]
220224
fn replace_some_stuff() {
221225
let mut d = Data::new("foo bar baz");
222226
d.replace_range(4..7, "lol").unwrap();
223-
assert_eq!("foo lol baz", str(&d.to_vec()));
227+
assert_eq!("foo lol baz", &d.to_string().unwrap());
224228
}
225229

226230
#[test]
227231
fn replace_a_single_char() {
228232
let mut d = Data::new("let y = true;");
229233
d.replace_range(4..5, "mut y").unwrap();
230-
assert_eq!("let mut y = true;", str(&d.to_vec()));
234+
assert_eq!("let mut y = true;", &d.to_string().unwrap());
231235
}
232236

233237
#[test]
234238
fn replace_multiple_lines() {
235239
let mut d = Data::new("lorem\nipsum\ndolor");
236240

237241
d.replace_range(6..11, "lol").unwrap();
238-
assert_eq!("lorem\nlol\ndolor", str(&d.to_vec()));
242+
assert_eq!("lorem\nlol\ndolor", &d.to_string().unwrap());
239243

240244
d.replace_range(12..17, "lol").unwrap();
241-
assert_eq!("lorem\nlol\nlol", str(&d.to_vec()));
245+
assert_eq!("lorem\nlol\nlol", &d.to_string().unwrap());
242246
}
243247

244248
#[test]
245249
fn replace_multiple_lines_with_insert_only() {
246250
let mut d = Data::new("foo!");
247251

248252
d.replace_range(3..3, "bar").unwrap();
249-
assert_eq!("foobar!", str(&d.to_vec()));
253+
assert_eq!("foobar!", &d.to_string().unwrap());
250254

251255
d.replace_range(0..3, "baz").unwrap();
252-
assert_eq!("bazbar!", str(&d.to_vec()));
256+
assert_eq!("bazbar!", &d.to_string().unwrap());
253257

254258
d.replace_range(3..4, "?").unwrap();
255-
assert_eq!("bazbar?", str(&d.to_vec()));
259+
assert_eq!("bazbar?", &d.to_string().unwrap());
256260
}
257261

258262
#[test]
@@ -264,17 +268,17 @@ mod tests {
264268
}
265269

266270
#[test]
267-
fn empty_to_vec_roundtrip() {
271+
fn empty_to_string_roundtrip() {
268272
let s = "";
269-
assert_eq!(s.as_bytes(), Data::new(s.as_bytes()).to_vec().as_slice());
273+
assert_eq!(s, &Data::new(s).to_string().unwrap());
270274
}
271275

272276
#[test]
273277
fn replace_same_range_diff_data() {
274278
let mut d = Data::new("foo bar baz");
275279

276280
d.replace_range(4..7, "lol").unwrap();
277-
assert_eq!("foo lol baz", str(&d.to_vec()));
281+
assert_eq!("foo lol baz", &d.to_string().unwrap());
278282

279283
assert!(matches!(
280284
d.replace_range(4..7, "lol2").unwrap_err(),
@@ -290,7 +294,7 @@ mod tests {
290294
let mut d = Data::new("foo bar baz");
291295

292296
d.replace_range(4..7, "lol").unwrap();
293-
assert_eq!("foo lol baz", str(&d.to_vec()));
297+
assert_eq!("foo lol baz", &d.to_string().unwrap());
294298

295299
assert!(matches!(
296300
d.replace_range(4..7, "lol").unwrap_err(),
@@ -314,50 +318,50 @@ mod tests {
314318
fn insert_same_twice() {
315319
let mut d = Data::new("foo");
316320
d.replace_range(1..1, "b").unwrap();
317-
assert_eq!("fboo", str(&d.to_vec()));
321+
assert_eq!("fboo", &d.to_string().unwrap());
318322
assert!(matches!(
319323
d.replace_range(1..1, "b").unwrap_err(),
320324
Error::AlreadyReplaced {
321325
is_identical: true,
322326
..
323327
},
324328
));
325-
assert_eq!("fboo", str(&d.to_vec()));
329+
assert_eq!("fboo", &d.to_string().unwrap());
326330
}
327331

328332
#[test]
329333
fn commit_restore() {
330334
let mut d = Data::new(", ");
331-
assert_eq!(", ", str(&d.to_vec()));
335+
assert_eq!(", ", &d.to_string().unwrap());
332336

333337
d.replace_range(2..2, "world").unwrap();
334338
d.replace_range(0..0, "hello").unwrap();
335-
assert_eq!("hello, world", str(&d.to_vec()));
339+
assert_eq!("hello, world", &d.to_string().unwrap());
336340

337341
d.restore();
338-
assert_eq!(", ", str(&d.to_vec()));
342+
assert_eq!(", ", &d.to_string().unwrap());
339343

340344
d.commit();
341-
assert_eq!(", ", str(&d.to_vec()));
345+
assert_eq!(", ", &d.to_string().unwrap());
342346

343347
d.replace_range(2..2, "world").unwrap();
344-
assert_eq!(", world", str(&d.to_vec()));
348+
assert_eq!(", world", &d.to_string().unwrap());
345349
d.commit();
346-
assert_eq!(", world", str(&d.to_vec()));
350+
assert_eq!(", world", &d.to_string().unwrap());
347351
d.restore();
348-
assert_eq!(", world", str(&d.to_vec()));
352+
assert_eq!(", world", &d.to_string().unwrap());
349353

350354
d.replace_range(0..0, "hello").unwrap();
351-
assert_eq!("hello, world", str(&d.to_vec()));
355+
assert_eq!("hello, world", &d.to_string().unwrap());
352356
d.commit();
353-
assert_eq!("hello, world", str(&d.to_vec()));
357+
assert_eq!("hello, world", &d.to_string().unwrap());
354358
d.restore();
355-
assert_eq!("hello, world", str(&d.to_vec()));
359+
assert_eq!("hello, world", &d.to_string().unwrap());
356360
}
357361

358362
proptest! {
359363
#[test]
360-
fn new_to_vec_roundtrip(ref s in "\\PC*") {
364+
fn new_to_string_roundtrip(ref s in "\\PC*") {
361365
assert_eq!(s.as_bytes(), &Data::new(s).to_vec());
362366
}
363367

0 commit comments

Comments
 (0)