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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// SPDX-License-Identifier: MIT
// Copyright 2024 IROX Contributors
//

pub type Error = BitsError;
pub type ErrorKind = BitsErrorKind;

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct BitsError {
    kind: BitsErrorKind,
    msg: &'static str,
}

impl BitsError {
    pub fn new(kind: BitsErrorKind, msg: &'static str) -> Self {
        BitsError { kind, msg }
    }

    pub fn kind(&self) -> BitsErrorKind {
        self.kind
    }

    pub fn msg(&self) -> &'static str {
        self.msg
    }
}

impl From<BitsErrorKind> for BitsError {
    fn from(kind: BitsErrorKind) -> Self {
        BitsError {
            kind,
            msg: match kind {
                BitsErrorKind::InvalidData => "Invalid Data",
                BitsErrorKind::UnexpectedEof => "Unexpected EOF",
                BitsErrorKind::FormatError => "Unspecified Formatting Error",
                BitsErrorKind::OutOfMemory => "Out of Memory",

                BitsErrorKind::NotFound => "Not Found",
                BitsErrorKind::PermissionDenied => "Permission Denied",
                BitsErrorKind::ConnectionRefused => "Connection Refused",
                BitsErrorKind::ConnectionReset => "Connection Reset",
                BitsErrorKind::ConnectionAborted => "Connection Aborted",
                BitsErrorKind::AddrInUse => "Address In Use",
                BitsErrorKind::AddrNotAvailable => "Address Not Available",
                BitsErrorKind::BrokenPipe => "Broken Pipe",
                BitsErrorKind::AlreadyExists => "Already Exists",
                BitsErrorKind::WouldBlock => "Would Block",
                BitsErrorKind::InvalidInput => "Invalid Input",
                BitsErrorKind::TimedOut => "Timed Out",
                BitsErrorKind::WriteZero => "Write Zero",
                BitsErrorKind::Interrupted => "Interrupted",
                BitsErrorKind::NotConnected => "Not Connected",
                BitsErrorKind::Unsupported => "Unsupported",
                BitsErrorKind::Other => "Other",
            },
        }
    }
}

impl From<BitsError> for core::fmt::Error {
    fn from(_kind: BitsError) -> Self {
        core::fmt::Error
    }
}

impl From<core::fmt::Error> for BitsError {
    fn from(_value: core::fmt::Error) -> Self {
        BitsErrorKind::FormatError.into()
    }
}

#[cfg(feature = "std")]
impl From<std::io::Error> for BitsError {
    fn from(value: std::io::Error) -> Self {
        BitsError {
            kind: value.kind().into(),
            msg: "IO Error",
        }
    }
}

#[cfg(feature = "std")]
impl From<BitsError> for std::io::Error {
    fn from(value: BitsError) -> Self {
        Into::<std::io::ErrorKind>::into(value.kind).into()
    }
}

#[cfg(feature = "std")]
impl From<std::io::ErrorKind> for BitsErrorKind {
    fn from(value: std::io::ErrorKind) -> Self {
        use std::io::ErrorKind;
        match value {
            ErrorKind::NotFound => BitsErrorKind::NotFound,
            ErrorKind::PermissionDenied => BitsErrorKind::PermissionDenied,
            ErrorKind::ConnectionRefused => BitsErrorKind::ConnectionRefused,
            ErrorKind::ConnectionReset => BitsErrorKind::ConnectionReset,
            ErrorKind::ConnectionAborted => BitsErrorKind::ConnectionAborted,
            ErrorKind::NotConnected => BitsErrorKind::NotConnected,
            ErrorKind::AddrInUse => BitsErrorKind::AddrInUse,
            ErrorKind::AddrNotAvailable => BitsErrorKind::AddrNotAvailable,
            ErrorKind::BrokenPipe => BitsErrorKind::BrokenPipe,
            ErrorKind::AlreadyExists => BitsErrorKind::AlreadyExists,
            ErrorKind::WouldBlock => BitsErrorKind::WouldBlock,
            ErrorKind::InvalidInput => BitsErrorKind::InvalidInput,
            ErrorKind::InvalidData => BitsErrorKind::InvalidData,
            ErrorKind::TimedOut => BitsErrorKind::TimedOut,
            ErrorKind::WriteZero => BitsErrorKind::WriteZero,
            ErrorKind::Interrupted => BitsErrorKind::Interrupted,
            ErrorKind::Unsupported => BitsErrorKind::Unsupported,
            ErrorKind::UnexpectedEof => BitsErrorKind::UnexpectedEof,
            ErrorKind::OutOfMemory => BitsErrorKind::OutOfMemory,
            _ => BitsErrorKind::Other,
        }
    }
}

#[cfg(feature = "std")]
impl From<BitsErrorKind> for std::io::ErrorKind {
    fn from(value: BitsErrorKind) -> Self {
        use std::io::ErrorKind;
        match value {
            BitsErrorKind::InvalidData => ErrorKind::InvalidData,
            BitsErrorKind::UnexpectedEof => ErrorKind::UnexpectedEof,
            BitsErrorKind::OutOfMemory => ErrorKind::OutOfMemory,
            BitsErrorKind::NotFound => ErrorKind::NotFound,
            BitsErrorKind::PermissionDenied => ErrorKind::PermissionDenied,
            BitsErrorKind::ConnectionRefused => ErrorKind::ConnectionRefused,
            BitsErrorKind::ConnectionReset => ErrorKind::ConnectionReset,
            BitsErrorKind::ConnectionAborted => ErrorKind::ConnectionAborted,
            BitsErrorKind::AddrInUse => ErrorKind::AddrInUse,
            BitsErrorKind::AddrNotAvailable => ErrorKind::AddrNotAvailable,
            BitsErrorKind::BrokenPipe => ErrorKind::BrokenPipe,
            BitsErrorKind::AlreadyExists => ErrorKind::AlreadyExists,
            BitsErrorKind::WouldBlock => ErrorKind::WouldBlock,
            BitsErrorKind::InvalidInput => ErrorKind::InvalidInput,
            BitsErrorKind::TimedOut => ErrorKind::TimedOut,
            BitsErrorKind::WriteZero => ErrorKind::WriteZero,
            BitsErrorKind::Interrupted => ErrorKind::Interrupted,
            BitsErrorKind::Unsupported => ErrorKind::Unsupported,
            BitsErrorKind::NotConnected => ErrorKind::NotConnected,
            _ => ErrorKind::Other,
        }
    }
}

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum BitsErrorKind {
    InvalidData,
    UnexpectedEof,
    FormatError,
    OutOfMemory,
    NotFound,
    PermissionDenied,
    ConnectionRefused,
    ConnectionReset,
    ConnectionAborted,
    AddrInUse,
    AddrNotAvailable,
    BrokenPipe,
    AlreadyExists,
    WouldBlock,
    InvalidInput,
    TimedOut,
    WriteZero,
    Interrupted,
    NotConnected,
    Unsupported,
    Other,
}

#[cfg(feature = "std")]
impl std::error::Error for BitsError {}

impl core::fmt::Display for BitsError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "BitsError({:?}): {}", self.kind, self.msg)
    }
}