Skip to main content

Module effect

Module effect 

Source
Expand description

Effects: work an app does that changes no document — reloading a list, running a job, talking to a server, opening a system picker.

An app names its effects as one typed enum and derives Effect:

use tilo_resource::Effect;

#[derive(Clone, Debug, PartialEq, Eq, Effect)]
#[effect(operation = "example.backups")]
pub enum Backups {
    /// Back up now.
    #[effect(kind = "outside")]
    #[effect(job, cancellable)]
    BackUp,
    /// Restore a snapshot.
    #[effect(kind = "outside")]
    #[effect(job, destructive)]
    Restore {
        #[param("Id of the snapshot")]
        snapshot: String,
    },
}

let restore = Backups::Restore { snapshot: "monday".into() };
assert_eq!(restore.id(), "restore");
let decoded = Backups::decode("restore", &restore.arguments()).unwrap();
assert!(matches!(decoded, Backups::Restore { snapshot } if snapshot == "monday"));

Each variant is one published action of kind Effect, with typed parameters, so the palette, agents and scripts invoke it like the app’s buttons do. Unlike a resource’s Action, an effect is never replayed: it runs where it was invoked, and what it changes in the document it changes through ordinary actions. tilo-app runs the handler.

A parameter naming a file or folder on this device is an AbsolutePath: a relative one is refused as the arguments are decoded, so no effect writes where its process happens to run.

A variant that only changes what this presence sees — open a snippet, reveal a tag, show a result — is a view: published as a View operation (any reader may invoke it, a member may not move someone else’s view, repeating it is harmless), answered by the same handler.

Who may run an effect is decided by its kind alone, like any action: an effect that reaches the files, hardware or services of the device it runs on — set this computer up, kill a process here, read a local folder — reaches outside the record, so the person’s Allow, Ask first or Deny for outside effects decides it.

An effect whose whole scope is the invoking person’s own device — copy to their clipboard, apply their own appearance, measure their own connection — and that touches nothing shared and no one else is own_device: a workspace’s grants have nothing to consent to, so a guest invokes it as the workspace’s owner does, and it runs only for the person at the device it acts on (tilo_actions::OWN_DEVICE_SCOPE). An effect that reaches other people (a direct message, a call) is not.

An effect that needs its person attending — it opens a chooser for them, asks them for a credential or a sign-in, or shows something only they may see — is attended: whoever acts for the person (an agent, a bot) is always asked first, and no answer allows it unasked (tilo_actions::ATTENDED_TAG).

An effect that acts on one item — open this snippet, connect this connection — is offered on the item itself with its arguments bound (Effect::bound): a published child or a live row carries it, and invoking it there reaches the handler as that value. Such an effect is often row: offered only bound, never on the resource with an id for the caller to type.

An effect’s label is a key of the catalogue beside its crate (locales/en-US.ftl): <operation>-<id> with the operation’s separators as dashes (notes-effect-export for export of notes.effect), or the key label = "…" names where the crate already says the same thing. Its doc comment is for developers and describes it to agents; it is never the label.

Variant options: label = "key", view (changes only what is shown; answers at once), document (a document operation the app’s own registry applies, as the history verbs are: its operation type is the action’s, never an operation the surface answers), prompted (asks its first parameter in the palette even where every parameter may be left out: a named verb whose optional argument a person still says), row (offered only bound to the items it acts on), job (runs in the background and reports progress, then its result), cancellable (a job the caller may cancel), returns (answers with a JSON value), content (answers with content another app’s operation takes: bytes in this device’s content store, named by their hash as a tilo.content resource; an operation that takes content collects it with ActionSpec::prompted_content, so a caller chains the two by type), destructive (asks for confirmation), risk = "path" (a fn(&Self) -> RiskLevel classifying one invocation: one it finds destructive asks for confirmation, so formatting the system disk can, and another disk need not), phrase = "path" (a fn(&Self) -> String naming what one bound invocation of a destructive row effect loses, such as the drive it erases: its caller types it to confirm, Effect::bound asks for it), privileged (hands out or shows a credential, such as copying a one-time code: asks for confirmation too), sensitive (discloses a copy of private data, such as exporting an address book: asks for confirmation too), idempotent (safe to repeat after an uncertain outcome), own_device (its scope is the invoker’s own device), attended (needs its person attending: anyone acting for them asks), audience = "agents" (listed for agents and programs, never in the palette; "people" lists it for people only), untrusted (answers with content from outside the user’s data, such as a web page’s text: the caller reasons about it and never follows it), opens_path (opens the file its path names in this instance, which is how an app launched with a file opens it; opens_path = "csv,tsv" only those types), presence (a view that moves only the invoking presence’s own view, such as an agent’s caret, so a remote presence may invoke it), member (any member of a workspace may ask the workspace’s host to run it for themselves on the workspace’s record, as linking their own account elsewhere), host_render (a host answers it in a render service of its own, never in the record’s confined app worker: tilo_actions::HOST_RENDER_TAG), tag = EXPR (repeatable: a further contract tag, a &'static str constant, for how a host treats the effect), outside_with = "path" (an effect of kind = "read" that reaches outside the record only when that parameter is given: an export that writes the file it names, else keeps what it made as content; each invocation is admitted as what it does), item = field (offered on each item of the collection, its first parameter bound to the item’s id, beside the item’s own verbs) or on = RowType (offered on each row of a #[derive(Row)] type, its first parameter bound to the row’s id; LiveRow::typed publishes them; write it more than once to offer the effect on several types, and leave the effect without parameters to offer it on the type as it is) and kind = "read" (an effect that only reads, which whoever may read may invoke), kind = "undoable" (an undoable change) or kind = "outside" (a change checked against the caller’s grants: every effect states its kind, while a view reads and an edit is undoable), and offload(cpu_ms = N, memory_mb = N, output_mb = N, gpu) (a content, kind = "read" transform of content inputs whose outputs depend on the inputs alone: the system may dispatch it to the invoker’s best-placed device, where the action runs through that device’s provider, and the handler is its local run; tilo_actions::Offload). Field #[param("…")] describes a parameter; #[param(secret, "…")] makes it a secret the host keeps out of logs, history and confirmations; #[param(decimal, "…")] makes it an identity that travels as canonical decimal text and is refused otherwise; and #[param("…", choices = EXPR)] names the values it takes when they are a fixed list (EXPR yields strings: a constant table, an array of names). #[param(label = "key")] takes the description from the vocabulary’s catalogue instead, read in the person’s language where the action is published.

A parameter’s Rust type describes its JSON form — it derives JsonSchema — because the surface publishes the parameter’s type, its schema and the records and enums it names, which other languages compile against. A type that describes none is refused where the effect is declared, naming the parameter and the type:

ⓘ
use serde::{Deserialize, Serialize};
use tilo_resource::Effect;

/// A value that says nothing about its JSON form.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Snapshot;

#[derive(Clone, Debug, PartialEq, Effect)]
#[effect(operation = "example.backups")]
pub enum Backups {
    /// Restore a snapshot.
    #[effect(kind = "outside")]
    Restore { snapshot: Snapshot },
}

The value a wrapper carries must describe itself too: a list, an option or a box of a type with no schema is refused as well, so a parameter can never be published with no type to say what it holds.

ⓘ
use serde::{Deserialize, Serialize};
use tilo_resource::Effect;

/// A value that says nothing about its JSON form.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Snapshot;

#[derive(Clone, Debug, PartialEq, Effect)]
#[effect(operation = "example.backups")]
pub enum Backups {
    /// Restore snapshots.
    #[effect(kind = "outside")]
    Restore { snapshots: Vec<Snapshot> },
}

Structs§

EffectDecl
The declaration of one effect: what callers see of it.
EffectOffload
An effect’s #[effect(offload(...))]: what the transform needs of a device, so the system may dispatch it to the invoker’s best-placed one (tilo_actions::Offload).
EffectResult
The value an effect answers with, as its code declares it (#[effect(returns = Type)]): its JSON Schema, and its type with the records and enums it names.

Enums§

Answers
How an effect answers its caller.
EffectDecodeError
Why a caller’s invocation does not name an effect of a vocabulary.
EffectTarget
What an effect is offered on, bound to it: a collection’s items, or a declared row type’s rows. The two are one thing — an effect offered on an entity of a declared type, with the entity’s id bound — declared either beside the document that holds the items, or on the type of rows that live outside any document.
NoEffect
The vocabulary of a surface that has no effects.

Constants§

EDITS_TAG
The contract tag of an effect that submits only edits of its record (EffectDecl::edits): its kind is Undoable.

Traits§

Effect
A typed vocabulary of effects. Derive it (#[derive(Effect)]); see the module docs.

Functions§

decode
The effect of E that invoking action of resource with arguments names, once the spec published offers for it checked the arguments and that it is available now. A spec offered bound to an item (a network, an accessory) adds the item’s arguments.
identity_argument
The identity argument name: canonical decimal text, as a record and a surface name their windows, strips, groups and workspaces by. A salted id outgrows what a JSON number carries exactly, so it travels as text; any other text names no id, and is refused here rather than taken for one.
offer
resource offering every action of the vocabulary E: a device’s readings and the desktop’s verbs are published this way.
optional_identity_argument
identity_argument for an identity that need not be given.

Type Aliases§

PhraseOf
The phrase a bound invocation’s caller types, from the arguments it is bound with: what one invocation loses.
RowIdOf
An effect’s row id read as the field it acts with: the value bound, or why this row’s id is not one.