Skip to content

Instantly share code, notes, and snippets.

View open_file.rs
fn render_docs(source: &Path, dest: &Path) -> Result<(), io::Error> {
let dir_list = try!(read_dir(source)
.map_err(|e| Error::Io(source.to_path_buf(), e)));
for entry_res in dir_list {
let entry = try!(entry_res
.map_err(|e| Error::Io(source.to_path_buf(), e)));
let path = entry.path();
let file = try!(File::open(&path)
.map_err(|e| Error::Io(path.clone(), e)));
for line_res in file.lines() {
View error_with_context.rs
enum DocsError {
Io(path: PathBuf, io::Error),
Format(path: PathBuf, lineno: usize, doc::FormatError),
}
View error_with_context.rs
enum DocsError {
Io(path: PathBuf, io::Error),
Format(path: PathBuf, lineno: usize, doc::FormatError),
}
View naive_error_type.rs
enum DocsError {
Io(io::Error),
Format(doc::FormatError),
}
View skeleton.rs
fn render_docs(source: &Path, &dest: &Path) -> Result<(), ErrorType> {
// ... read files at source
// ... write files to dest
Ok(())
}
fn main() {
// .. parse command line
match render_docs(&src, &dst) {
Ok(()) => {}
View render_io_error.rs
fn render_docs(source: &Path, dest: &Path) -> Result<(), io::Error> {
for entry_res in try!(read_dir(source)) {
let entry = try!(entry_res);
let file = try!(File::open(&entry.path());
for line_res in file.lines() {
let line = try!(line_res);
// ...
if wrong_syntax_flag {
return Err(io::Error::new(io::ErrorKind::Other,
"Wrong syntax"));
View context2.toml
[dependencies.quick-error]
git = "http://github.com/tailhook/quick-error"
branch = "context2"
View quick_error_context_io.rs
quick_error! {
#[derive(Debug)]
pub enum Error {
Io(err: io::Error, path: PathBuf) {
display("could not read file {:?}: {}", path, err)
context(path: &'a Path, err: io::Error)
-> (err, path.to_path_buf())
}
// ...
View context_impl.rs
pub struct Context<X, E>(pub X, pub E);
impl<T, E> ResultExt<T, E> for Result<T, E> {
fn context<X>(self, x: X) -> Result<T, Context<X, E>> {
self.map_err(|e| Context(x, e))
}
}
View result_ext.rs
pub trait ResultExt<T, E> {
fn context<X>(self, x: X) -> Result<T, Context<X, E>>;
}