Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ctrlc = { version = "3.2", features = ["termination"] }
ignore = "0.4.18"
regex = "1.11.1"
rubyfmt = { path = "./librubyfmt" }
similar = "2.1.0"
similar = { version = "2.7.0", features = ["bytes"] }
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The update to 2.7.0 is just to keep us current with the latest version -- it doesn't have any functional differences as far as I can tell, just a bit of 🧹

The bytes feature is so that we can pass u8 Vecs to TextDiff::from_lines, which by default only handles strings.


[dev-dependencies]
assert_cmd = "2"
Expand Down
4 changes: 2 additions & 2 deletions librubyfmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub enum FormatError {
DiffDetected = 5,
}

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

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

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

pub fn toplevel_format_program_with_prism<W: Write>(
Expand Down
20 changes: 10 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ fn rubyfmt_string(
..
}: &CommandlineOpts,
buffer: &str,
) -> Result<Option<String>, rubyfmt::RichFormatError> {
) -> Result<Option<Vec<u8>>, rubyfmt::RichFormatError> {
if header_opt_in || header_opt_out {
// Only look at the first 500 bytes for the magic header.
// This is for performance
Expand Down Expand Up @@ -282,7 +282,7 @@ fn iterate_input_files(opts: &CommandlineOpts, f: &dyn Fn((&Path, &String))) {
if is_path_ignored(path, opts.include_gitignored) {
// Print unchanged output for ignored files unless we're in check mode
if !opts.check {
puts_stdout(&buffer);
puts_stdout(buffer.as_bytes());
}
return;
}
Expand Down Expand Up @@ -350,7 +350,7 @@ fn iterate_input_files(opts: &CommandlineOpts, f: &dyn Fn((&Path, &String))) {
}
}

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

fn iterate_formatted(opts: &CommandlineOpts, f: FormattingFunc) {
iterate_input_files(
Expand All @@ -365,9 +365,9 @@ fn iterate_formatted(opts: &CommandlineOpts, f: FormattingFunc) {
);
}

fn puts_stdout(input: &String) {
fn puts_stdout(input: &[u8]) {
io::stdout()
.write_all(input.as_bytes())
.write_all(input)
.expect("Could not write to stdout");
io::stdout().flush().expect("flush works");
}
Expand All @@ -392,7 +392,7 @@ fn main() {
&|(file_path, before)| match rubyfmt_string(&opts, before) {
Ok(None) => {}
Ok(Some(fmtted)) => {
let diff = TextDiff::from_lines(before, &fmtted);
let diff = TextDiff::from_lines(before.as_bytes(), &fmtted);
let path_string = file_path.to_str().unwrap();
text_diffs.lock().unwrap().push(format!(
"{}",
Expand All @@ -416,7 +416,7 @@ fn main() {

for diff in all_diffs.iter() {
if !diff.is_empty() {
puts_stdout(diff);
puts_stdout(diff.as_bytes());
diffs_reported += 1
}
}
Expand All @@ -434,12 +434,12 @@ fn main() {
iterate_formatted(&opts, &|(file_path, before, after)| match after {
None => {}
Some(fmtted) => {
if fmtted.ne(before) {
if fmtted.ne(before.as_bytes()) {
let file_write = OpenOptions::new()
.write(true)
.truncate(true)
.open(file_path)
.and_then(|mut file| file.write_all(fmtted.as_bytes()));
.and_then(|mut file| file.write_all(&fmtted));

match file_write {
Ok(_) => {}
Expand All @@ -455,7 +455,7 @@ fn main() {

_ => iterate_formatted(&opts, &|(_, before, after)| match after {
Some(fmtted) => puts_stdout(&fmtted),
None => puts_stdout(before),
None => puts_stdout(before.as_bytes()),
}),
}
}