diff options
author | Thomas Voss <mail@thomasvoss.com> | 2022-11-04 11:32:07 +0100 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2022-11-04 11:37:07 +0100 |
commit | bb0f6f3a76d9e1099460fb2b3b7b41653d697898 (patch) | |
tree | d2d44ff8073bceacbbfb92814921f72fad29c035 /src/error.rs |
Initial commit
Diffstat (limited to 'src/error.rs')
-rw-r--r-- | src/error.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..06bfbbe --- /dev/null +++ b/src/error.rs @@ -0,0 +1,34 @@ +use std::{ + env, + fmt::{self, Display}, + io +}; + +pub enum Error { + BadLengths, + DuplicateElems(Vec<String>), + IOError(io::Error), + NoEditor, + SpawnFailed(String, io::Error), +} + +impl Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let p = env::args().next().unwrap(); + match self { + Self::BadLengths => writeln!(f, "{p}: Files have been added or removed during editing"), + Self::DuplicateElems(ds) => ds.iter().try_for_each( + |d| writeln!(f, "{p}: Multiple files named \"{}\" specified", d) + ), + Self::IOError(e) => writeln!(f, "{p}: {e}"), + Self::NoEditor => writeln!(f, "{p}: \"EDITOR\" environment variable is not set"), + Self::SpawnFailed(ed, e) => writeln!(f, "{p}: Failed to spawn editor \"{ed}\": {e}") + } + } +} + +impl From<io::Error> for Error { + fn from(e: io::Error) -> Self { + Self::IOError(e) + } +} |