wireguard_control/
lib.rs

1pub mod backends;
2mod config;
3mod device;
4mod key;
5
6use std::{
7    fmt::{self, Display, Formatter},
8    str::FromStr,
9};
10
11pub use crate::{config::*, device::*, key::*};
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub enum Backend {
15    #[cfg(target_os = "linux")]
16    Kernel,
17    #[cfg(target_os = "openbsd")]
18    OpenBSD,
19    Userspace,
20}
21
22#[allow(clippy::derivable_impls)]
23impl Default for Backend {
24    fn default() -> Self {
25        #[cfg(target_os = "linux")]
26        {
27            Self::Kernel
28        }
29        #[cfg(target_os = "openbsd")]
30        {
31            Self::OpenBSD
32        }
33        #[cfg(not(any(target_os = "linux", target_os = "openbsd")))]
34        {
35            Self::Userspace
36        }
37    }
38}
39
40impl Display for Backend {
41    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
42        match self {
43            #[cfg(target_os = "linux")]
44            Self::Kernel => write!(f, "kernel"),
45            #[cfg(target_os = "openbsd")]
46            Self::OpenBSD => write!(f, "kernel"),
47            Self::Userspace => write!(f, "userspace"),
48        }
49    }
50}
51
52impl FromStr for Backend {
53    type Err = String;
54
55    fn from_str(s: &str) -> Result<Self, Self::Err> {
56        match s.to_ascii_lowercase().as_str() {
57            #[cfg(target_os = "linux")]
58            "kernel" => Ok(Self::Kernel),
59            #[cfg(target_os = "openbsd")]
60            "kernel" => Ok(Self::OpenBSD),
61            "userspace" => Ok(Self::Userspace),
62            _ => Err(format!("valid values: {}.", Self::variants().join(", "))),
63        }
64    }
65}
66
67impl Backend {
68    pub fn variants() -> &'static [&'static str] {
69        #[cfg(any(target_os = "linux", target_os = "openbsd"))]
70        {
71            &["kernel", "userspace"]
72        }
73        #[cfg(not(any(target_os = "linux", target_os = "openbsd")))]
74        {
75            &["userspace"]
76        }
77    }
78}