evorule-primitives 0.1.0

EvoRule base primitive types: the deterministic JSON value model and the I/O handler contract (single definition site, zero evorule-* dependencies)
Documentation
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2026 EvoRule Project
// This file is part of EvoRule, licensed under GNU Affero General Public License v3 or later.
//! I/O handler trait — object-safe, supports dynamic dispatch via trait objects.
//!
//! This contract was historically defined inside the reactor crate (itself
//! moved there from governance during the H5 architecture-debt fix). It now
//! lives at the base-primitive layer so that any ecosystem block can speak
//! the I/O handler contract without depending on the canonical execution
//! crates; the reactor re-exports it unchanged.
//!
//! # Usage example
//!
//! ```
//! use async_trait::async_trait;
//! use evorule_primitives::{IoHandler, IoResult};
//! use evorule_primitives::JsonValue;
//!
//! struct EchoHandler;
//!
//! #[async_trait]
//! impl IoHandler for EchoHandler {
//!     async fn execute(&self, params: &JsonValue) -> IoResult {
//!         Ok(params.clone())
//!     }
//! }
//! ```

use crate::value::JsonValue;
use alloc::boxed::Box;
use alloc::string::String;
use async_trait::async_trait;

/// I/O execution result.
///
/// On success returns a [`JsonValue`] (injected into
/// `payload.__io_results__.{io_type}` by the reactor); on failure returns an
/// error message (recorded into the `IoResponse.error` field).
pub type IoResult = Result<JsonValue, String>;

/// I/O handler trait — all I/O handlers implement this interface.
///
/// Uses `#[async_trait]` to make the trait object-safe, enabling
/// `Arc<dyn IoHandler>` dynamic dispatch.
///
/// `params` comes from `Fact::IoRequest.params`, already resolved from path
/// references into concrete values by the TCB.
///
/// # Implementer responsibilities
/// - Extract the required fields from `params` (e.g. `url`, `query`, `key`)
/// - Perform the actual I/O operation (HTTP request, SQL query, file I/O, etc.)
/// - Return `Ok(JsonValue)` or `Err(String)`
///
/// # Callers
/// - The governance `IoDispatcher` dispatches to the matching handler by `IoType`
/// - The governance `IoSubscriber` subscribes to `Fact::IoRequest` and calls dispatch
#[async_trait]
pub trait IoHandler: Send + Sync {
    /// Execute the I/O operation.
    ///
    /// # Parameters
    /// - `params`: I/O request parameters (passed through by the `io_request`
    ///   instruction; e.g. `call_external` carries
    ///   `{"messages": [...], "tools": [...]}`, `call_service` carries
    ///   `{"service_name": "...", "args": {...}}`)
    ///
    /// # Returns
    /// - `Ok(JsonValue)`: I/O result (injected into `payload.__io_results__.{io_type}`)
    /// - `Err(String)`: error message (recorded into `IoResponse.error`; the
    ///   result is not consumed)
    async fn execute(&self, params: &JsonValue) -> IoResult;
}