diff options
| author | Teddy Wing | 2016-04-22 17:20:27 -0400 |
|---|---|---|
| committer | Teddy Wing | 2016-04-22 17:20:27 -0400 |
| commit | 4535477b09ff1753cdc83e3c44585ad393a5c716 (patch) | |
| tree | 8c4f01407db538569ae964fe69e5086ad9c19ef3 | |
| parent | 7d71a6392836880d88bab27481b32e6b63b77c6e (diff) | |
| download | mutt-alias-auto-add-4535477b09ff1753cdc83e3c44585ad393a5c716.tar.bz2 | |
Write alias errors to STDERR instead of panicking
We use an `.ok()` call on the result of the write so that we can ignore
these errors. I think we shouldn't really worry too much about not being
to write to STDERR, and instead try to print out the full email message
as best we can.
In order to write the error (`e.to_string()`), we needed to implement
`fmt::Display` on `AliasSearchError`. While I was at it also implemented
the `error::Error` trait.
| -rw-r--r-- | src/main.rs | 32 |
1 files changed, 29 insertions, 3 deletions
diff --git a/src/main.rs b/src/main.rs index e5bab05..1be35a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,6 @@ +use std::error; use std::io::{self, BufRead, BufReader, Write}; +use std::fmt; use std::fs::{File, OpenOptions}; use std::path::Path; @@ -78,8 +80,32 @@ enum AliasSearchError { Io(io::Error), } -// impl fmt::Display for AliasSearchError {} -// impl error::Error for AliasSearchError {} +impl fmt::Display for AliasSearchError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + AliasSearchError::NotFound => write!(f, ""), + AliasSearchError::EmailExists => write!(f, ""), + AliasSearchError::Io(ref err) => write!(f, "IO error: {}", err), + } + } +} + +impl error::Error for AliasSearchError { + fn description(&self) -> &str { + match *self { + AliasSearchError::NotFound => "Alias could not be found in aliases file", + AliasSearchError::EmailExists => "Email already exists in aliases file", + AliasSearchError::Io(ref err) => err.description(), + } + } + + fn cause(&self) -> Option<&error::Error> { + match *self { + AliasSearchError::Io(ref err) => Some(err), + _ => None, + } + } +} impl From<io::Error> for AliasSearchError { fn from(err: io::Error) -> AliasSearchError { @@ -144,7 +170,7 @@ fn main() { match write_alias(line) { Ok(_) => continue, Err(AliasSearchError::NotFound) | Err(AliasSearchError::EmailExists) => continue, - Err(e) => panic!(e), + Err(e) => io::stderr().write(e.to_string().as_bytes()).ok(), }; } } |
