nautalid 0.1.0

Scratch container substrate — TLS 1.3 HTTP/2+3 kernel, LID/AetherDB, optional filter bus (GPL-3.0-or-later).
//! Bus control for inbound TLS reload (`tls reload`).

use std::sync::Arc;

use kdl::KdlNode;

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

/// TLS module for [`crate::bus::BusRegistry`].
pub struct TlsModule {
    tls: Arc<InboundTls>,
}

impl TlsModule {
    #[must_use]
    pub fn new(tls: Arc<InboundTls>) -> Self {
        Self { tls }
    }

    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 TlsModule {
    fn name(&self) -> &'static str {
        "tls"
    }

    fn handle(&self, module_node: &KdlNode, command_nodes: &[&KdlNode]) -> String {
        let Some(cmd) = Self::command_node(module_node, command_nodes) else {
            return response::err("tls commands: reload");
        };
        match cmd.name().value() {
            "reload" => match tokio::task::block_in_place(|| {
                tokio::runtime::Handle::current().block_on(self.tls.reload_from_env())
            }) {
                Ok(()) => response::ok("reloaded"),
                Err(err) => response::err(&err.to_string()),
            },
            other => response::err(&format!("unknown tls command: {other}")),
        }
    }
}