Expand description
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.
Functions like open and create wrap the usual io::File methods,
except that they quit instead of panicking. There are functions
read_to_string and 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 (PathBuf,Metadata)
pairs in a directory
The or_die method is implemented for option and result types, allowing
a Perl-style equivalent to the usual expect.
to_vec and to_map are trivial specializations of collect in the
usual case where you do want these containers. print and debug are
convenient consumers of iterators which dump out Display or Debug values.
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:
§Examples
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");Modules§
Macros§
- quit
- quit works like try, except it quits instead of returning.
The error message is output using
quit_err.
Structs§
- DirIter
- implements directory iterator over (path,metadata)
created by
paths - File
Name Iter - implements directory iterator over filenames
- Line
Iter - implements line iterator over a readable.
Functions§
- argn_
err - get the nth argument or quit with a message.
- argn_or
- get the nth command-line argument or return the default.
- create
- create a file for writing, quitting if not possible.
- files
- 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)
- lines
- iterate over all lines from a readable. The iterator is over String (will quit if there is an i/o error)
- open
- open a file for reading, quitting if there’s any error.
- open_
buffered - open a buffered file for reading, quitting if there’s any error.
- paths
- iterator over all entries in a directory.
Returns a tuple of (
path::PathBuf,fs::Metadata); will quit if the directory does not exist or there is an i/o error) - quit
- quit this program, printing a message and returning a non-zero exit code.
- quit_
err - a form of
quitwhich works with the standardErrortype. - read_
to_ string - read the contents of a file as a string, quitting otherwise
- shell
- execute a shell command, combining stdout and stderr, and return the result as a string
- write_
all - write a String to a new file, or quit