firecloud-core 0.2.0

Core types and traits for FireCloud distributed storage
Documentation
//! Node-related types for P2P network

use serde::{Deserialize, Serialize};
use std::fmt;

/// Unique identifier for a node (derived from its public key)
#[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct NodeId(pub [u8; 32]);

impl NodeId {
    /// Create from raw bytes
    pub fn from_bytes(bytes: [u8; 32]) -> Self {
        Self(bytes)
    }

    /// Get as hex string
    pub fn to_hex(&self) -> String {
        hex::encode(self.0)
    }

    /// Short display (first 8 chars)
    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())
    }
}

/// Information about a node in the network
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeInfo {
    /// Node's unique identifier
    pub id: NodeId,
    /// Human-readable name (optional)
    pub name: Option<String>,
    /// Available storage in bytes
    pub available_storage: u64,
    /// Total storage capacity in bytes
    pub total_storage: u64,
    /// Node's public addresses
    pub addresses: Vec<String>,
    /// Software version
    pub version: String,
    /// Last seen timestamp (Unix millis)
    pub last_seen: i64,
}

impl NodeInfo {
    /// Calculate storage utilization as percentage
    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
    }
}