pub struct S;Expand description
The S namespace — static factory methods for state transforms.
Implementations§
Source§impl S
impl S
Sourcepub fn pick(keys: &[&str]) -> StateTransform
pub fn pick(keys: &[&str]) -> StateTransform
Keep only the specified keys.
Sourcepub fn rename(mappings: &[(&str, &str)]) -> StateTransform
pub fn rename(mappings: &[(&str, &str)]) -> StateTransform
Rename keys according to the mappings.
Sourcepub fn merge(keys: &[&str], into: &str) -> StateTransform
pub fn merge(keys: &[&str], into: &str) -> StateTransform
Merge the specified keys into a single key as an object.
Sourcepub fn defaults(defaults: Value) -> StateTransform
pub fn defaults(defaults: Value) -> StateTransform
Set default values for missing keys.
Sourcepub fn map(f: impl Fn(&mut Value) + Send + Sync + 'static) -> StateTransform
pub fn map(f: impl Fn(&mut Value) + Send + Sync + 'static) -> StateTransform
Apply a custom transformation function.
Sourcepub fn flatten(key: &str) -> StateTransform
pub fn flatten(key: &str) -> StateTransform
Flatten a nested object key into the top level.
Sourcepub fn set(key: &str, value: Value) -> StateTransform
pub fn set(key: &str, value: Value) -> StateTransform
Set a key to a fixed value.
Sourcepub fn is_set(key: &str) -> impl Fn(&State) -> bool + Send + Sync + 'static
pub fn is_set(key: &str) -> impl Fn(&State) -> bool + Send + Sync + 'static
Returns true if the given key exists with any non-null value.
Replaces the common pattern |s| s.get::<String>("key").is_some().
.transition("next_phase", S::is_set("caller_name"))Sourcepub fn is_true(key: &str) -> impl Fn(&State) -> bool + Send + Sync + 'static
pub fn is_true(key: &str) -> impl Fn(&State) -> bool + Send + Sync + 'static
Returns true if the given key holds a truthy boolean.
.transition("next_phase", S::is_true("disclosure_given"))Sourcepub fn eq(
key: &str,
expected: &str,
) -> impl Fn(&State) -> bool + Send + Sync + 'static
pub fn eq( key: &str, expected: &str, ) -> impl Fn(&State) -> bool + Send + Sync + 'static
Returns true if the given key equals the expected string value.
.transition("tech:greet", S::eq("issue_type", "technical"))Sourcepub fn one_of(
key: &str,
values: &[&str],
) -> impl Fn(&State) -> bool + Send + Sync + 'static
pub fn one_of( key: &str, values: &[&str], ) -> impl Fn(&State) -> bool + Send + Sync + 'static
Returns true if the given key matches any of the provided string values.
.transition("arrange_payment", S::one_of("negotiation_intent", &["full_pay", "partial_pay"]))Sourcepub fn transform(
key: &str,
f: impl Fn(Value) -> Value + Send + Sync + 'static,
) -> StateTransform
pub fn transform( key: &str, f: impl Fn(Value) -> Value + Send + Sync + 'static, ) -> StateTransform
Transform a single key’s value with a function.
Sourcepub fn guard(
predicate: impl Fn(&Value) -> bool + Send + Sync + 'static,
msg: &str,
) -> StateTransform
pub fn guard( predicate: impl Fn(&Value) -> bool + Send + Sync + 'static, msg: &str, ) -> StateTransform
Guard — assert a condition on state, panic with message if false.
Sourcepub fn compute(
key: &str,
f: impl Fn(&Value) -> Value + Send + Sync + 'static,
) -> StateTransform
pub fn compute( key: &str, f: impl Fn(&Value) -> Value + Send + Sync + 'static, ) -> StateTransform
Compute derived values from state.
Sourcepub fn accumulate(source_key: &str, into: &str) -> StateTransform
pub fn accumulate(source_key: &str, into: &str) -> StateTransform
Accumulate values into a list under a target key.
Sourcepub fn counter(key: &str, step: i64) -> StateTransform
pub fn counter(key: &str, step: i64) -> StateTransform
Increment a counter key by a step.
Sourcepub fn require(keys: &[&str]) -> StateTransform
pub fn require(keys: &[&str]) -> StateTransform
Require that specified keys exist.
Sourcepub fn identity() -> StateTransform
pub fn identity() -> StateTransform
Identity transform — no-op passthrough.
Sourcepub fn when(
predicate: impl Fn(&Value) -> bool + Send + Sync + 'static,
inner: StateTransform,
) -> StateTransform
pub fn when( predicate: impl Fn(&Value) -> bool + Send + Sync + 'static, inner: StateTransform, ) -> StateTransform
Conditional transform — applies inner transform only when predicate is true.
Sourcepub fn drop(keys: &[&str]) -> StateTransform
pub fn drop(keys: &[&str]) -> StateTransform
Drop the specified keys.
Sourcepub fn log(message: &str) -> StateTransform
pub fn log(message: &str) -> StateTransform
Log a message during state transform (side-effect marker).
Useful for debugging transform pipelines — prints the message to stderr each time the transform is applied.
let chain = S::pick(&["a"]) >> S::log("after pick") >> S::rename(&[("a", "x")]);Sourcepub fn unflatten(key: &str) -> StateTransform
pub fn unflatten(key: &str) -> StateTransform
Unflatten a dotted-key object into a nested structure (inverse of flatten).
Takes all top-level keys that start with key. and nests them under key as an object.
For example, {"addr.city": "NYC", "addr.zip": "10001"} with unflatten("addr")
becomes {"addr": {"city": "NYC", "zip": "10001"}}.
let t = S::unflatten("addr");Sourcepub fn zip(keys: &[&str], into: &str) -> StateTransform
pub fn zip(keys: &[&str], into: &str) -> StateTransform
Zip multiple array keys into an array of tuples (arrays).
Takes the arrays at each of keys and produces an array of arrays under into,
where element i contains [keys[0][i], keys[1][i], ...].
Arrays are zipped to the length of the shortest.
// {"names": ["a","b"], "scores": [1,2]} -> {"zipped": [["a",1], ["b",2]]}
let t = S::zip(&["names", "scores"], "zipped");Sourcepub fn group_by(source: &str, key: &str, into: &str) -> StateTransform
pub fn group_by(source: &str, key: &str, into: &str) -> StateTransform
Group array elements by a field value.
Takes the array at source, groups its elements by the string value of key,
and writes the resulting object (field value -> array of elements) to into.
// {"items": [{"type":"a","v":1}, {"type":"b","v":2}, {"type":"a","v":3}]}
// -> {"grouped": {"a": [{"type":"a","v":1}, {"type":"a","v":3}], "b": [{"type":"b","v":2}]}}
let t = S::group_by("items", "type", "grouped");Sourcepub fn history(key: &str, max: usize) -> StateTransform
pub fn history(key: &str, max: usize) -> StateTransform
Keep history of a key’s values (append to list, cap at max).
Each time this transform runs, the current value of key is appended to
{key}_history. The history array is capped at max entries (oldest dropped).
let t = S::history("score", 5); // keeps last 5 score values in "score_history"Sourcepub fn validate(schema: Value) -> StateTransform
pub fn validate(schema: Value) -> StateTransform
Validate state against a JSON schema value.
Panics with a descriptive message if any required key from the schema’s
required array is missing, or if a key’s type doesn’t match the schema’s
properties.{key}.type declaration.
let t = S::validate(json!({
"required": ["name", "age"],
"properties": {
"name": {"type": "string"},
"age": {"type": "number"}
}
}));Sourcepub fn branch(
predicate: impl Fn(&Value) -> bool + Send + Sync + 'static,
if_true: StateTransform,
if_false: StateTransform,
) -> StateTransform
pub fn branch( predicate: impl Fn(&Value) -> bool + Send + Sync + 'static, if_true: StateTransform, if_false: StateTransform, ) -> StateTransform
Conditional branching of state transforms.
Applies if_true when the predicate returns true, otherwise applies if_false.
let t = S::branch(
|s| s.get("premium").and_then(|v| v.as_bool()).unwrap_or(false),
S::set("tier", json!("gold")),
S::set("tier", json!("basic")),
);