use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct NodeId(pub [u8; 32]);
impl NodeId {
pub fn from_bytes(bytes: [u8; 32]) -> Self {
Self(bytes)
}
pub fn to_hex(&self) -> String {
hex::encode(self.0)
}
pub fn short(&self) -> String {
self.to_hex()[..8].to_string()
}
}
impl fmt::Debug for NodeId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "NodeId({})", self.short())
}
}
impl fmt::Display for NodeId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.short())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeInfo {
pub id: NodeId,
pub name: Option<String>,
pub available_storage: u64,
pub total_storage: u64,
pub addresses: Vec<String>,
pub version: String,
pub last_seen: i64,
}
impl NodeInfo {
pub fn storage_utilization(&self) -> f64 {
if self.total_storage == 0 {
return 0.0;
}
let used = self.total_storage - self.available_storage;
(used as f64 / self.total_storage as f64) * 100.0
}
}