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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use std::{
	error::Error,
	fmt::{Display, Formatter, Result},
};

#[derive(Debug)]
pub struct KVError;

impl Error for KVError {}

impl Display for KVError {
	fn fmt(&self, formatter: &mut Formatter<'_>) -> Result {
		write!(formatter, "Failed to parse APT key-value data")
	}
}

#[derive(Debug)]
pub struct ParseError;

impl Error for ParseError {}

impl Display for ParseError {
	fn fmt(&self, formatter: &mut Formatter<'_>) -> Result {
		write!(formatter, "Failed to parse an APT value")
	}
}

#[derive(Debug)]
pub struct MissingKeyError {
	pub key: String,
	pub data: String,
	details: String,
}

impl MissingKeyError {
	pub fn new(key: &str, data: &str) -> MissingKeyError {
		MissingKeyError {
			key: key.to_owned(),
			data: data.to_owned(),
			details: format!("Failed to find key {0}", key),
		}
	}
}

impl Display for MissingKeyError {
	fn fmt(&self, formatter: &mut Formatter<'_>) -> Result {
		write!(formatter, "{}", self.details)
	}
}

impl Error for MissingKeyError {
	fn description(&self) -> &str {
		&self.details
	}
}

#[derive(Debug)]
pub enum APTError {
	KVError(KVError),
	ParseError(ParseError),
	MissingKeyError(MissingKeyError),
}

impl Error for APTError {}

impl Display for APTError {
	fn fmt(&self, formatter: &mut Formatter<'_>) -> Result {
		match self {
			APTError::KVError(err) => write!(formatter, "{}", err),
			APTError::ParseError(err) => write!(formatter, "{}", err),
			APTError::MissingKeyError(err) => write!(formatter, "{}", err),
		}
	}
}

impl From<ParseError> for APTError {
	fn from(err: ParseError) -> APTError {
		APTError::ParseError(err)
	}
}

impl From<KVError> for APTError {
	fn from(err: KVError) -> APTError {
		APTError::KVError(err)
	}
}

impl From<MissingKeyError> for APTError {
	fn from(err: MissingKeyError) -> APTError {
		APTError::MissingKeyError(err)
	}
}