Crate easy_shortcuts [−] [src]
Small programs which are meant to interoperate with other system commands need to quit early and return a non-zero exit code. Panicking comes across as unprofessional in such cases. This crate provides some useful shortcuts if you are writing programs that fit this 'fail hard and early' pattern.
Errors go through the
quit function. You can switch the behaviour back to panicking
with the EASY_DONT_QUIT_PANIC environment variable.
create wrap the usual
except that they quit instead of panicking. There are functions
write_all to read and write the contents of
text files, and
lines provides a straight iterator over all lines
in a readable.
paths provides an iterator over
pairs in a directory
or_die method is implemented for option and result types, allowing
a Perl-style equivalent to the usual
to_map are trivial specializations of
collect in the
usual case where you do want these containers.
convenient consumers of iterators which dump out
Building up strings is a common operation;
join is already defined on
vectors of strings, but here it's also defined on iterators of strings.
prepend collects strings with some text prepended to each value:
use easy_shortcuts::traits::*; (0..).take(5).print("\n"); (0..5).map(|n| (n,2*n)).debug("\n"); let strs = ["one","two","three"]; let s = strs.into_iter().prepend(" -L"); assert_eq!(s," -Lone -Ltwo -Lthree");
quit works like try, except it quits instead of returning.
The error message is output using
implements directory iterator over (path,metadata)
implements directory iterator over filenames
implements line iterator over a readable.
implements recursive directory iterator over (path,metadata)
recursive iterator over all entries in a directory.
Returns a tuple of (
get the nth argument or quit with a message.
get the nth command-line argument or return the default.
create a file for writing, quitting if not possible.
iterator over all files in a directory. Returns the files as strings; will quit if the directory does not exist or there is an i/o error)
iterate over all lines from a readable. The iterator is over String (will quit if there is an i/o error)
open a file for reading, quitting if there's any error.
open a buffered file for reading, quitting if there's any error.
iterator over all entries in a directory.
Returns a tuple of (
quit this program, printing a message and returning a non-zero exit code.
a form of
read the contents of a file as a string, quitting otherwise
execute a shell command, combining stdout and stderr, and return the result as a string
write a String to a new file, or quit