Skip to main content

resource

Attribute Macro resource 

#[resource]
Expand description

Turn a record’s #[action] methods into its replayable, published action vocabulary, and implement Resource for it.

ⓘ
#[resource(kind = "example.note", label = "Note")]
impl Note {
    /// Replace the note's text.
    #[action]
    pub fn edit(&mut self, #[param("Note text")] text: String) { self.body = text; }
}

Generates, per action, a parameter struct in a module named after the type (note::Edit { text }, with a JSON Schema derived from the parameter types), a variant of NoteAction carrying it, and Note::EDIT, an ActionRef views bind controls to and tests apply; then impl Resource. Field edits are a built-in variant.

An action method takes &mut self, optionally cx: &mut ActionCx and &mut Outbox<T> fields of the record (by field name: such an action reaches outside the document), then its parameters, and returns () or Result<(), impl Into<Refusal>>. Its public id is its name in kebab case unless #[action(id = "...")]; a parameter is published under its name unless #[param(name = "...")], and #[param("Description", example = value)] describes it.

An action whose first parameter is an item Id is also published on each item of the collection it acts on, with the id bound: the collection named like the parameter, or the record’s only one; #[action(item = field)] names it otherwise, and anything else does not compile.

Action options: label = "...", destructive (people confirm it, through any door), housekeeping (a change about the record rather than to it, such as a read marker), external (content from outside, nobody’s undo step), field = "..." (an action that writes only that field is placed below it, so a grant on the field reaches it: someone who may only comment may add to the comments).

Resource options: label, params = module (the parameter module’s name), initialize = path (fn() -> Self, the deterministic genesis), seed = path (fn(&mut Self, &mut ActionCx), the creator’s first content), available = path (fn(&Self, module::Verb) -> Availability), children = path (fn(&Self) -> Vec<Derived<Self>>), search = path, scopes = path, read_content = path (fn(&Self, Option<&Id>) -> Result<serde_json::Value, String>) and views = path.