nautalid 0.1.0

Scratch container substrate — TLS 1.3 HTTP/2+3 kernel, LID/AetherDB, optional filter bus (GPL-3.0-or-later).
//! Control-plane status for ECDSA image-trust configuration.

use kdl::KdlNode;

use crate::bus::{BusModule, response};

pub struct ImageTrustModule {
    configured: bool,
}

impl ImageTrustModule {
    #[must_use]
    pub fn new(configured: bool) -> Self {
        Self { configured }
    }

    fn command_node<'a>(
        module_node: &'a KdlNode,
        command_nodes: &[&'a KdlNode],
    ) -> Option<&'a KdlNode> {
        if let Some(cmd) = command_nodes.first() {
            return Some(cmd);
        }
        module_node.children()?.nodes().first()
    }
}

impl BusModule for ImageTrustModule {
    fn name(&self) -> &'static str {
        "image-trust"
    }

    fn handle(&self, module_node: &KdlNode, command_nodes: &[&KdlNode]) -> String {
        let Some(cmd) = Self::command_node(module_node, command_nodes) else {
            return response::err("image-trust commands: status");
        };
        match cmd.name().value() {
            "status" => format!(
                "status configured={} endpoint=\"/.well-known/nautalid-image-trust.json\"",
                self.configured
            ),
            other => response::err(&format!("unknown image-trust command: {other}")),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use kdl::KdlDocument;

    #[test]
    fn status_when_unconfigured() {
        let doc: KdlDocument = "image-trust {\n    status\n}".parse().unwrap();
        let node = doc.nodes().first().unwrap();
        let body = ImageTrustModule::new(false).handle(node, &[]);
        assert!(body.contains("configured=false"));
    }
}