1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use crate::contexts::{Context, ContextProvider};
use anyhow::Result;
use gethostname::gethostname;
use os_info;

pub struct OSContextProvider {}

impl ContextProvider for OSContextProvider {
    fn get_prefix(&self) -> String {
        String::from("os")
    }

    fn get_contexts(&self) -> Result<Vec<super::Context>> {
        let osinfo = os_info::get();

        Ok(vec![
            Context::KeyValueContext(String::from("hostname"), gethostname().into()),
            Context::KeyValueContext(String::from("family"), std::env::consts::FAMILY.into()),
            Context::KeyValueContext(String::from("name"), std::env::consts::OS.into()),
            Context::KeyValueContext(
                String::from("distribution"),
                format!("{}", osinfo.os_type()).into(),
            ),
            Context::KeyValueContext(
                String::from("codename"),
                String::from(osinfo.codename().unwrap_or("unknown")).into(),
            ),
            Context::KeyValueContext(
                String::from("bitness"),
                format!("{}", osinfo.bitness()).into(),
            ),
            Context::KeyValueContext(
                String::from("version"),
                format!("{}", osinfo.version()).into(),
            ),
            Context::KeyValueContext(
                String::from("edition"),
                String::from(osinfo.edition().unwrap_or("unknown")).into(),
            ),
        ])
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn it_can_prefix() {
        let oscontext = OSContextProvider {};
        let prefix = oscontext.get_prefix();
        assert_eq!(String::from("os"), prefix);
    }

    #[test]
    #[cfg(target_os = "macos")]
    fn it_can_macos() {
        let oscontext = OSContextProvider {};
        let keyvaluepairs = oscontext.get_contexts().unwrap();

        keyvaluepairs.iter().for_each(|context| match context {
            Context::KeyValueContext(k, v) => match k.as_ref() {
                "family" => assert_eq!(v.to_string(), String::from("unix")),
                "name" => assert_eq!(v.to_string(), String::from("macos")),
                _ => (),
            },
            Context::ListContext(_, _) => {
                assert_eq!(true, false);
            }
        })
    }

    #[test]
    #[cfg(windows)]
    fn it_can_windows() {
        let oscontext = OSContextProvider {};
        let keyvaluepairs = oscontext.get_contexts().unwrap();

        keyvaluepairs.iter().for_each(|context| match context {
            Context::KeyValueContext(k, v) => match k.as_ref() {
                "family" => assert_eq!(v.to_string(), String::from("windows")),
                "name" => assert_eq!(v.to_string(), String::from("windows")),
                _ => (),
            },
            Context::ListContext(_, _) => {
                assert_eq!(true, false);
            }
        })
    }

    #[test]
    #[cfg(target_os = "linux")]
    fn it_can_linux() {
        let oscontext = OSContextProvider {};
        let keyvaluepairs = oscontext.get_contexts().unwrap();

        keyvaluepairs.iter().for_each(|context| match context {
            Context::KeyValueContext(k, v) => match k.as_ref() {
                "family" => assert_eq!(v.to_string(), String::from("unix")),
                "name" => assert_eq!(v.to_string(), String::from("linux")),
                _ => (),
            },
            Context::ListContext(_, _) => {
                assert_eq!(true, false);
            }
        })
    }

    #[test]
    #[cfg(target_os = "freebsd")]
    fn it_can_linux() {
        let oscontext = OSContextProvider {};
        let keyvaluepairs = oscontext.get_contexts().unwrap();

        keyvaluepairs.iter().for_each(|context| match context {
            Context::KeyValueContext(k, v) => match k.as_ref() {
                "family" => assert_eq!(v.to_string(), String::from("unix")),
                "name" => assert_eq!(v.to_string(), String::from("freebsd")),
                _ => (),
            },
            Context::ListContext(_, _) => {
                assert_eq!(true, false);
            }
        })
    }
}