flow-toolkit 0.1.0

Ordinary typed Rust functions and exact native data for Flow Tools, automatically prepared from direct native imports.
Documentation
//! The Rust toolkit for Tool implementations (`docs/runtime/spec/abi.md`,
//! *Toolkits*).
//!
//! # Ordinary Rust functions
//!
//! Annotate public functions with [`tool`] and derive [`Flow`] for boundary
//! records and enums. A direct `.rs` import selects a module in its Cargo library;
//! the `flow-toolkit` CLI prepares its generated launcher. Function bodies remain
//! directly callable by Rust tests. An optional [`lifecycle`] initializer returns a
//! [`Resource`] whose asynchronous or synchronous cleanup runs after calls drain
//! at normal shutdown; an injected [`Context`] retrieves the shared resource and
//! writes call-correlated operational logs through [`Context::log`]. Native build and metadata extraction happen during
//! explicit preparation; metadata-only reads never compile or invoke bodies.
//!
//! # Types
//!
//! | Flow | Rust |
//! | --- | --- |
//! | `Int` | [`Int`], exact at any size |
//! | `Decimal` | [`Decimal`], exact, never binary floating point |
//! | `Bool`, `Text`, `Unit`, `Never` | `bool`, `String`, `()`, [`Infallible`](std::convert::Infallible) |
//! | `Bytes` | [`Bytes`] |
//! | `Duration`, `Instant` | [`Duration`], [`Instant`]: exact seconds |
//! | `List<T>`, `Set<T>`, `Dict<K, V>` | `Vec<T>`, `BTreeSet<T>`, `BTreeMap<K, V>` |
//! | `Option<T>`, tuples | `Option<T>`, tuples |
//! | `json.Value` | [`JsonValue`], with Flow's equality and order |
//! | a record, an enum | a struct, an enum |
//! | a `@tool type` | `#[derive(Flow)] #[flow(opaque)]` String newtype |
//! | a Tool's type parameter | [`Value`], admitted through [`ContextCodec`] |
//! | a parameter-dependent `Set` or `Dict` | [`ContextSet`], [`ContextDict`] |
//!
//! Generic calls carry validated schemas and type descriptions. The automatic
//! bridge instantiates generic Rust functions with typed erased values and applies
//! contextual codecs through their native containers, records and enum payloads.
//! Ordinary monomorphic values keep their native Rust representation.
//!
//! Narrowing an [`Int`] to a fixed width is the author's own checked
//! conversion.
//!
//! # Answers
//!
//! A call whose arguments do not decode is answered `NotRun`, as is one
//! naming a declaration this implementation does not serve; a method that
//! panics is answered `Unknown` (`abi.md`, *Toolkits*). A `cancel` sets the
//! call's [`Context::cancelled`]; it proves nothing, and a reply sent after
//! it is harmless. Calls run concurrently, one thread each.

pub mod data;
mod server;

pub use data::{
    Bytes, ContextCodec, ContextDict, ContextSet, Data, DecodeError, JsonValue, PayloadView,
    parse_json,
};
pub use flow_abi::{DecodeErrorKind, Json, ValidatedTypes};
pub use flow_value::{Decimal, Duration, Instant, Int};
pub use server::{Context, Problem};

mod function;
#[doc(hidden)]
pub mod prepare;
pub use flow_toolkit_macros::{Flow, lifecycle, tool};
pub use function::{FlowType, Resource, Value};

/// Compiler-generated adapter ABI. Native authors use `#[tool]` and `Flow` instead.
#[doc(hidden)]
pub mod __bridge {
    pub use crate::function::{
        Function, Lifecycle, Shape, Start, Types, block_on, decode_field, encode_field, extract,
    };
    use crate::server::{Args, Declaration};
    use crate::{Context, Data, DecodeError, Json, Problem};
    use flow_abi::TypeSpec;
    /// Metadata construction emitted only by `#[tool]`, then compiler checked during preparation.
    pub fn function(
        identity: (&'static str, &[&str]),
        docs: &'static str,
        params: Vec<(String, String)>,
        ok: String,
        err: String,
        types: Types,
        factory: fn(&str, TypeSpec) -> Adapter,
    ) -> Function {
        Function::new(identity.0, docs, params, ok, err, types, factory).with_generics(identity.1)
    }
    /// A generated adapter with no public registration or serving operations.
    pub struct Adapter(pub(crate) Declaration);
    /// Constructs the typed boundary emitted by `#[tool]`.
    pub fn typed<T: Data, E: Data, F>(entry: &str, spec: TypeSpec, call: F) -> Adapter
    where
        F: Fn(&Context, &Args<'_>) -> Result<Result<T, Problem<E>>, DecodeError>
            + Send
            + Sync
            + 'static,
    {
        Adapter(Declaration::new(entry, spec, call))
    }
    /// Constructs the contextual boundary emitted for a generic `#[tool]`.
    pub fn contextual<F>(entry: &str, spec: TypeSpec, call: F) -> Adapter
    where
        F: Fn(&Context, &Args<'_>) -> Result<Result<Json, Problem<Json>>, DecodeError>
            + Send
            + Sync
            + 'static,
    {
        Adapter(Declaration::contextual(entry, spec, call))
    }
    /// Entry point called only by the automatically compiled foreign launcher.
    pub fn run(functions: &[Function], start: Option<Start>) -> Result<(), String> {
        crate::function::launcher_with_lifecycle(functions, start)
    }
}

#[cfg(test)]
extern crate self as flow_toolkit;
#[cfg(test)]
mod test_contextual;
#[cfg(test)]
mod test_protocol;

#[cfg(test)]
mod test_contract;

#[cfg(test)]
#[path = "../tests/common/mod.rs"]
mod test_common;