S

Struct S 

Source
pub struct S;
Expand description

The S namespace — static factory methods for state transforms.

Implementations§

Source§

impl S

Source

pub fn pick(keys: &[&str]) -> StateTransform

Keep only the specified keys.

Source

pub fn rename(mappings: &[(&str, &str)]) -> StateTransform

Rename keys according to the mappings.

Source

pub fn merge(keys: &[&str], into: &str) -> StateTransform

Merge the specified keys into a single key as an object.

Source

pub fn defaults(defaults: Value) -> StateTransform

Set default values for missing keys.

Source

pub fn map(f: impl Fn(&mut Value) + Send + Sync + 'static) -> StateTransform

Apply a custom transformation function.

Source

pub fn flatten(key: &str) -> StateTransform

Flatten a nested object key into the top level.

Source

pub fn set(key: &str, value: Value) -> StateTransform

Set a key to a fixed value.

Source

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"))
Source

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"))
Source

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"))
Source

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"]))
Source

pub fn transform( key: &str, f: impl Fn(Value) -> Value + Send + Sync + 'static, ) -> StateTransform

Transform a single key’s value with a function.

Source

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.

Source

pub fn compute( key: &str, f: impl Fn(&Value) -> Value + Send + Sync + 'static, ) -> StateTransform

Compute derived values from state.

Source

pub fn accumulate(source_key: &str, into: &str) -> StateTransform

Accumulate values into a list under a target key.

Source

pub fn counter(key: &str, step: i64) -> StateTransform

Increment a counter key by a step.

Source

pub fn require(keys: &[&str]) -> StateTransform

Require that specified keys exist.

Source

pub fn identity() -> StateTransform

Identity transform — no-op passthrough.

Source

pub fn when( predicate: impl Fn(&Value) -> bool + Send + Sync + 'static, inner: StateTransform, ) -> StateTransform

Conditional transform — applies inner transform only when predicate is true.

Source

pub fn drop(keys: &[&str]) -> StateTransform

Drop the specified keys.

Source

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")]);
Source

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");
Source

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");
Source

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");
Source

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"
Source

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"}
    }
}));
Source

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")),
);

Auto Trait Implementations§

§

impl Freeze for S

§

impl RefUnwindSafe for S

§

impl Send for S

§

impl Sync for S

§

impl Unpin for S

§

impl UnwindSafe for S

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more