1use serde::{Deserialize, Serialize};
4use std::fmt;
5
6#[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
8pub struct NodeId(pub [u8; 32]);
9
10impl NodeId {
11 pub fn from_bytes(bytes: [u8; 32]) -> Self {
13 Self(bytes)
14 }
15
16 pub fn to_hex(&self) -> String {
18 hex::encode(self.0)
19 }
20
21 pub fn short(&self) -> String {
23 self.to_hex()[..8].to_string()
24 }
25}
26
27impl fmt::Debug for NodeId {
28 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29 write!(f, "NodeId({})", self.short())
30 }
31}
32
33impl fmt::Display for NodeId {
34 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35 write!(f, "{}", self.short())
36 }
37}
38
39#[derive(Debug, Clone, Serialize, Deserialize)]
41pub struct NodeInfo {
42 pub id: NodeId,
44 pub name: Option<String>,
46 pub available_storage: u64,
48 pub total_storage: u64,
50 pub addresses: Vec<String>,
52 pub version: String,
54 pub last_seen: i64,
56}
57
58impl NodeInfo {
59 pub fn storage_utilization(&self) -> f64 {
61 if self.total_storage == 0 {
62 return 0.0;
63 }
64 let used = self.total_storage - self.available_storage;
65 (used as f64 / self.total_storage as f64) * 100.0
66 }
67}