aboutsummaryrefslogtreecommitdiff
path: root/src/error.rs
blob: 06bfbbe9d0b0a3cba1329e0c8a29b82256a2f236 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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)
	}	
}