aboutsummaryrefslogtreecommitdiff
path: root/src/error.rs
blob: d28a8dcc8f505e8532ad4aeb570b17adef2f85d2 (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use std::{
	env,
	fmt::{self, Display},
	io,
	str,
};

pub enum Error {
	BadArgs,
	BadLengths,
	DupInputElems(Vec<String>),
	DupOutputElems(Vec<String>),
	IOError(io::Error),
	Nop,
	UTF8Error(std::str::Utf8Error),
	SpawnFailed(String, io::Error),
	BadDecoding(String),
}

impl Display for Error {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		let p = env::args().next().unwrap();
		match self {
			Self::BadArgs => writeln!(f, "Usage: {p} [-0ei] [--] utility [argument ...]"),
			Self::BadLengths => writeln!(f, "{p}: Files have been added or removed during editing"),
			Self::DupInputElems(ds) => ds.iter().try_for_each(
				|d| writeln!(f, "{p}: Multiple input files named \"{}\" specified", d)
			),
			Self::DupOutputElems(ds) => ds.iter().try_for_each(
				|d| writeln!(f, "{p}: Multiple output files named \"{}\" specified", d)
			),
			Self::IOError(e) => writeln!(f, "{p}: {e}"),
			Self::Nop => Ok(()),
			Self::UTF8Error(e) => writeln!(f, "{p}: {e}"),
			Self::SpawnFailed(ed, e) => writeln!(f, "{p}: Failed to spawn editor \"{ed}\": {e}"),
			Self::BadDecoding(s) => writeln!(f, "{p}: Decoding the text {s:?} failed!"),
		}
	}
}

impl From<io::Error> for Error {
	fn from(e: io::Error) -> Self {
		Self::IOError(e)
	}	
}

impl From<str::Utf8Error> for Error {
	fn from(e: str::Utf8Error) -> Self {
		Self::UTF8Error(e)
	}
}