netzwork_api/host/
mod.rs

1use std::str;
2
3use gethostname::gethostname;
4use serde::{Deserialize, Serialize};
5use uuid::Uuid;
6
7use crate::id::host_uuid;
8
9#[derive(Debug, PartialEq, Deserialize, Serialize, Clone)]
10pub struct HostInfo {
11    /// The hostname
12    pub hostname: String,
13    /// The (stable) Host UUID.
14    pub host_uuid: Uuid,
15    /// The host's current uptime
16    pub uptime: f64,
17}
18impl HostInfo {
19    pub fn new() -> HostInfo {
20        let host_uuid = host_uuid().expect("Failed to generate host UUID");
21        HostInfo {
22            hostname: gethostname().into_string().expect("Failed to get hostname"),
23            host_uuid,
24            uptime: uptime_lib::get()
25                .expect("Failed to get uptime")
26                .as_secs_f64(),
27        }
28    }
29}
30impl Default for HostInfo {
31    fn default() -> Self {
32        Self::new()
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39
40    #[test]
41    fn test_new() {
42        let host_info = HostInfo::new();
43
44        // Assert that the hostname is not empty
45        assert!(!host_info.hostname.is_empty());
46
47        // Assert that the host_uuid is not nil
48        assert_ne!(host_info.host_uuid, Uuid::nil());
49
50        // Assert that the uptime is greater than or equal to zero
51        assert!(host_info.uptime >= 0.0);
52    }
53}