Add --stdout option / redirect error messages to stderr #79
Conversation
src/main.rs
Outdated
let in_file = args.value_of("in-file").unwrap(); | ||
let out_file = args.value_of("out-file").unwrap(); | ||
let in_file = args.value_of("in-file").unwrap(); | ||
let out_file = args.value_of("out-file").unwrap_or(""); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should use Option<String>
in this case. So .unwrap_or("")
is not needed.
src/main.rs
Outdated
|
||
if cli::get_flag(&args, Key::Quiet) { | ||
return; | ||
if !out_file.is_empty() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we switched to Option<String>
it will became:
if let Some(out_file) = out_file {
Looks good, but there are few problems:
As for code itself - I've added few comments. |
src/main.rs
Outdated
@@ -72,7 +73,7 @@ fn main() { | |||
|
|||
let on_err = || { | |||
// copy original file to destination | |||
if cli::get_flag(&args, Key::CopyOnError) { | |||
if out_file.is_empty() && cli::get_flag(&args, Key::CopyOnError) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there an error? It should be !out_file.is_empty()
?
-out_file.is_empty()
+out_file.is_some()
@RazrFalcon I believe I've add |
src/cleaner.rs
Outdated
@@ -207,6 +207,11 @@ pub fn write_buffer(doc: &Document, opt: &WriteOptions, buf: &mut Vec<u8>) { | |||
doc.write_buf_opt(opt, buf); | |||
} | |||
|
|||
pub fn write_stdout(data: &[u8]) -> Result<(), io::Error> { | |||
io::stdout().write(&data).unwrap(); | |||
Ok(()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should remove either Ok(())
or unwrap()
, because this code has no meaning.
src/main.rs
Outdated
if in_file != out_file { | ||
try_msg!(fs::copy(in_file, out_file)); | ||
if in_file != out_file.unwrap() { | ||
try_msg!(fs::copy(in_file, out_file.unwrap())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Too much unwrap
. You can use if let
syntax here too.
if let Some(out_file) = out_file {
if in_file != out_file {
try_msg!(fs::copy(in_file, out_file));
}
}
I've added two comments. You can fix this issues and squash commits into one. Then I will merge it. |
|
- Add -c flag and --stdout option as alias of it - print error messages to stderr
eef1fcd
to
4b7502f
Compare
@RazrFalcon fixed, and squashed commits into one (I didn't see your commit mixed in, sorry) |
Good. Thanks. |
Hi,
I added an option to take output to stdout. This is my first attempt to write Rust code other than hello world, so I believe it could be not-so-much-idiomatic ;) Please take your time to tell me if there's anything to fix!
Also, I redirected error messages to stderr to prevent error messages to be written to stdout. I needed this option because I wanted to replace svgo with svgcleaner, using stream interface in Node.