flow_toolkit/lib.rs
1//! The Rust toolkit for Tool implementations (`docs/runtime/spec/abi.md`,
2//! *Toolkits*).
3//!
4//! # Ordinary Rust functions
5//!
6//! Annotate public functions with [`tool`] and derive [`Flow`] for boundary
7//! records and enums. A direct `.rs` import selects a module in its Cargo library;
8//! the `flow-toolkit` CLI prepares its generated launcher. Function bodies remain
9//! directly callable by Rust tests. An optional [`lifecycle`] initializer returns a
10//! [`Resource`] whose asynchronous or synchronous cleanup runs after calls drain
11//! at normal shutdown; an injected [`Context`] retrieves the shared resource and
12//! writes call-correlated operational logs through [`Context::log`]. Native build and metadata extraction happen during
13//! explicit preparation; metadata-only reads never compile or invoke bodies.
14//!
15//! # Types
16//!
17//! | Flow | Rust |
18//! | --- | --- |
19//! | `Int` | [`Int`], exact at any size |
20//! | `Decimal` | [`Decimal`], exact, never binary floating point |
21//! | `Bool`, `Text`, `Unit`, `Never` | `bool`, `String`, `()`, [`Infallible`](std::convert::Infallible) |
22//! | `Bytes` | [`Bytes`] |
23//! | `Duration`, `Instant` | [`Duration`], [`Instant`]: exact seconds |
24//! | `List<T>`, `Set<T>`, `Dict<K, V>` | `Vec<T>`, `BTreeSet<T>`, `BTreeMap<K, V>` |
25//! | `Option<T>`, tuples | `Option<T>`, tuples |
26//! | `json.Value` | [`JsonValue`], with Flow's equality and order |
27//! | a record, an enum | a struct, an enum |
28//! | a `@tool type` | `#[derive(Flow)] #[flow(opaque)]` String newtype |
29//! | a Tool's type parameter | [`Value`], admitted through [`ContextCodec`] |
30//! | a parameter-dependent `Set` or `Dict` | [`ContextSet`], [`ContextDict`] |
31//!
32//! Generic calls carry validated schemas and type descriptions. The automatic
33//! bridge instantiates generic Rust functions with typed erased values and applies
34//! contextual codecs through their native containers, records and enum payloads.
35//! Ordinary monomorphic values keep their native Rust representation.
36//!
37//! Narrowing an [`Int`] to a fixed width is the author's own checked
38//! conversion.
39//!
40//! # Answers
41//!
42//! A call whose arguments do not decode is answered `NotRun`, as is one
43//! naming a declaration this implementation does not serve; a method that
44//! panics is answered `Unknown` (`abi.md`, *Toolkits*). A `cancel` sets the
45//! call's [`Context::cancelled`]; it proves nothing, and a reply sent after
46//! it is harmless. Calls run concurrently, one thread each.
47
48pub mod data;
49mod server;
50
51pub use data::{
52 Bytes, ContextCodec, ContextDict, ContextSet, Data, DecodeError, JsonValue, PayloadView,
53 parse_json,
54};
55pub use flow_abi::{DecodeErrorKind, Json, ValidatedTypes};
56pub use flow_value::{Decimal, Duration, Instant, Int};
57pub use server::{Context, Problem};
58
59mod function;
60#[doc(hidden)]
61pub mod prepare;
62pub use flow_toolkit_macros::{Flow, lifecycle, tool};
63pub use function::{FlowType, Resource, Value};
64
65/// Compiler-generated adapter ABI. Native authors use `#[tool]` and `Flow` instead.
66#[doc(hidden)]
67pub mod __bridge {
68 pub use crate::function::{
69 Function, Lifecycle, Shape, Start, Types, block_on, decode_field, encode_field, extract,
70 };
71 use crate::server::{Args, Declaration};
72 use crate::{Context, Data, DecodeError, Json, Problem};
73 use flow_abi::TypeSpec;
74 /// Metadata construction emitted only by `#[tool]`, then compiler checked during preparation.
75 pub fn function(
76 identity: (&'static str, &[&str]),
77 docs: &'static str,
78 params: Vec<(String, String)>,
79 ok: String,
80 err: String,
81 types: Types,
82 factory: fn(&str, TypeSpec) -> Adapter,
83 ) -> Function {
84 Function::new(identity.0, docs, params, ok, err, types, factory).with_generics(identity.1)
85 }
86 /// A generated adapter with no public registration or serving operations.
87 pub struct Adapter(pub(crate) Declaration);
88 /// Constructs the typed boundary emitted by `#[tool]`.
89 pub fn typed<T: Data, E: Data, F>(entry: &str, spec: TypeSpec, call: F) -> Adapter
90 where
91 F: Fn(&Context, &Args<'_>) -> Result<Result<T, Problem<E>>, DecodeError>
92 + Send
93 + Sync
94 + 'static,
95 {
96 Adapter(Declaration::new(entry, spec, call))
97 }
98 /// Constructs the contextual boundary emitted for a generic `#[tool]`.
99 pub fn contextual<F>(entry: &str, spec: TypeSpec, call: F) -> Adapter
100 where
101 F: Fn(&Context, &Args<'_>) -> Result<Result<Json, Problem<Json>>, DecodeError>
102 + Send
103 + Sync
104 + 'static,
105 {
106 Adapter(Declaration::contextual(entry, spec, call))
107 }
108 /// Entry point called only by the automatically compiled foreign launcher.
109 pub fn run(functions: &[Function], start: Option<Start>) -> Result<(), String> {
110 crate::function::launcher_with_lifecycle(functions, start)
111 }
112}
113
114#[cfg(test)]
115extern crate self as flow_toolkit;
116#[cfg(test)]
117mod test_contextual;
118#[cfg(test)]
119mod test_protocol;
120
121#[cfg(test)]
122mod test_contract;
123
124#[cfg(test)]
125#[path = "../tests/common/mod.rs"]
126mod test_common;