-
Notifications
You must be signed in to change notification settings - Fork 21
Allow cli/ tool to read data from stdin #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Avoid extra newlines, or additions like "Result: ", since they make the command less useful.
This is especially useful for binary data, which may not survive in an argument list (e.g. due to the presence of NUL bytes). The 'encode' and 'decode' commands are now exact inverses of each other, so data will roundtrip correctly, e.g. $ echo "hello world" | ./multibase encode | ./multibase decode hello world $ echo "hello world" | ./multibase encode | ./multibase decode | ./multibase encode z2yGEbwRFyhPZZckJm $ echo "hello world" | ./multibase encode z2yGEbwRFyhPZZckJm
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.
Thanks a lot for the PR, that's indeed a nice addition. Sorry for the long delay for the review. The change looks good as it is.
You mentioned that you're new to Rust. Hence I thought I leave a suggestion as I find code reviews a good way to learn from others. It's a matter of style and not about correctness. So feel free to ignore them.
let input_bytes = if let Some(s) = input { | ||
s.into_bytes() | ||
} else { | ||
let mut buf = Vec::new(); | ||
io::stdin().read_to_end(&mut buf)?; | ||
buf | ||
}; |
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 personally use if let
when there's no else (e.g. for early returns). When there's an else, I usually use match
.
let input_bytes = if let Some(s) = input { | |
s.into_bytes() | |
} else { | |
let mut buf = Vec::new(); | |
io::stdin().read_to_end(&mut buf)?; | |
buf | |
}; | |
let input_bytes = match input { | |
Some(s) => s.into_bytes(), | |
None => { | |
let mut buf = Vec::new(); | |
io::stdin().read_to_end(&mut buf)?; | |
buf | |
} | |
}; |
It's deeper nested, but I find it easier to read/reason about.
let input_str = if let Some(s) = input { | ||
s | ||
} else { | ||
let mut buf = String::new(); | ||
io::stdin().read_to_string(&mut buf)?; | ||
buf | ||
}; |
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.
Same reasons as for the comment above:
let input_str = if let Some(s) = input { | |
s | |
} else { | |
let mut buf = String::new(); | |
io::stdin().read_to_string(&mut buf)?; | |
buf | |
}; | |
let input_str = match input { | |
Some(s) => s, | |
None => { | |
let mut buf = String::new(); | |
io::stdin().read_to_string(&mut buf)?; | |
buf | |
} | |
}; |
The executable in
cli/
is a nice little conversion tool: much lighter than depending on Kubo, and supports more bases than other CLIs I've found. I'm not sure if anybody's actually using it for anything, but I'd like to; however, it does a couple of things which make it unsuitable for scripting. These commits scratch that itch, so I thought I'd upstream them. If it's not appropriate then feel free to ignore me :-)encode
, since binary data (which may contain NUL bytes, etc.) doesn't work well in argument lists; it forces shell scripts to use variables, which can't store it correctly; etc. In contrast, stdio is binary-safe, and requires no variables.Result:
prefix. This makes it less useful for programmatic use.With these changes, the
multibase
executable feels more like a member of coreutils (e.g.base64
), and is much easier to plug into other tools.PS: I'm not a Rust programmer, so might have made some mistakes. It would also be nice to stream the stdio conversion, rather than reading it all into memory; but this is a less invasive change, and fine for my current needs, at least.