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
22impl Default for Backend {
23 fn default() -> Self {
24 #[cfg(target_os = "linux")]
25 {
26 Self::Kernel
27 }
28 #[cfg(target_os = "openbsd")]
29 {
30 Self::OpenBSD
31 }
32 #[cfg(not(any(target_os = "linux", target_os = "openbsd")))]
33 {
34 Self::Userspace
35 }
36 }
37}
38
39impl Display for Backend {
40 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
41 match self {
42 #[cfg(target_os = "linux")]
43 Self::Kernel => write!(f, "kernel"),
44 #[cfg(target_os = "openbsd")]
45 Self::OpenBSD => write!(f, "kernel"),
46 Self::Userspace => write!(f, "userspace"),
47 }
48 }
49}
50
51impl FromStr for Backend {
52 type Err = String;
53
54 fn from_str(s: &str) -> Result<Self, Self::Err> {
55 match s.to_ascii_lowercase().as_str() {
56 #[cfg(target_os = "linux")]
57 "kernel" => Ok(Self::Kernel),
58 #[cfg(target_os = "openbsd")]
59 "kernel" => Ok(Self::OpenBSD),
60 "userspace" => Ok(Self::Userspace),
61 _ => Err(format!("valid values: {}.", Self::variants().join(", "))),
62 }
63 }
64}
65
66impl Backend {
67 pub fn variants() -> &'static [&'static str] {
68 #[cfg(any(target_os = "linux", target_os = "openbsd"))]
69 {
70 &["kernel", "userspace"]
71 }
72 #[cfg(not(any(target_os = "linux", target_os = "openbsd")))]
73 {
74 &["userspace"]
75 }
76 }
77}