Skip to content

Instantly share code, notes, and snippets.

@hyone
Last active October 15, 2016 07:18
Show Gist options
  • Save hyone/af10f44c18082cda8eff78d40ca32f55 to your computer and use it in GitHub Desktop.
Save hyone/af10f44c18082cda8eff78d40ca32f55 to your computer and use it in GitHub Desktop.
open file with std::error::Error
use std::error::Error as StdError;
use std::env;
use std::fmt;
use std::fs::File;
use std::io::Error as ioError;
use std::io::prelude::*;
use std::path::Path;
macro_rules! eprintln {
($($tt:tt)*) => {{
use std::io::Write;
let _ = writeln!(&mut ::std::io::stderr(), $($tt)*);
}}
}
type Result<'a, T> = std::result::Result<T, Box<StdError + 'a>>;
#[derive(Debug)]
enum Error<'a> {
OpenError(&'a Path, ioError),
ReadError(&'a Path, ioError),
}
impl <'a> StdError for Error<'a> {
fn description(&self) -> &str {
match *self {
Error::OpenError(_, _) => "OpenError",
Error::ReadError(_, _) => "ReadError",
}
}
fn cause(&self) -> Option<&StdError> {
match *self {
Error::OpenError(_, ref e) => Some(e),
Error::ReadError(_, ref e) => Some(e)
}
}
}
impl <'a> fmt::Display for Error<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::OpenError(path, ref e) =>
write!(f, "Couldn't open {}: {}", path.display(), e.description()),
Error::ReadError(path, ref e) =>
write!(f, "Couldn't read {}: {}", path.display(), e.description()),
}
}
}
fn read_content(path: &Path) -> Result<String> {
let mut file = try!(
File::open(path).or_else(|e| Err(Error::OpenError(path, e)))
);
let mut s = String::new();
try!(
file.read_to_string(&mut s).map_err(|e| Error::ReadError(path, e))
);
Ok(s)
}
fn help(command: &str) -> String {
format!("{} <filename>", command)
}
fn main() {
let argv: Vec<String> = env::args().collect();
if let Some(filename) = argv.get(1) {
let path = Path::new(&filename);
match read_content(&path) {
Err(e) => eprintln!("[Error]: {}", e),
Ok(s) => print!("[{}] contains:\n{}", path.display(), s),
}
} else {
println!("{}", help(&argv[0]));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment