Rust

hook

Module

Hooks: callbacks with the params, injections, groups and labels they declare.

Contents

use utopia_servers::Arguments;
Rust
pub struct Arguments

What a callback receives: its arguments by name, in the order the server resolved them.

Rust
use std::sync::Arc;
use serde_json::json;
use utopia_servers::{Argument, Arguments};

let mut args = Arguments::new();
args.push("email", Argument::Value(json!("me@example.com")));
args.push("db", Argument::Resource(Arc::new(String::from("primary"))));
assert_eq!(args.value("email"), Some(&json!("me@example.com")));
assert_eq!(args.resource::<String>("db").map(String::as_str), Some("primary"));
use utopia_servers::Hook;
Rust
pub struct Hook

A callback with its description, params, injections, groups and labels.

Rust
use serde_json::json;
use utopia_servers::{Hook, Param};
use utopia_validators::Text;

let mut hook = Hook::new();
hook.inject("user").param("x", Param::new(json!(""), Text::new(10), "")).inject("time");
assert_eq!(hook.dependencies(), ["user", "time"]);
assert_eq!(hook.injections()["time"].order, 2);
Also implements Default, Debug.
use utopia_servers::Injection;
Rust
pub struct Injection

A resource a hook injects, and its position among the hook's params and injections.

use utopia_servers::Param;
Rust
pub struct Param

A param a hook declares: where its value comes from, how it is validated, and what documents it.

Rust
use serde_json::json;
use utopia_servers::Param;
use utopia_validators::Text;

let param = Param::new(json!(""), Text::new(64), "Project ID.")
    .optional(true)
    .aliases(&["project", "project_id"]);
assert!(param.optional);
assert_eq!(param.aliases, ["project", "project_id"]);
use utopia_servers::Argument;
Rust
pub enum Argument

One resolved argument of a callback.

Also implements Debug.
use utopia_servers::Check;
Rust
pub enum Check

A param's validator: an instance, or a factory called with its injections.

Also implements From, Debug.
use utopia_servers::ParamDefault;
Rust
pub enum Default

A param's default: a value, or a factory called with its injections.

Re-exported from the crate root as ParamDefault.

Also implements From, Debug.
Rust
pub type Action = Arc<dyn Fn(Arguments) -> BoxFuture<'static, Result<(), Error>> + Send + Sync>

A hook's callback: receives its resolved Arguments.

Rust
pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>

A boxed, sendable future, as hook callbacks return.

Rust
pub type DefaultFactory = Arc<dyn Fn(&[Resource]) -> Value + Send + Sync>

Builds a param's default from its injections.

Rust
pub type Resource = Arc<dyn Any + Send + Sync>

An injectable resource: any shared value.

Rust
pub type ValidatorFactory = Arc<dyn Fn(&[Resource]) -> Arc<dyn Validator> + Send + Sync>

Builds a param's validator from its injections.