righvalor 0.1.0

RighValor: AI Infrastructure and Applications Framework for the Far Edge
use std::collections::HashSet;

use serde::Deserialize;

use crate::service::ValorServiceId;

/// Registry holding the mapping from service IDs to concrete services
#[derive(Debug, Default)]
pub struct ValorServiceRegistry {
    #[allow(dead_code)]
    services: HashSet<ValorServiceId>,
}

impl ValorServiceRegistry {
    /// Create an empty registry
    pub fn new() -> Self {
        Self {
            services: HashSet::new(),
        }
    }

    /// Load registry from a TOML file (e.g., valor/service.toml)
    #[allow(dead_code)]
    pub fn load_from_file(path: &str) -> anyhow::Result<Self> {
        let data = std::fs::read_to_string(path)?;
        let cfg: ValorServiceToml = toml::from_str(&data)?;
        let mut reg = Self::new();
        for entry in cfg
            .service
            .into_iter()
            .filter(|e| e.enabled.unwrap_or(true))
        {
            reg.register(entry.id);
        }
        Ok(reg)
    }

    /// Register a service ID
    #[allow(dead_code)]
    pub fn register(&mut self, id: ValorServiceId) {
        self.services.insert(id);
    }

    /// Check if a service is registered
    #[allow(dead_code)]
    pub fn contains(&self, id: &ValorServiceId) -> bool {
        self.services.contains(id)
    }

    /// Current number of registered services
    #[allow(dead_code)]
    pub fn len(&self) -> usize {
        self.services.len()
    }

    /// Iterate over all registered services
    #[allow(dead_code)]
    pub fn iter(&self) -> impl Iterator<Item = &ValorServiceId> {
        self.services.iter()
    }
}

// ---------------- TOML config -----------------

#[derive(Debug, Deserialize)]
pub struct ValorServiceToml {
    /// Services defined in this node
    #[serde(default)]
    pub service: Vec<ValorServiceTomlEntry>,
}

#[derive(Debug, Deserialize)]
pub struct ValorServiceTomlEntry {
    /// Unique service ID, e.g. "righ.device_classification" or "common.cmd"
    pub id: ValorServiceId,
    /// Enable/disable this service (default true)
    #[serde(default)]
    pub enabled: Option<bool>,
    /// Friendly name for humans (optional)
    #[serde(default)]
    #[allow(dead_code)]
    pub name: Option<String>,
    /// Preferred runtime: onnx|tensorflow|wasm|cmd (optional)
    #[serde(default)]
    #[allow(dead_code)]
    pub runtime: Option<String>,
    /// Semantic version, e.g. "0.1.0" (optional)
    #[serde(default)]
    #[allow(dead_code)]
    pub version: Option<String>,
    /// Model path for model-based services (optional)
    #[serde(default)]
    #[allow(dead_code)]
    pub model_path: Option<String>,
    /// Metadata file path (bytes) to load (optional)
    #[serde(default)]
    #[allow(dead_code)]
    pub metadata_file: Option<String>,
    /// Default command for common.cmd (optional)
    #[serde(default)]
    #[allow(dead_code)]
    pub command: Option<String>,
    /// Default args for common.cmd (optional)
    #[serde(default)]
    #[allow(dead_code)]
    pub args: Option<Vec<String>>,
}