comtrya_lib/contexts/
os.rs1use crate::contexts::{Context, ContextProvider};
2use anyhow::Result;
3use gethostname::gethostname;
4use os_info;
5
6pub struct OSContextProvider {}
7
8impl ContextProvider for OSContextProvider {
9 fn get_prefix(&self) -> String {
10 String::from("os")
11 }
12
13 fn get_contexts(&self) -> Result<Vec<super::Context>> {
14 let osinfo = os_info::get();
15
16 Ok(vec![
17 Context::KeyValueContext(String::from("hostname"), gethostname().into()),
18 Context::KeyValueContext(String::from("family"), std::env::consts::FAMILY.into()),
19 Context::KeyValueContext(String::from("name"), std::env::consts::OS.into()),
20 Context::KeyValueContext(
21 String::from("distribution"),
22 format!("{}", osinfo.os_type()).into(),
23 ),
24 Context::KeyValueContext(
25 String::from("codename"),
26 String::from(osinfo.codename().unwrap_or("unknown")).into(),
27 ),
28 Context::KeyValueContext(
29 String::from("bitness"),
30 format!("{}", osinfo.bitness()).into(),
31 ),
32 Context::KeyValueContext(
33 String::from("version"),
34 format!("{}", osinfo.version()).into(),
35 ),
36 Context::KeyValueContext(
37 String::from("edition"),
38 String::from(osinfo.edition().unwrap_or("unknown")).into(),
39 ),
40 ])
41 }
42}
43
44#[cfg(test)]
45mod test {
46 use super::*;
47
48 #[test]
49 fn it_can_prefix() {
50 let oscontext = OSContextProvider {};
51 let prefix = oscontext.get_prefix();
52 assert_eq!(String::from("os"), prefix);
53 }
54
55 #[test]
56 #[cfg(target_os = "macos")]
57 fn it_can_macos() {
58 let oscontext = OSContextProvider {};
59 let keyvaluepairs = oscontext.get_contexts().unwrap();
60
61 keyvaluepairs.iter().for_each(|context| match context {
62 Context::KeyValueContext(k, v) => match k.as_ref() {
63 "family" => assert_eq!(v.to_string(), String::from("unix")),
64 "name" => assert_eq!(v.to_string(), String::from("macos")),
65 _ => (),
66 },
67 Context::ListContext(_, _) => {
68 assert_eq!(true, false);
69 }
70 })
71 }
72
73 #[test]
74 #[cfg(windows)]
75 fn it_can_windows() {
76 let oscontext = OSContextProvider {};
77 let keyvaluepairs = oscontext.get_contexts().unwrap();
78
79 keyvaluepairs.iter().for_each(|context| match context {
80 Context::KeyValueContext(k, v) => match k.as_ref() {
81 "family" => assert_eq!(v.to_string(), String::from("windows")),
82 "name" => assert_eq!(v.to_string(), String::from("windows")),
83 _ => (),
84 },
85 Context::ListContext(_, _) => {
86 assert_eq!(true, false);
87 }
88 })
89 }
90
91 #[test]
92 #[cfg(target_os = "linux")]
93 fn it_can_linux() {
94 let oscontext = OSContextProvider {};
95 let keyvaluepairs = oscontext.get_contexts().unwrap();
96
97 keyvaluepairs.iter().for_each(|context| match context {
98 Context::KeyValueContext(k, v) => match k.as_ref() {
99 "family" => assert_eq!(v.to_string(), String::from("unix")),
100 "name" => assert_eq!(v.to_string(), String::from("linux")),
101 _ => (),
102 },
103 Context::ListContext(_, _) => {
104 assert_eq!(true, false);
105 }
106 })
107 }
108
109 #[test]
110 #[cfg(target_os = "freebsd")]
111 fn it_can_linux() {
112 let oscontext = OSContextProvider {};
113 let keyvaluepairs = oscontext.get_contexts().unwrap();
114
115 keyvaluepairs.iter().for_each(|context| match context {
116 Context::KeyValueContext(k, v) => match k.as_ref() {
117 "family" => assert_eq!(v.to_string(), String::from("unix")),
118 "name" => assert_eq!(v.to_string(), String::from("freebsd")),
119 _ => (),
120 },
121 Context::ListContext(_, _) => {
122 assert_eq!(true, false);
123 }
124 })
125 }
126}