tun/
error.rs

1//            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2//                    Version 2, December 2004
3//
4// Copyleft (ↄ) meh. <meh@schizofreni.co> | http://meh.schizofreni.co
5//
6// Everyone is permitted to copy and distribute verbatim or modified
7// copies of this license document, and changing it is allowed as long
8// as the name is changed.
9//
10//            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
11//   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
12//
13//  0. You just DO WHAT THE FUCK YOU WANT TO.
14
15#[derive(thiserror::Error, Debug)]
16pub enum Error {
17    #[error("invalid configuration")]
18    InvalidConfig,
19
20    #[error("not implementated")]
21    NotImplemented,
22
23    #[error("device tun name too long")]
24    NameTooLong,
25
26    #[error("invalid device tun name")]
27    InvalidName,
28
29    #[error("invalid address")]
30    InvalidAddress,
31
32    #[error("invalid file descriptor")]
33    InvalidDescriptor,
34
35    #[error("unsuported network layer of operation")]
36    UnsupportedLayer,
37
38    #[error("invalid queues number")]
39    InvalidQueuesNumber,
40
41    #[error("out of range integral type conversion attempted")]
42    TryFromIntError,
43
44    #[error(transparent)]
45    Io(#[from] std::io::Error),
46
47    #[error(transparent)]
48    Nul(#[from] std::ffi::NulError),
49
50    #[error(transparent)]
51    ParseNum(#[from] std::num::ParseIntError),
52
53    #[cfg(target_os = "windows")]
54    #[error(transparent)]
55    WintunError(#[from] wintun_bindings::Error),
56
57    #[error("{0}")]
58    String(String),
59}
60
61impl From<&str> for Error {
62    fn from(err: &str) -> Self {
63        Self::String(err.to_string())
64    }
65}
66
67impl From<String> for Error {
68    fn from(err: String) -> Self {
69        Self::String(err)
70    }
71}
72
73impl From<&String> for Error {
74    fn from(err: &String) -> Self {
75        Self::String(err.to_string())
76    }
77}
78
79impl From<Error> for std::io::Error {
80    fn from(value: Error) -> Self {
81        match value {
82            Error::Io(err) => err,
83            _ => std::io::Error::other(value),
84        }
85    }
86}
87
88pub type BoxError = Box<dyn std::error::Error + Send + Sync>;
89
90pub type Result<T, E = Error> = ::std::result::Result<T, E>;