1use std::{error::Error, fmt::Display};
2
3use error_stack::Report;
4
5#[derive(Debug, Clone, Copy)]
6pub enum IpconError {
7 InvalidName,
8 InvalidKevent,
9 InvalidLibIpconMsg,
10 InvalidData,
11 SysErrorTimeOut,
12 SysErrorInvalidValue,
13 SysErrorPermission,
14 SystemErrorNotExist,
15 SystemErrorOther,
16 Unexpected,
17}
18
19impl Display for IpconError {
20 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21 let err_str = match self {
22 IpconError::InvalidName => "Invalid name",
23 IpconError::InvalidKevent => "Invalid Kevent",
24 IpconError::InvalidData => "Invalid data",
25 IpconError::InvalidLibIpconMsg => "Invalid libipcon message",
26 IpconError::SysErrorTimeOut => "Timeout system error",
27 IpconError::SysErrorInvalidValue => "Invalid value system error",
28 IpconError::SystemErrorNotExist => "Entry (peer/group) not exist",
29 IpconError::SysErrorPermission => "Permission denied system error",
30 IpconError::SystemErrorOther => "Other system error",
31 _ => "Unexpected error",
32 };
33
34 write!(f, "{}", err_str)
35 }
36}
37
38impl From<Report<IpconError>> for IpconError {
39 fn from(report: Report<IpconError>) -> Self {
40 report.downcast_ref::<IpconError>().unwrap().to_owned()
41 }
42}
43
44impl From<IpconError> for std::io::Error {
45 fn from(e: IpconError) -> Self {
46 std::io::Error::new(std::io::ErrorKind::Other, e.to_string())
47 }
48}
49
50impl From<std::io::Error> for IpconError {
51 fn from(e: std::io::Error) -> Self {
52 match e.to_string().as_str() {
53 "Invalid name" => IpconError::InvalidName,
54 "Invalid Kevent" => IpconError::InvalidKevent,
55 "Invalid data" => IpconError::InvalidData,
56 "Invalid libipcon message" => IpconError::InvalidLibIpconMsg,
57 "Timeout system error" => IpconError::SysErrorTimeOut,
58 "Invalid value system error" => IpconError::SysErrorInvalidValue,
59 "Entry (peer/group) not exist" => IpconError::SystemErrorNotExist,
60 "Permission denied system error" => IpconError::SysErrorPermission,
61 "Other system error" => IpconError::SystemErrorOther,
62 _ => IpconError::Unexpected,
63 }
64 }
65}
66
67impl Error for IpconError {}