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
use std::str;

use gethostname::gethostname;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::id::host_uuid;

#[derive(Debug, PartialEq, Deserialize, Serialize, Clone)]
pub struct HostInfo {
    /// The hostname
    pub hostname: String,
    /// The (stable) Host UUID.
    pub host_uuid: Uuid,
    /// The host's current uptime
    pub uptime: f64,
}
impl HostInfo {
    pub fn new() -> HostInfo {
        let host_uuid = host_uuid().expect("Failed to generate host UUID");
        HostInfo {
            hostname: gethostname().into_string().expect("Failed to get hostname"),
            host_uuid,
            uptime: uptime_lib::get()
                .expect("Failed to get uptime")
                .as_secs_f64(),
        }
    }
}
impl Default for HostInfo {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_new() {
        let host_info = HostInfo::new();

        // Assert that the hostname is not empty
        assert!(!host_info.hostname.is_empty());

        // Assert that the host_uuid is not nil
        assert_ne!(host_info.host_uuid, Uuid::nil());

        // Assert that the uptime is greater than or equal to zero
        assert!(host_info.uptime >= 0.0);
    }
}