tilo-resource 0.5.0

The Tilo resource layer: typed, hierarchical app resources.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
//! Derived resources: things computed from a record's state that people act
//! on (the folders of a snippet library, its tags, a smart list), published
//! as children whose actions are the record's own verbs with some arguments
//! bound.
//!
//! ```ignore
//! #[resource(kind = "snippets.library", children = Snippets::folders)]
//! impl Snippets { /* … */ }
//!
//! impl Snippets {
//!     fn folders(&self) -> Vec<Derived<Self>> {
//!         self.folder_names().map(|name| {
//!             Derived::new(&name).with(
//!                 Snippets::NEW_SNIPPET.bind(snippets::NewSnippet::paths().folder, name.clone()),
//!             )
//!         }).collect()
//!     }
//! }
//! ```

use std::any::TypeId;
use std::cell::RefCell;
use std::collections::HashMap;
use std::marker::PhantomData;
use std::rc::Rc;

use serde::Serialize;

use crate::action::ActionRef;
use crate::path::{Path, Segment};
use crate::resource::{ActionDecl, Resource};
use crate::vocabulary::Declaration;

/// A verb with some of its parameters bound: what a derived resource offers.
pub struct Bound<R> {
    verb: BoundVerb,
    marker: PhantomData<fn() -> R>,
}

/// A [`Bound`] without its resource type, so a [`Place`] holding it is
/// compared and published by code compiled once.
#[derive(Clone, Debug, PartialEq)]
struct BoundVerb {
    action: &'static str,
    values: Vec<(String, serde_json::Value)>,
}

impl<R> Clone for Bound<R> {
    fn clone(&self) -> Self {
        Self {
            verb: self.verb.clone(),
            marker: PhantomData,
        }
    }
}

impl<R> PartialEq for Bound<R> {
    fn eq(&self, other: &Self) -> bool {
        self.verb == other.verb
    }
}

impl<R> std::fmt::Debug for Bound<R> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Bound")
            .field("action", &self.verb.action)
            .field("values", &self.verb.values)
            .finish()
    }
}

impl<R> Bound<R> {
    fn new(action: &'static str, values: Vec<(String, serde_json::Value)>) -> Self {
        Self {
            verb: BoundVerb { action, values },
            marker: PhantomData,
        }
    }

    /// Bind one more parameter.
    #[must_use]
    pub fn and<P, V: Serialize>(mut self, param: Path<P, V>, value: V) -> Self {
        self.verb.values.push(bound_value(param, &value));
        self
    }

    pub fn action(&self) -> &'static str {
        self.verb.action
    }

    pub fn values(&self) -> &[(String, serde_json::Value)] {
        &self.verb.values
    }
}

impl<R: Resource> Bound<R> {
    /// This verb, its bound arguments filled, as a declaration of its own
    /// under `id`, named `label` (`word!("video-editor-split-here")`): what a
    /// surface offers bound to its view state ("split the clip at the
    /// playhead"), built and checked as the verb is.
    #[must_use]
    pub fn declaration(
        &self,
        id: &'static str,
        label: crate::label::Word,
    ) -> Option<ActionDecl<R>> {
        bound_declaration(
            crate::vocabulary::<R>(),
            self.verb.action,
            &self.verb.values,
            id,
            label,
        )
        .map(ActionDecl::declared)
    }
}

/// [`Bound::declaration`] of the verb `action` of the resource `vocabulary`
/// with `values` bound.
fn bound_declaration(
    vocabulary: &crate::Vocabulary,
    action: &str,
    values: &[(String, serde_json::Value)],
    id: &'static str,
    label: crate::label::Word,
) -> Option<Declaration> {
    let declarations = vocabulary.declarations();
    let verb = declarations.iter().find(|decl| decl.id == action)?;
    let mut params = Vec::new();
    for param in &verb.params {
        if !values.iter().any(|(name, _)| param.name == name.as_str()) {
            params.push(param.clone());
        }
    }
    let all = verb.params.clone();
    let build = std::sync::Arc::clone(&verb.build);
    let invokes = (
        verb.id.to_owned(),
        values
            .iter()
            .filter(|(_, value)| !value.is_null())
            .cloned()
            .collect(),
    );
    let values = values.to_vec();
    let build: crate::vocabulary::Builds = std::sync::Arc::new(move |args, now_ms| {
        let mut arguments = args.to_json();
        for (name, value) in &values {
            // An optional parameter bound to nothing is left out.
            if !value.is_null() {
                arguments.insert(name.clone(), value.clone());
            }
        }
        match tilo_actions::ActionArgs::bind_json(&all, &arguments) {
            Ok(args) => build(&args, now_ms),
            Err(error) => Err(error.to_string()),
        }
    });
    let mut decl = Declaration::new(vocabulary, id, label.source.to_owned(), params, build);
    decl.word = Some(label);
    // The same act with some of its arguments given: it wears the verb's
    // picture, and is found by the words people use for the verb.
    decl.icon.clone_from(&verb.icon);
    decl.keywords.clone_from(&verb.keywords);
    decl.invokes = Some(invokes);
    decl.kind = verb.kind;
    decl.field = verb.field;
    decl.description.clone_from(&verb.description);
    decl.destructive = verb.destructive;
    decl.step = verb.step;
    decl.creates = verb.creates;
    // Bound or not, the verb is undone the same way (or not at all), and
    // housekeeping stays housekeeping.
    decl.tags.extend(
        verb.tags
            .iter()
            .filter(|tag| {
                *tag == tilo_actions::COMPENSATED_UNDO_TAG
                    || *tag == tilo_actions::IRREVERSIBLE_TAG
                    || *tag == tilo_actions::HOUSEKEEPING_TAG
            })
            .cloned(),
    );
    Some(decl)
}

fn bound_value<P, V: Serialize>(param: Path<P, V>, value: &V) -> (String, serde_json::Value) {
    let segments = param.into_segments();
    let name = match segments.as_slice() {
        [Segment::Field(name)] => name.clone(),
        other => other
            .iter()
            .map(ToString::to_string)
            .collect::<Vec<_>>()
            .join("."),
    };
    (
        name,
        crate::__private::json_via_bytes(value).unwrap_or(serde_json::Value::Null),
    )
}

impl<R, P: Serialize> ActionRef<R, P> {
    /// This verb with the parameter at `param` (a path of its parameter
    /// struct, `NewSnippet::paths().folder`) bound to `value`.
    #[must_use]
    pub fn bind<V: Serialize>(&self, param: Path<P, V>, value: V) -> Bound<R> {
        Bound::new(self.id, vec![bound_value(param, &value)])
    }

    /// This verb with nothing bound: every parameter still asked, offered on
    /// a place as it is on the record (the terminal's verbs on its place).
    #[must_use]
    pub fn unbound(&self) -> Bound<R> {
        Bound::new(self.id, Vec::new())
    }
}

/// One derived resource. See the module docs.
pub struct Derived<R> {
    place: Place,
    marker: PhantomData<fn() -> R>,
}

/// A [`Derived`] without its resource type: what it publishes, compared and
/// published by code compiled once, in the runtime, rather than into every
/// app for each resource it declares.
#[derive(Clone)]
struct Place {
    id: String,
    /// `id` is the whole published address, not a name among its siblings
    /// beneath the record ([`Derived::addressed`]).
    addressed: bool,
    /// The field of the record it is an item of (`blocks`), published as
    /// `<record kind>/<field>/<id>`; `None` for `<record kind>/derived/<id>`.
    field: Option<String>,
    label: String,
    kind: Option<String>,
    /// The declared type it is an entity of ([`Derived::typed`]): what search
    /// names it by, as a document's item names its own type.
    words: Option<tilo_actions::ResourceType>,
    actions: Vec<BoundVerb>,
    effects: Vec<tilo_actions::ActionSpec>,
    /// What each of `effects` was made from, in order.
    offers: Vec<Offer>,
    /// Shared between copies, so a copy is as cheap as the resource alone
    /// and is told equal to its original without comparing its children.
    children: Rc<Vec<Place>>,
}

/// What an offered effect or edit of a [`Derived`] was made from: two equal
/// offers publish the same action.
#[derive(Clone, Debug, PartialEq)]
enum Offer {
    Effect {
        operation: &'static str,
        id: &'static str,
        arguments: serde_json::Map<String, serde_json::Value>,
        asking: Vec<String>,
    },
    Edit {
        id: &'static str,
        label: String,
        path: Vec<Segment>,
        op: crate::EditOp,
    },
    /// Made by a function of its caller's value, which cannot be compared:
    /// equal to no other.
    Asking,
    /// An operation stated for the row as it stands now, with its own label
    /// and availability ([`Derived::with_operation`]): compared by what it
    /// states, so an unchanged one is the resource published last.
    Stated {
        id: String,
        label: String,
        availability: tilo_actions::Availability,
    },
}

impl PartialEq for Place {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
            && self.addressed == other.addressed
            && self.field == other.field
            && self.label == other.label
            && self.kind == other.kind
            && self.words == other.words
            && self.actions == other.actions
            && self.offers == other.offers
            && !self.offers.contains(&Offer::Asking)
            && (Rc::ptr_eq(&self.children, &other.children) || self.children == other.children)
    }
}

impl Place {
    /// The child resource this is published as, among the derived resources
    /// of a record of `vocabulary`.
    fn publish(&self, vocabulary: &'static crate::Vocabulary) -> tilo_actions::ResourceSpec {
        self.publish_under(
            &format!(
                "{}/{}",
                vocabulary.kind,
                self.field.as_deref().unwrap_or("derived")
            ),
            vocabulary,
        )
    }

    fn publish_under(
        &self,
        parent: &str,
        vocabulary: &'static crate::Vocabulary,
    ) -> tilo_actions::ResourceSpec {
        let id = if self.addressed {
            self.id.clone()
        } else {
            format!("{parent}/{}", self.id)
        };
        // Built only when a verb is bound here: a long list of derived
        // places offering effects alone stays cheap to publish.
        let declarations = if self.actions.is_empty() {
            Vec::new().into()
        } else {
            vocabulary.declarations()
        };
        let kind = self.kind.as_deref().unwrap_or("derived");
        let mut child = tilo_actions::ResourceSpec::new(
            &id,
            // A kind that names its own namespace already is the whole kind;
            // a bare one (`book`) sits under the record's.
            if kind.contains('.') {
                kind.to_owned()
            } else {
                format!("{}.{kind}", vocabulary.kind)
            },
            &self.label,
        );
        for bound in &self.actions {
            if let Some(decl) = declarations.iter().find(|decl| decl.id == bound.action) {
                child = child.with_action(crate::items::bound(decl, &bound.values));
            }
        }
        for effect in &self.effects {
            child = child.with_action(effect.clone());
        }
        if let Some(words) = &self.words {
            child = child.with_type(words.clone());
        }
        child.with_children(
            self.children
                .iter()
                .map(|nested| nested.publish_under(&id, vocabulary)),
        )
    }
}

impl<R> Clone for Derived<R> {
    fn clone(&self) -> Self {
        Self {
            place: self.place.clone(),
            marker: PhantomData,
        }
    }
}

impl<R> PartialEq for Derived<R> {
    fn eq(&self, other: &Self) -> bool {
        self.place == other.place
    }
}

impl<R> Derived<R> {
    /// A derived resource named `label`, identified by it among its
    /// siblings.
    pub fn new(label: impl Into<String>) -> Self {
        let label = label.into();
        Self {
            place: Place {
                id: label.clone(),
                addressed: false,
                field: None,
                label,
                kind: None,
                words: None,
                actions: Vec::new(),
                effects: Vec::new(),
                offers: Vec::new(),
                children: Rc::default(),
            },
            marker: PhantomData,
        }
    }

    /// Nest `child` beneath it (a table's cells), identified within it.
    #[must_use]
    pub fn with_child(mut self, child: Self) -> Self {
        Rc::make_mut(&mut self.place.children).push(child.place);
        self
    }

    /// What kind of thing it is among the record's derived resources
    /// (`folder`, `tag`), published as `<record kind>.<kind>`; a kind that
    /// names its own namespace (`music.track`) is published as it is.
    #[must_use]
    pub fn of_kind(mut self, kind: impl Into<String>) -> Self {
        self.place.kind = Some(kind.into());
        self
    }

    /// Say what the place is as people name it: it is an entity of the
    /// declared row type `R`, so search names it by `R`'s words and
    /// [`LiveRow::declared`](crate::row::LiveRow::declared) publishes it as a
    /// row of that type.
    #[must_use]
    pub fn typed<T: crate::words::RowType>(mut self) -> Self {
        self.place.words = Some(T::words());
        self
    }

    /// Offer `spec`, an operation of the row as the row stands now: its
    /// label follows what it shows (`Play`/`Pause`), and one that cannot
    /// act offers its reason ([`tilo_actions::ActionSpec::unavailable`]).
    /// [`Self::with_effect`] offers the effect as its declaration reads.
    #[must_use]
    pub fn with_operation(mut self, spec: tilo_actions::ActionSpec) -> Self {
        self.place.offers.push(Offer::Stated {
            id: spec.id.clone(),
            label: spec.label.clone(),
            availability: spec.contract.availability.clone(),
        });
        self.place.effects.push(spec);
        self
    }

    /// Offer the effect `effect` on it, its arguments bound (a view that
    /// reveals the folder).
    #[must_use]
    pub fn with_effect<E: crate::Effect>(mut self, effect: &E) -> Self {
        self.place.effects.push(effect.bound());
        self.place.offers.push(Offer::Effect {
            operation: E::OPERATION,
            id: effect.id(),
            arguments: effect.arguments(),
            asking: Vec::new(),
        });
        self
    }

    /// Offer the effect `effect` on it with the arguments named in `asking`
    /// left to its caller, the rest bound (select a passage of *this* book,
    /// from a chapter the caller names).
    #[must_use]
    pub fn with_effect_asking<E: crate::Effect>(mut self, effect: &E, asking: &[&str]) -> Self {
        self.place.effects.push(effect.bound_asking(asking));
        self.place.offers.push(Offer::Effect {
            operation: E::OPERATION,
            id: effect.id(),
            arguments: effect.arguments(),
            asking: asking.iter().map(|name| (*name).to_owned()).collect(),
        });
        self
    }

    /// Identify it by `id` rather than its label, when the label can change.
    #[must_use]
    pub fn identified(mut self, id: impl Into<String>) -> Self {
        self.place.id = id.into();
        self
    }

    /// Publish it under `id` as a whole rather than as a name among the
    /// record's derived resources: for a row that already has an address of
    /// its own, which a search hit, a script or an agent names as it was
    /// named before (`music.track:<id>`).
    #[must_use]
    pub fn addressed(mut self, id: impl Into<String>) -> Self {
        self.place.id = id.into();
        self.place.addressed = true;
        self
    }

    /// Publish it as an item of the record's `field` (a document's
    /// paragraphs, `blocks`), as `<record kind>/<field>/<id>`: what a place
    /// parameter naming that field takes (`place = "blocks"`).
    #[must_use]
    pub fn in_field(mut self, field: impl Into<String>) -> Self {
        self.place.field = Some(field.into());
        self
    }

    /// Offer `action` on it.
    #[must_use]
    pub fn with(mut self, action: Bound<R>) -> Self {
        self.place.actions.push(action.verb);
        self
    }

    pub fn id(&self) -> &str {
        &self.place.id
    }

    pub fn label(&self) -> &str {
        &self.place.label
    }
}

impl<R: Resource> Derived<R> {
    /// Offer the field edit `edit` on it as the action `id` labelled
    /// `label`, its value bound ("Use Proton for Adobe Reader" on the app
    /// it routes): the same edit, checked and recorded as any other.
    #[must_use]
    pub fn with_edit(
        mut self,
        id: &'static str,
        label: impl Into<String>,
        edit: crate::Edit<R>,
    ) -> Self
    where
        crate::Edit<R>: Send + Sync,
    {
        let label = label.into();
        self.place.offers.push(Offer::Edit {
            id,
            label: label.clone(),
            path: edit.path().to_vec(),
            op: edit.op().clone(),
        });
        let decl = ActionDecl::<R>::edit(id, label, edit);
        self.place
            .effects
            .push(crate::resource::declaration_spec(&decl));
        self
    }

    /// Offer a field edit whose one value its caller supplies
    /// ([`ActionDecl::edit_asking`]): "Rename playlist" on the playlist.
    #[must_use]
    pub fn with_edit_asking<V>(
        mut self,
        id: &'static str,
        label: impl Into<String>,
        param: &'static str,
        description: &'static str,
        make: impl Fn(V) -> crate::Edit<R> + Send + Sync + 'static,
    ) -> Self
    where
        V: serde::de::DeserializeOwned + crate::JsonSchema,
    {
        let decl = ActionDecl::<R>::edit_asking(id, label, param, description, make);
        self.place
            .effects
            .push(crate::resource::declaration_spec(&decl));
        self.place.offers.push(Offer::Asking);
        self
    }

    /// The child resource this is published as.
    #[must_use]
    pub fn publish(&self) -> tilo_actions::ResourceSpec {
        self.place.publish(crate::vocabulary::<R>())
    }

    /// The published resource `id` among `derived` and their children.
    #[must_use]
    pub fn find(derived: &[Self], id: &str) -> Option<tilo_actions::ResourceSpec> {
        let vocabulary = crate::vocabulary::<R>();
        derived
            .iter()
            .find_map(|entry| find_published(entry.place.publish(vocabulary), id))
    }
}

/// The resource `id` among `resource` and its children.
fn find_published(
    resource: tilo_actions::ResourceSpec,
    id: &str,
) -> Option<tilo_actions::ResourceSpec> {
    if resource.id.as_str() == id {
        return Some(resource);
    }
    resource
        .children
        .into_iter()
        .find_map(|child| find_published(child, id))
}

/// A record's nested resources ([`crate::child::record_nested_children`])
/// and derived ones ([`Derived`]) as published, with the declarations they
/// offer with arguments bound.
#[derive(Default)]
pub struct PublishedDerived {
    resources: Rc<[Rc<tilo_actions::ResourceSpec>]>,
    /// Each action id they offer, with the fewest parameters it takes on
    /// one of them.
    offered: HashMap<String, usize>,
}

impl PublishedDerived {
    /// The published resources, in order: each the very one published last
    /// while the derived resource it publishes is unchanged.
    #[must_use]
    pub fn resources(&self) -> &Rc<[Rc<tilo_actions::ResourceSpec>]> {
        &self.resources
    }

    /// Whether some resource offers the declaration `id` with `params`
    /// parameters with some of its arguments bound.
    #[must_use]
    pub fn offers_bound(&self, id: &str, params: usize) -> bool {
        self.offered.get(id).is_some_and(|&taken| taken < params)
    }

    /// Copies of the published resources, in order.
    #[must_use]
    pub fn to_resources(&self) -> Vec<tilo_actions::ResourceSpec> {
        self.resources
            .iter()
            .map(|resource| (**resource).clone())
            .collect()
    }

    /// The published resource `id` among them and their children.
    #[must_use]
    pub fn find(&self, id: &str) -> Option<tilo_actions::ResourceSpec> {
        fn walk<'a>(
            resource: &'a tilo_actions::ResourceSpec,
            id: &str,
        ) -> Option<&'a tilo_actions::ResourceSpec> {
            if resource.id.as_str() == id {
                return Some(resource);
            }
            resource.children.iter().find_map(|child| walk(child, id))
        }
        self.resources
            .iter()
            .find_map(|resource| walk(resource, id))
            .cloned()
    }
}

/// The derived resources of one record type, seen without that type.
trait Places {
    fn len(&self) -> usize;
    fn place(&self, index: usize) -> Option<&Place>;
    /// Where they are held, which stays theirs while they are kept.
    fn address(&self) -> *const ();
}

impl<R: 'static> Places for Rc<[Derived<R>]> {
    fn len(&self) -> usize {
        <[Derived<R>]>::len(self)
    }

    fn place(&self, index: usize) -> Option<&Place> {
        self.get(index).map(|derived| &derived.place)
    }

    fn address(&self) -> *const () {
        Rc::as_ptr(self).cast()
    }
}

/// The derived resources a record type published last.
struct Kept {
    epoch: u64,
    derived: Rc<dyn Places>,
    published: Rc<PublishedDerived>,
}

thread_local! {
    static PUBLISHED: RefCell<HashMap<TypeId, Rc<Kept>>> = RefCell::new(HashMap::new());
}

/// The nested resources of the record `R` followed by `derived`, published:
/// the very publication of the last ones of the same resource type when
/// `derived` are the same ones or equal to them, so a change that leaves a
/// record's derived resources as they were publishes nothing again and hands
/// its publisher what it already holds. Otherwise each derived resource
/// equal to one published last is that very resource again, and only the
/// others are published. The nested resources depend on the type alone.
#[must_use]
pub fn published<R: Resource>(derived: impl Into<Rc<[Derived<R>]>>) -> Rc<PublishedDerived> {
    let derived: Rc<[Derived<R>]> = derived.into();
    published_places(
        TypeId::of::<R>(),
        crate::vocabulary::<R>(),
        Rc::new(derived),
    )
}

/// [`published`] of a record that derives no resources: its nested ones.
#[must_use]
pub fn published_nested<R: Resource>() -> Rc<PublishedDerived> {
    published_places(
        TypeId::of::<R>(),
        crate::vocabulary::<R>(),
        Rc::new(NoPlaces),
    )
}

/// The derived resources of a record that derives none.
struct NoPlaces;

impl Places for NoPlaces {
    fn len(&self) -> usize {
        0
    }

    fn place(&self, _index: usize) -> Option<&Place> {
        None
    }

    fn address(&self) -> *const () {
        std::ptr::null()
    }
}

/// [`published`] for the record type `of`, whose vocabulary is `vocabulary`.
fn published_places(
    of: TypeId,
    vocabulary: &'static crate::Vocabulary,
    derived: Rc<dyn Places>,
) -> Rc<PublishedDerived> {
    let epoch = crate::resource::words_epoch();
    let kept = PUBLISHED
        .with_borrow(|kept| kept.get(&of).cloned())
        .filter(|kept| kept.epoch == epoch);
    if let Some(kept) = kept.as_ref().filter(|kept| {
        std::ptr::eq(kept.derived.address(), derived.address()) || equal(&*kept.derived, &*derived)
    }) {
        return Rc::clone(&kept.published);
    }
    let resources: Rc<[Rc<tilo_actions::ResourceSpec>]> = match &kept {
        Some(kept) => {
            let last = &*kept.derived;
            let nested = kept.published.resources.len().saturating_sub(last.len());
            let at: HashMap<&str, usize> = (0..last.len())
                .filter_map(|index| Some((last.place(index)?.id.as_str(), index)))
                .collect();
            let published = |index: usize| {
                kept.published
                    .resources
                    .get(nested.saturating_add(index))
                    .cloned()
            };
            kept.published
                .resources
                .iter()
                .take(nested)
                .cloned()
                .chain((0..derived.len()).filter_map(|index| {
                    let next = derived.place(index)?;
                    let same = if last.place(index) == Some(next) {
                        Some(index)
                    } else {
                        at.get(next.id.as_str())
                            .copied()
                            .filter(|&at| last.place(at) == Some(next))
                    };
                    Some(
                        same.and_then(published)
                            .unwrap_or_else(|| Rc::new(next.publish(vocabulary))),
                    )
                }))
                .collect()
        }
        None => crate::child::nested_children_of(vocabulary)
            .into_iter()
            .map(Rc::new)
            .chain(
                (0..derived.len())
                    .filter_map(|index| derived.place(index))
                    .map(|next| Rc::new(next.publish(vocabulary))),
            )
            .collect(),
    };
    let mut offered = HashMap::new();
    for resource in resources.iter() {
        offered_on(resource, &mut offered);
    }
    let published = Rc::new(PublishedDerived { resources, offered });
    let kept = Rc::new(Kept {
        epoch,
        derived,
        published: Rc::clone(&published),
    });
    PUBLISHED.with_borrow_mut(|all| all.insert(of, kept));
    published
}

/// Whether `a` and `b` are equal, one by one.
fn equal(a: &dyn Places, b: &dyn Places) -> bool {
    a.len() == b.len() && (0..a.len()).all(|index| a.place(index) == b.place(index))
}

/// Note each action `resource` and its children offer in `offered`, with
/// the fewest parameters it takes on one of them.
fn offered_on(resource: &tilo_actions::ResourceSpec, offered: &mut HashMap<String, usize>) {
    for spec in &resource.actions {
        let taken = spec.params().len();
        offered
            .entry(spec.id.clone())
            .and_modify(|fewest| *fewest = (*fewest).min(taken))
            .or_insert(taken);
    }
    for child in &resource.children {
        offered_on(child, offered);
    }
}