wintun_bindings/
error.rs

1/// Error type used to convey that a value is outside of a range that it must fall inside
2#[derive(Debug)]
3pub struct OutOfRangeData<T> {
4    pub range: std::ops::RangeInclusive<T>,
5    pub value: T,
6}
7
8/// Error type returned when preconditions of this API are broken
9#[derive(thiserror::Error, Debug)]
10#[non_exhaustive]
11pub enum Error {
12    #[error(transparent)]
13    Io(#[from] std::io::Error),
14
15    #[error("CapacityNotPowerOfTwo {0}")]
16    CapacityNotPowerOfTwo(u32),
17
18    #[error("CapacityOutOfRange {0:?}")]
19    CapacityOutOfRange(OutOfRangeData<u32>),
20
21    #[error("{0}")]
22    String(String),
23
24    #[error(transparent)]
25    LibLoading(#[from] libloading::Error),
26
27    #[error(transparent)]
28    FromUtf16Error(#[from] std::string::FromUtf16Error),
29
30    #[error(transparent)]
31    Utf8Error(#[from] std::str::Utf8Error),
32
33    #[error(transparent)]
34    FromUtf8Error(#[from] std::string::FromUtf8Error),
35
36    #[error(transparent)]
37    AddrParseError(#[from] std::net::AddrParseError),
38
39    #[error(transparent)]
40    SystemTimeError(#[from] std::time::SystemTimeError),
41
42    #[error(transparent)]
43    TryFromSliceError(#[from] std::array::TryFromSliceError),
44
45    #[error(transparent)]
46    Infallible(#[from] std::convert::Infallible),
47
48    #[error("Session shutting down")]
49    ShuttingDown,
50}
51
52impl From<String> for Error {
53    fn from(value: String) -> Self {
54        Error::String(value)
55    }
56}
57
58impl From<&String> for Error {
59    fn from(value: &String) -> Self {
60        Error::String(value.clone())
61    }
62}
63
64impl From<&str> for Error {
65    fn from(value: &str) -> Self {
66        Error::String(value.to_string())
67    }
68}
69
70impl From<BoxError> for Error {
71    fn from(value: BoxError) -> Self {
72        Error::String(value.to_string())
73    }
74}
75
76impl From<Error> for std::io::Error {
77    fn from(value: Error) -> Self {
78        match value {
79            Error::Io(io) => io,
80            _ => std::io::Error::other(value),
81        }
82    }
83}
84
85pub type BoxError = Box<dyn std::error::Error + Send + Sync>;
86
87pub type Result<T, E = Error> = std::result::Result<T, E>;