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 {
pub hostname: String,
pub host_uuid: Uuid,
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!(!host_info.hostname.is_empty());
assert_ne!(host_info.host_uuid, Uuid::nil());
assert!(host_info.uptime >= 0.0);
}
}