apt_parser/
errors.rs

1use std::{
2	error::Error,
3	fmt::{Display, Formatter, Result},
4};
5
6#[derive(Debug)]
7pub struct KVError;
8
9impl Error for KVError {}
10
11impl Display for KVError {
12	fn fmt(&self, formatter: &mut Formatter<'_>) -> Result {
13		write!(formatter, "Failed to parse APT key-value data")
14	}
15}
16
17#[derive(Debug)]
18pub struct ParseError;
19
20impl Error for ParseError {}
21
22impl Display for ParseError {
23	fn fmt(&self, formatter: &mut Formatter<'_>) -> Result {
24		write!(formatter, "Failed to parse an APT value")
25	}
26}
27
28#[derive(Debug)]
29pub struct MissingKeyError {
30	pub key: String,
31	pub data: String,
32	details: String,
33}
34
35impl MissingKeyError {
36	pub fn new(key: &str, data: &str) -> MissingKeyError {
37		MissingKeyError {
38			key: key.to_owned(),
39			data: data.to_owned(),
40			details: format!("Failed to find key {0}", key),
41		}
42	}
43}
44
45impl Display for MissingKeyError {
46	fn fmt(&self, formatter: &mut Formatter<'_>) -> Result {
47		write!(formatter, "{}", self.details)
48	}
49}
50
51impl Error for MissingKeyError {
52	fn description(&self) -> &str {
53		&self.details
54	}
55}
56
57#[derive(Debug)]
58pub enum APTError {
59	KVError(KVError),
60	ParseError(ParseError),
61	MissingKeyError(MissingKeyError),
62}
63
64impl Error for APTError {}
65
66impl Display for APTError {
67	fn fmt(&self, formatter: &mut Formatter<'_>) -> Result {
68		match self {
69			APTError::KVError(err) => write!(formatter, "{}", err),
70			APTError::ParseError(err) => write!(formatter, "{}", err),
71			APTError::MissingKeyError(err) => write!(formatter, "{}", err),
72		}
73	}
74}
75
76impl From<ParseError> for APTError {
77	fn from(err: ParseError) -> APTError {
78		APTError::ParseError(err)
79	}
80}
81
82impl From<KVError> for APTError {
83	fn from(err: KVError) -> APTError {
84		APTError::KVError(err)
85	}
86}
87
88impl From<MissingKeyError> for APTError {
89	fn from(err: MissingKeyError) -> APTError {
90		APTError::MissingKeyError(err)
91	}
92}