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 pub hostname: String,
13 pub host_uuid: Uuid,
15 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!(!host_info.hostname.is_empty());
46
47 assert_ne!(host_info.host_uuid, Uuid::nil());
49
50 assert!(host_info.uptime >= 0.0);
52 }
53}