tilo-ui-graph 0.5.2

Pure schema and structural editor for Tilo serialized UI graphs
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
//! Component descriptions: what each described toolkit widget takes and does, as data.
//!
//! A description is generated from the widget's Rust definition (`#[derive(Component)]` in the
//! toolkit), so it cannot drift from the widget. It is read by everything that composes widgets
//! without being Rust: tilolang reads the catalogue as its package `ui`, the generic host adapter
//! builds widgets from it, and a serialized graph names component instances by it.
//!
//! A description holds props (typed values), parts (child content, typed by what may go in it),
//! events (data, never closures), bindings (two-way to a resource field, with the merge an edit is
//! applied with), state (what the widget keeps between frames, which is lens state) and semantics
//! (its accessibility role, and which props name it and the resource it shows). [`FORMAT_VERSION`]
//! is the shape of this format; [`Catalogue::toolkit`] the toolkit version it was generated from.

use serde::{Deserialize, Serialize};

/// The version of the description format: a change to the shape of [`Description`] bumps it.
pub const FORMAT_VERSION: u32 = 10;

/// The prop a component carries when its block gives such a part: the row offers to open into its
/// document, which comes once it is open.
pub const OPENS_PROP: &str = "opens";

/// Every described component of one toolkit version.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Catalogue {
    /// [`FORMAT_VERSION`] when it was written.
    pub format: u32,
    /// The toolkit version the descriptions come from.
    pub toolkit: String,
    pub components: Vec<Description>,
    /// The closed sets with fields a prop can name ([`Type::Cases`]): a drawing's shapes.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub enums: Vec<EnumDescription>,
    /// The toolkit's icon set, by kebab-case name (`more-horizontal`): what a prop of an icon
    /// may name, read by tilolang and the tools without linking the toolkit.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub icons: Vec<String>,
}

/// A closed set whose cases carry fields, as a tilolang `enum` with payloads reads it: the
/// toolkit declares it once, and every prop of [`Type::Cases`] names it.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct EnumDescription {
    pub name: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub doc: String,
    pub cases: Vec<CaseDescription>,
}

/// One case of an [`EnumDescription`], with its fields in order.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CaseDescription {
    pub name: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub doc: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub fields: Vec<FieldDescription>,
}

/// One field of a [`CaseDescription`].
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct FieldDescription {
    pub name: String,
    pub ty: Type,
    /// Its value when a case leaves it out; none when it is required.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub default: Option<Value>,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub doc: String,
}

impl Catalogue {
    /// The description of the component `name`.
    #[must_use]
    pub fn get(&self, name: &str) -> Option<&Description> {
        self.components.iter().find(|c| c.name == name)
    }

    /// The names of every component, for messages.
    #[must_use]
    pub fn names(&self) -> Vec<&str> {
        self.components.iter().map(|c| c.name.as_str()).collect()
    }

    /// Read a catalogue written by [`Self::to_json`].
    ///
    /// # Errors
    ///
    /// The text is not a catalogue, or one of another format version.
    pub fn from_json(text: &str) -> Result<Self, String> {
        let catalogue: Self = serde_json::from_str(text).map_err(|e| e.to_string())?;
        if catalogue.format != FORMAT_VERSION {
            return Err(format!(
                "component catalogue format {} is not the supported {FORMAT_VERSION}",
                catalogue.format
            ));
        }
        Ok(catalogue)
    }

    /// The catalogue as pretty JSON, one stable text per catalogue.
    #[must_use]
    pub fn to_json(&self) -> String {
        let mut text = serde_json::to_string_pretty(self).unwrap_or_default();
        text.push('\n');
        text
    }
}

/// The catalogue of the toolkit this crate ships with, as the toolkit generated it
/// (`components.json`; the toolkit's tests fail while it is stale).
///
/// # Panics
/// Never for the shipped file, which the toolkit's tests read back.
#[must_use]
pub fn catalogue() -> &'static Catalogue {
    static CATALOGUE: std::sync::OnceLock<Catalogue> = std::sync::OnceLock::new();
    CATALOGUE.get_or_init(|| {
        #[expect(
            clippy::expect_used,
            reason = "the shipped catalogue is written by the toolkit and read back by its tests"
        )]
        Catalogue::from_json(SHIPPED).expect("the shipped component catalogue reads")
    })
}

/// The text of the shipped catalogue.
pub const SHIPPED: &str = include_str!("../components.json");

/// One described component.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Description {
    /// The widget's name, as code calls it (`ListRow`).
    pub name: String,
    /// The widget's doc text.
    pub doc: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub props: Vec<Prop>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub parts: Vec<Part>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub events: Vec<Event>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub bindings: Vec<Binding>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub state: Vec<State>,
    pub semantics: Semantics,
}

impl Description {
    #[must_use]
    pub fn prop(&self, name: &str) -> Option<&Prop> {
        self.props.iter().find(|p| p.name == name)
    }

    #[must_use]
    pub fn part(&self, name: &str) -> Option<&Part> {
        self.parts.iter().find(|s| s.name == name)
    }

    /// The part a component's block fills when it names none.
    #[must_use]
    pub fn default_part(&self) -> Option<&Part> {
        self.parts.iter().find(|s| s.default)
    }

    #[must_use]
    pub fn event(&self, name: &str) -> Option<&Event> {
        self.events.iter().find(|e| e.name == name)
    }

    #[must_use]
    pub fn binding(&self, name: &str) -> Option<&Binding> {
        self.bindings.iter().find(|b| b.name == name)
    }

    #[must_use]
    pub fn state(&self, name: &str) -> Option<&State> {
        self.state.iter().find(|s| s.name == name)
    }

    /// The props given by position, in order.
    pub fn positional(&self) -> impl Iterator<Item = &Prop> {
        self.props.iter().filter(|p| p.positional)
    }

    /// The first line of the doc text, for messages.
    #[must_use]
    pub fn summary(&self) -> &str {
        first_line(&self.doc)
    }
}

/// The first line of a doc text.
#[must_use]
pub fn first_line(doc: &str) -> &str {
    doc.lines().next().unwrap_or("").trim()
}

/// What a companion says of the prop it completes.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Companion {
    /// `<name>:as`: which kind of text the prop holds (`date`, `time`), for the UI host to
    /// format it the person's way.
    Kind,
    /// `<name>:time`: the instant of the time of day the day the prop holds is due at, which
    /// reads with the day as one fact (`Today 9:00 AM`).
    Time,
}

/// The companion `name` is, and the prop it completes, when it is one: `value:as` says which kind
/// of text `value` holds, and `meta:time` the time of day the day `meta` is due at.
///
/// A companion is the UI host's: it reads it beside the prop it completes, so a component declares
/// the prop and never the companion. `None` for a name that is no companion.
#[must_use]
pub fn companion(name: &str) -> Option<(&str, Companion)> {
    let (prop, said) = name.rsplit_once(':')?;
    let companion = match said {
        "as" => Companion::Kind,
        "time" => Companion::Time,
        _ => return None,
    };
    Some((prop, companion))
}

/// A typed value a component takes.
#[allow(
    clippy::struct_excessive_bools,
    reason = "each flag is a separate part of the schema, set by its author"
)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Prop {
    pub name: String,
    pub doc: String,
    #[serde(rename = "type")]
    pub ty: Type,
    /// Whether every instance gives it.
    #[serde(default, skip_serializing_if = "is_false")]
    pub required: bool,
    /// Whether it may be given by position, in declaration order.
    #[serde(default, skip_serializing_if = "is_false")]
    pub positional: bool,
    /// Whether its text is data the component reads (a key, an address, an emoji) rather than
    /// words it shows: a view's literal given to it is never a message to translate.
    #[serde(default, skip_serializing_if = "is_false")]
    pub data: bool,
    /// Whether it names the value among its siblings (a list's chosen item, a row's key): a
    /// filter given to it stands for the query it asks, which the host compares by.
    #[serde(default, skip_serializing_if = "is_false")]
    pub identity: bool,
}

/// Child content a component takes.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Part {
    pub name: String,
    pub doc: String,
    /// What may go in it.
    pub accepts: Accepts,
    /// Whether it holds any number of children (else at most one).
    #[serde(default, skip_serializing_if = "is_false")]
    pub many: bool,
    /// Whether it is what the component's block fills when no part is named.
    #[serde(default, skip_serializing_if = "is_false")]
    pub default: bool,
    /// Whether it holds the document a row opens into: rendered only for the open row, so closed
    /// rows cost none of it. A component with such a part says so (`opens`) where it is built, so a
    /// row still offers to open.
    #[serde(default, skip_serializing_if = "is_false")]
    pub when_open: bool,
}

/// What a part accepts.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Accepts {
    View,
    /// Only the component of this name, whose props the part's owner reads.
    Component(String),
    /// Any of the components of these names, in any order (a menu's commands, separators and
    /// emoji bar), whose props the part's owner reads.
    OneOf(Vec<String>),
}

impl Accepts {
    /// Whether the part holds components whose props its owner reads, rather than views.
    #[must_use]
    pub fn reads_components(&self) -> bool {
        !matches!(self, Self::View)
    }

    /// Whether a component of the name `name` may stand in the part.
    #[must_use]
    pub fn takes(&self, name: &str) -> bool {
        match self {
            Self::View => true,
            Self::Component(want) => want == name,
            Self::OneOf(names) => names.iter().any(|want| want == name),
        }
    }

    /// What it takes, for messages: `a view`, `` `ui.MenuItem` `` or a list of them.
    #[must_use]
    pub fn show(&self) -> String {
        match self {
            Self::View => "a view".to_owned(),
            Self::Component(name) => format!("`ui.{name}`"),
            Self::OneOf(names) => names
                .iter()
                .map(|name| format!("`ui.{name}`"))
                .collect::<Vec<_>>()
                .join(", "),
        }
    }
}

/// Something that happens to a component. What it does is data: an action reference, invoked
/// through the action API, or a view-state update; never a closure.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Event {
    pub name: String,
    pub doc: String,
    /// The value the event carries (the key a list selected), when it carries one.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub value: Option<Type>,
    /// Where the value lands, when the event says: a drop's place among the items it was dropped
    /// on, counted from 0. An event with one carries `[value, at]`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub at: Option<Type>,
    /// Whether the event is the action of the control that raises it: a press runs the action the
    /// view bound to it, through the action API, so the press and an agent's invocation are the one
    /// route. An event that is not is the view's own: it is reported to the view.
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub press: bool,
}

/// A two-way binding to a resource field or view state.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Binding {
    pub name: String,
    pub doc: String,
    #[serde(rename = "type")]
    pub ty: Type,
    pub merge: Merge,
}

/// How an edit through a binding is applied to what it is bound to.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Merge {
    /// The value is replaced: a flag, a choice.
    Register,
    /// Text: the edit is spliced into it, never written over it, so concurrent edits merge.
    Splice,
    /// Rich text: its paragraphs spliced and its marks set, never written over.
    Rich,
}

/// What a widget keeps between frames (scroll, selection, caret, expansion). It is lens state:
/// personal, shown as presence, resumed on the person's other devices.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct State {
    pub name: String,
    pub doc: String,
    #[serde(rename = "type")]
    pub ty: Type,
}

/// How assistive technology and tests see a component.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Semantics {
    /// The accessibility role, as `accesskit` names it.
    pub role: String,
    /// The prop that names the component to assistive technology, when one does.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub label: Option<String>,
    /// The prop holding the resource the component presents, when it presents one.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub presents: Option<String>,
    /// The component draws nothing: it gives the view it stands in something to do (a context menu,
    /// a toolbar on hover, a swipe). Wherever a view's body puts it, it belongs to that view; a
    /// layout runs the menu a view places in it, and a list item holds its row's.
    #[serde(default, skip_serializing_if = "is_false")]
    pub attaches: bool,
    /// Why the gallery cannot show it (`#[component(not_in_gallery = "…")]` at the component's
    /// declaration): a component that only means something in a live session, such as a place's
    /// window well, is left out by its own declaration rather than by a name list in a check.
    /// `None` when the gallery can show it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub not_in_gallery: Option<String>,
}

/// The type of a prop, an event's value, a binding or a piece of state: the language's types.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Type {
    Text,
    Int,
    Float,
    Bool,
    /// A member: a person or an agent.
    Actor,
    /// A resource the component presents.
    Resource,
    /// A moment: a date and a time of day, as the language's `Time`.
    Time,
    /// One of the platform's icons, by its kebab-case name.
    Icon,
    /// A picture: where its bytes are read from, and what it shows the person.
    Picture,
    /// A file a person handed the window — dropped on it, or chosen — as the runtime's `File`:
    /// what it is called, its media type, its size and where its bytes are.
    File,
    /// Rich text, with how its references read for the person reading it.
    RichText,
    /// One of a closed set of cases: the design system's roles and tokens.
    Choice {
        name: String,
        cases: Vec<String>,
    },
    Option(Box<Type>),
    List(Box<Type>),
    /// Distinct values in no order the component relies on: who reacted with an emoji.
    Set(Box<Type>),
    /// Values by key: the people who reacted, by emoji.
    Map {
        key: Box<Type>,
        value: Box<Type>,
    },
    /// An action offered to the person, as a control shows it: what it is called, its icon and
    /// role, and what choosing it runs (an event of the instance, never a closure).
    Action,
    /// One case of the catalogue's closed set `name` whose cases carry fields: a drawing's shape.
    Cases {
        name: String,
    },
    /// A case of one of the app's own enums, by its name: which cases it holds is the field the
    /// binding names, so a description says only that it takes such a field.
    Enum,
    /// A record a view names, as the component offers it: what refers to it, and how it reads for
    /// the person now. What the component writes names the record, never the text, so a record
    /// renamed later reads by its new title everywhere it was linked.
    Link,
}

impl Type {
    /// How the type reads in messages: `Option<Text>`, `TextRole`.
    #[must_use]
    pub fn show(&self) -> String {
        match self {
            Self::Text => "Text".into(),
            Self::Int => "Int".into(),
            Self::Float => "Float".into(),
            Self::Bool => "Bool".into(),
            Self::Actor => "Actor".into(),
            Self::Resource => "a resource".into(),
            Self::Time => "Time".into(),
            Self::Icon => "Icon".into(),
            Self::Picture => "Picture".into(),
            Self::File => "File".into(),
            Self::RichText => "RichText".into(),
            Self::Choice { name, .. } | Self::Cases { name } => name.clone(),
            Self::Option(inner) => format!("Option<{}>", inner.show()),
            Self::List(inner) => format!("List<{}>", inner.show()),
            Self::Set(inner) => format!("Set<{}>", inner.show()),
            Self::Map { key, value } => format!("Map<{}, {}>", key.show(), value.show()),
            Self::Action => "Action".into(),
            Self::Enum => "a case of an enum".into(),
            Self::Link => "Link".into(),
        }
    }
}

/// A value of a [`Type`], as an instance of a component holds it.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Value {
    Text(String),
    Int(i64),
    Float(f64),
    Bool(bool),
    /// Who acted, what the workspace calls them, and the presence palette slot it paints them
    /// in (`None`: derived from `identity`).
    Actor {
        identity: tilo_action_policy::Actor,
        name: String,
        #[serde(default)]
        hue: Option<u8>,
        /// The address of their picture (a `data:` or `tilo-blob://` URL), when the workspace
        /// shows one; `None` shows their initials.
        #[serde(default)]
        picture: Option<String>,
    },
    /// A resource, by its id.
    Resource(String),
    /// An icon's kebab-case name.
    Icon(String),
    /// A picture: its address (where a host reads its bytes) and what it says to assistive
    /// technology.
    Picture(Picture),
    /// A file a person handed the window: its name, its media type, how many bytes it is and
    /// where they are (`tilo-blob://<hash>`), as the runtime's `File` value holds it.
    File(File),
    /// A case of a choice, by name.
    Choice(String),
    /// Rich text, with how its references read for the person reading it.
    Rich(crate::rich::RichText),
    /// An `Option` that holds nothing.
    None,
    /// A list, or a set's values in the order they were given.
    List(Vec<Value>),
    /// A map's entries, each key once, in key order.
    Map(Vec<(Value, Value)>),
    /// An action offered to the person.
    Action(Offered),
    /// A case of a [`Type::Cases`] set, with its fields' values in the case's order.
    Case {
        case: String,
        fields: Vec<(String, Value)>,
    },
    /// A record a view names: what refers to it (the catalogue's reference, `tilo_reference`),
    /// and how it reads for the person now (its title, or the phrase for one they cannot read).
    Link {
        reference: String,
        title: String,
    },
}

/// A picture a component shows, by where its bytes are. The address is opaque to the toolkit: a
/// window whose host holds a content store reads `tilo-blob://<hash>`, and any other address is
/// fetched by the store the picture loads through.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Picture {
    /// Where its bytes are read from: a content address (`tilo-blob://<hash>`) or a URL.
    pub src: String,
}

/// A file a person handed the window: dropped on it, or chosen in a chooser. Its bytes are in the
/// device's content store, and the value is what the runtime's `File` reads: its name, its media
/// type, its size and the content address its bytes are at.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct File {
    /// What the person's file is called, extension included (`Q3 plan.md`).
    pub name: String,
    /// Its media type (`text/markdown`).
    pub media: String,
    /// How many bytes its content is.
    pub size: i64,
    /// Where its bytes are: a content address (`tilo-blob://<hash>`).
    pub url: String,
}

/// An action offered to the person, as a control shows it, and the event of the instance that
/// choosing it fires. Every field is always written: the wire's encoding is not self-describing.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Offered {
    /// What it does, as a button or a menu names it.
    pub label: String,
    /// Its icon's kebab-case name, when it has one.
    pub icon: Option<String>,
    /// Whether it is its resource's primary action.
    pub primary: bool,
    /// Whether it takes something away: undone, not confirmed.
    pub destructive: bool,
    /// Whether the state it toggles is on, when it toggles one.
    pub on: Option<bool>,
    /// `key: "e"`: the plain key that runs it while its window has the keyboard (and, on a row's
    /// action, while that row is selected). `None` when it declares none.
    pub key: Option<String>,
    /// The event of the instance choosing it fires.
    pub event: String,
}

impl Value {
    /// The type the value is of, as far as the value says (`None` and an empty list say nothing).
    #[must_use]
    pub fn kind(&self) -> &'static str {
        match self {
            Self::Text(_) => "Text",
            Self::Int(_) => "Int",
            Self::Float(_) => "Float",
            Self::Bool(_) => "Bool",
            Self::Actor { .. } => "Actor",
            Self::Resource(_) => "a resource",
            Self::Icon(_) => "Icon",
            Self::Picture(_) => "a picture",
            Self::File(_) => "a file",
            Self::Choice(_) | Self::Case { .. } => "a case",
            Self::Rich(_) => "RichText",
            Self::None => "nothing",
            Self::List(_) => "List",
            Self::Map(_) => "Map",
            Self::Action(_) => "Action",
            Self::Link { .. } => "Link",
        }
    }
}

#[expect(
    clippy::trivially_copy_pass_by_ref,
    reason = "serde's skip_serializing_if passes a reference"
)]
fn is_false(flag: &bool) -> bool {
    !*flag
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn the_shipped_catalogue_reads_and_names_each_component_once() {
        let catalogue = catalogue();
        let mut names = catalogue.names();
        let all = names.len();
        names.sort_unstable();
        names.dedup();
        assert_eq!(names.len(), all, "a component is described once");
        assert!(catalogue.get("ListRow").is_some());
    }

    #[test]
    fn a_catalogue_of_another_format_is_refused() {
        // A catalogue otherwise as this build writes it, numbered as another format's.
        let mut other = catalogue().clone();
        other.format = FORMAT_VERSION + 1;
        let text = serde_json::to_string(&other).unwrap_or_default();
        assert!(Catalogue::from_json(&text).is_err());
    }
}