Skip to main content

cerbero_lib/
error.rs

1use kerberos_asn1::KrbError;
2use kerberos_constants::error_codes;
3use std::{fmt, io, result};
4
5pub type Result<T> = result::Result<T, Error>;
6
7#[derive(Debug)]
8pub enum Error
9{
10	String(String),
11	KrbError(KrbError),
12
13	/// Errors due to IO, such as failures in network or file operations.
14	IOError(String, io::Error),
15
16	/// Errors related to handling of raw data, such as parsing, encrypting,
17	/// etc.
18	DataError(String),
19}
20
21impl std::error::Error for Error {}
22
23impl Error
24{
25	pub fn is_not_found_error(&self) -> bool
26	{
27		if let Error::IOError(_, ref io_err) = self
28		{
29			return io_err.kind() == io::ErrorKind::NotFound;
30		}
31		false
32	}
33
34	pub fn is_data_error(&self) -> bool
35	{
36		if let Error::DataError(_) = self
37		{
38			return true;
39		}
40		false
41	}
42}
43
44impl fmt::Display for Error
45{
46	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
47	{
48		match self
49		{
50			Error::String(s) => write!(f, "{}", s),
51			Error::DataError(s) => write!(f, "{}", s),
52			Error::KrbError(krb_error) =>
53			{
54				write!(f, "{}", create_krb_error_msg(krb_error))
55			},
56			Error::IOError(desc, io_error) =>
57			{
58				write!(f, "{}: {}", desc, io_error)
59			},
60		}
61	}
62}
63
64impl From<String> for Error
65{
66	fn from(error: String) -> Self
67	{
68		Self::String(error)
69	}
70}
71
72impl From<&str> for Error
73{
74	fn from(error: &str) -> Self
75	{
76		Self::String(error.to_string())
77	}
78}
79
80impl From<KrbError> for Error
81{
82	fn from(error: KrbError) -> Self
83	{
84		Self::KrbError(error)
85	}
86}
87
88impl From<(&str, io::Error)> for Error
89{
90	fn from(error: (&str, io::Error)) -> Self
91	{
92		Self::IOError(error.0.into(), error.1)
93	}
94}
95
96impl From<(String, io::Error)> for Error
97{
98	fn from(error: (String, io::Error)) -> Self
99	{
100		Self::IOError(error.0, error.1)
101	}
102}
103
104fn create_krb_error_msg(krb_error: &KrbError) -> String
105{
106	let error_string = error_codes::error_code_to_string(krb_error.error_code);
107	format!("Error {}: {}", krb_error.error_code, error_string)
108}