coreason-runtime 0.1.0

Kinetic Plane execution engine for the CoReason Tripartite Cybernetic Manifold
Documentation
// Copyright (c) 2026 CoReason, Inc.
// All rights reserved.

use futures_util::stream::StreamExt;
use std::sync::Arc;

/// Dynamic validator listening to NATS registry assertions published by the Rust-based URN Authority
pub struct UrnValidator {
    nats_client: Option<async_nats::Client>,
}

impl UrnValidator {
    /// Creates a new UrnValidator instance
    pub fn new() -> Self {
        Self { nats_client: None }
    }

    /// Connects asynchronously to the NATS cluster broker
    pub async fn connect(&mut self, url: &str) -> Result<(), String> {
        let client = async_nats::connect(url)
            .await
            .map_err(|e| format!("NATS connection failed: {}", e))?;
        self.nats_client = Some(client);
        println!("[URN-VAL] Connected to NATS broker at: {}", url);
        Ok(())
    }

    /// Starts an asynchronous message loop validating signature payloads in the background
    pub async fn start_validation_loop(&self, subject: &str) -> Result<(), String> {
        let client = self
            .nats_client
            .as_ref()
            .ok_or_else(|| "NATS client not connected".to_string())?;

        let mut subscriber = client
            .subscribe(subject.to_string())
            .await
            .map_err(|e| format!("NATS subscription failed: {}", e))?;

        println!("[URN-VAL] Listening on NATS subject: {}", subject);

        tokio::spawn(async move {
            while let Some(message) = subscriber.next().await {
                let payload = String::from_utf8_lossy(&message.payload);
                println!(
                    "[URN-VAL] Received registration payload from URN Authority: {}",
                    payload
                );
                // In production, signature checks map directly to coreason_runtime_rust::license functions.
            }
        });

        Ok(())
    }
}