Skip to content

Commit 95833f0

Browse files
authored
Make format_buffer return a Vec<u8> (#835)
* Have `format_buffer` return a `Vec<u8>` * Clippy
1 parent 6922c68 commit 95833f0

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

Cargo.lock

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ ctrlc = { version = "3.2", features = ["termination"] }
1717
ignore = "0.4.18"
1818
regex = "1.11.1"
1919
rubyfmt = { path = "./librubyfmt" }
20-
similar = "2.1.0"
20+
similar = { version = "2.7.0", features = ["bytes"] }
2121

2222
[dev-dependencies]
2323
assert_cmd = "2"

librubyfmt/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub enum FormatError {
6161
DiffDetected = 5,
6262
}
6363

64-
pub fn format_buffer(buf: &str) -> Result<String, RichFormatError> {
64+
pub fn format_buffer(buf: &str) -> Result<Vec<u8>, RichFormatError> {
6565
let out_data = vec![];
6666
let mut output = Cursor::new(out_data);
6767

@@ -80,7 +80,7 @@ pub fn format_buffer(buf: &str) -> Result<String, RichFormatError> {
8080
)?;
8181

8282
output.flush().expect("flushing to a vec should never fail");
83-
Ok(String::from_utf8(output.into_inner()).expect("we never write invalid UTF-8"))
83+
Ok(output.into_inner())
8484
}
8585

8686
pub fn toplevel_format_program_with_prism<W: Write>(

src/main.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ fn rubyfmt_string(
156156
..
157157
}: &CommandlineOpts,
158158
buffer: &str,
159-
) -> Result<Option<String>, rubyfmt::RichFormatError> {
159+
) -> Result<Option<Vec<u8>>, rubyfmt::RichFormatError> {
160160
if header_opt_in || header_opt_out {
161161
// Only look at the first 500 bytes for the magic header.
162162
// This is for performance
@@ -282,7 +282,7 @@ fn iterate_input_files(opts: &CommandlineOpts, f: &dyn Fn((&Path, &String))) {
282282
if is_path_ignored(path, opts.include_gitignored) {
283283
// Print unchanged output for ignored files unless we're in check mode
284284
if !opts.check {
285-
puts_stdout(&buffer);
285+
puts_stdout(buffer.as_bytes());
286286
}
287287
return;
288288
}
@@ -350,7 +350,7 @@ fn iterate_input_files(opts: &CommandlineOpts, f: &dyn Fn((&Path, &String))) {
350350
}
351351
}
352352

353-
type FormattingFunc<'a> = &'a dyn Fn((&Path, &String, Option<String>));
353+
type FormattingFunc<'a> = &'a dyn Fn((&Path, &String, Option<Vec<u8>>));
354354

355355
fn iterate_formatted(opts: &CommandlineOpts, f: FormattingFunc) {
356356
iterate_input_files(
@@ -365,9 +365,9 @@ fn iterate_formatted(opts: &CommandlineOpts, f: FormattingFunc) {
365365
);
366366
}
367367

368-
fn puts_stdout(input: &String) {
368+
fn puts_stdout(input: &[u8]) {
369369
io::stdout()
370-
.write_all(input.as_bytes())
370+
.write_all(input)
371371
.expect("Could not write to stdout");
372372
io::stdout().flush().expect("flush works");
373373
}
@@ -392,7 +392,7 @@ fn main() {
392392
&|(file_path, before)| match rubyfmt_string(&opts, before) {
393393
Ok(None) => {}
394394
Ok(Some(fmtted)) => {
395-
let diff = TextDiff::from_lines(before, &fmtted);
395+
let diff = TextDiff::from_lines(before.as_bytes(), &fmtted);
396396
let path_string = file_path.to_str().unwrap();
397397
text_diffs.lock().unwrap().push(format!(
398398
"{}",
@@ -416,7 +416,7 @@ fn main() {
416416

417417
for diff in all_diffs.iter() {
418418
if !diff.is_empty() {
419-
puts_stdout(diff);
419+
puts_stdout(diff.as_bytes());
420420
diffs_reported += 1
421421
}
422422
}
@@ -434,12 +434,12 @@ fn main() {
434434
iterate_formatted(&opts, &|(file_path, before, after)| match after {
435435
None => {}
436436
Some(fmtted) => {
437-
if fmtted.ne(before) {
437+
if fmtted.ne(before.as_bytes()) {
438438
let file_write = OpenOptions::new()
439439
.write(true)
440440
.truncate(true)
441441
.open(file_path)
442-
.and_then(|mut file| file.write_all(fmtted.as_bytes()));
442+
.and_then(|mut file| file.write_all(&fmtted));
443443

444444
match file_write {
445445
Ok(_) => {}
@@ -455,7 +455,7 @@ fn main() {
455455

456456
_ => iterate_formatted(&opts, &|(_, before, after)| match after {
457457
Some(fmtted) => puts_stdout(&fmtted),
458-
None => puts_stdout(before),
458+
None => puts_stdout(before.as_bytes()),
459459
}),
460460
}
461461
}

0 commit comments

Comments
 (0)