tilo_resource/words.rs
1//! What a resource is called, for the resources its type publishes.
2//!
3//! Every resource says what it is ([`tilo_actions::ResourceType`]): its
4//! type's noun, its app's name, and whether it is that app's root. The type's
5//! noun is its own name in words, down to what a collection's items are. The
6//! app's name is not the type's to say: the framework app hosting the types
7//! names itself once ([`name_app`]), and every type it publishes, at any
8//! depth and whatever its kind, says it without writing anything.
9//!
10//! A kind names its app by its first part (`tube` in `tube.channel`,
11//! `file-explorer` in `file-explorer.entry`): the part every kind of one app
12//! shares, and the part that keeps two apps' kinds apart. A host that
13//! publishes a type none registered (a test's, a tool's) gets its app's name
14//! from [`tilo_actions::in_words`] of that part.
15//!
16//! **A noun an app words its own way is refined in the app's catalogue, never in
17//! code.** A type called by an ordinary English noun says it already (`Person` is a
18//! `person` and `people`, `Videos` a `video` and `videos`); one an app calls
19//! something else (a `Note` it calls a `notebook`) writes the kind's message with
20//! `$count` in its `locales/<lang>.ftl`, the same catalogue entry that names the
21//! type ([`crate::label`]); a noun no catalogue of theirs holds is the derived one.
22
23use std::collections::BTreeMap;
24use std::sync::{Mutex, OnceLock};
25
26use tilo_actions::{Noun, ResourceType};
27use tilo_lock::MutexRecover as _;
28
29/// What one app is called, and which kind is its root.
30#[derive(Clone, Debug)]
31struct AppWords {
32 /// Its name as people read it: `Tube`, `tubes`.
33 app: Noun,
34 /// The kind of its root: the one resource of it a workspace holds, which
35 /// a query naming the app means.
36 root: &'static str,
37}
38
39fn registry() -> &'static Mutex<BTreeMap<&'static str, AppWords>> {
40 static APPS: OnceLock<Mutex<BTreeMap<&'static str, AppWords>>> = OnceLock::new();
41 APPS.get_or_init(Mutex::default)
42}
43
44/// Name the app that publishes records of the kind `root` — its title, as
45/// people read it — for every kind the app declares: `tube` for
46/// `tube.library`, so `tube.channel` and `tube.video` say the same app.
47///
48/// Called by the framework app once, where it declares its types; calling it
49/// again with the same title is free, and with another (a launch naming the
50/// app otherwise) renames it from the next publication.
51pub fn name_app(title: &'static str, root: &'static str) {
52 let Some(segment) = segment_of(root) else {
53 return;
54 };
55 let mut app = registry().lock_recover();
56 let many = tilo_actions::plural(&title.to_lowercase());
57 app.insert(
58 segment,
59 AppWords {
60 app: Noun::new(title, many),
61 root,
62 },
63 );
64}
65
66/// The first part of a kind: the app it belongs to (`tube` in
67/// `tube.channel`).
68fn segment_of(kind: &'static str) -> Option<&'static str> {
69 let end = kind.find('.').unwrap_or(kind.len());
70 let segment = kind.get(..end)?;
71 (!segment.is_empty()).then_some(segment)
72}
73
74/// What the app of the kind `kind` is called, and whether `kind` is its
75/// root. A kind no app registered names its app by its first part in words.
76#[must_use]
77pub fn app_words_of(kind: &str) -> (Noun, bool) {
78 let segment = kind.split('.').next().unwrap_or(kind);
79 let words = registry().lock_recover().get(segment).cloned();
80 match words {
81 Some(words) => (words.app, words.root == kind),
82 None => (Noun::of_type(segment), false),
83 }
84}
85
86/// What a resource of `kind`, whose type is called `label` as people read it
87/// (a `#[resource]`'s `label`, e.g. `Channel`), is: the type's noun made of
88/// its name in words and refined by the app's catalogue
89/// ([`crate::label::localized_noun`]), its
90/// app's name, and whether it is that app's root. The app is the kind's
91/// first part, as [`app_words_of`] reads it.
92///
93/// The one place a Rust type's [`ResourceType`] is built, so a published
94/// type reads to search, the palette and agents exactly as a tilolang type
95/// does.
96#[must_use]
97pub fn type_words(kind: &str, label: &str) -> ResourceType {
98 let (app, root) = app_words_of(kind);
99 ResourceType {
100 noun: crate::label::localized_noun(kind, Noun::of_type(label)),
101 app,
102 root,
103 }
104}
105
106/// A declared resource type whose resources a host publishes from outside a
107/// document — the rows a surface shows that live outside it (a fetched
108/// catalogue's channels and videos, a process, a container). Its words are
109/// the type's, so such a row says what it is to search exactly as a record's
110/// item does, and what a press does is one of the type's actions or `open`.
111///
112/// Every [`Resource`](crate::Resource) is one (its kind and its label are its
113/// type's); a type whose state a document does not hold — Tube's fetched
114/// `Channel` and `Video`, which live in the catalogue, not a workspace record
115/// — implements it by hand with its kind and label, which is all a row needs
116/// before any record exists.
117pub trait RowType {
118 /// [`Resource::KIND`](crate::Resource::KIND): what a row of it is.
119 const KIND: &'static str;
120 /// [`Resource::LABEL`](crate::Resource::LABEL): its name as people read
121 /// it.
122 const LABEL: &'static str;
123
124 /// What a resource of it is, as people name it.
125 fn words() -> ResourceType {
126 type_words(Self::KIND, Self::LABEL)
127 }
128
129 /// The effects the type offers on each of its rows, each with its first
130 /// parameter bound to the row's id (`#[effect(on = Row)]`), or as it is
131 /// when it takes none. A row that is an item of a declared type offers
132 /// these, so what a press does on it is one of the type's effects,
133 /// published on every row of the type.
134 fn effects(_id: &crate::Id) -> Vec<tilo_actions::ActionSpec> {
135 Vec::new()
136 }
137
138 /// The effects the type offers on the row `id`, those `bindings` names
139 /// among them ([`RowBindings`](crate::row::RowBindings)): each with
140 /// exactly the arguments the row's state gives bound and the rest still
141 /// asked. This is [`Self::effects`] for a row whose entity is named by
142 /// more than its id (a saved network and whether it joins by itself),
143 /// offered only for the effects that apply to it now.
144 fn effects_with(
145 _id: &crate::Id,
146 _bindings: &crate::row::RowBindings,
147 ) -> Vec<tilo_actions::ActionSpec> {
148 Vec::new()
149 }
150}
151
152/// A `#[derive(Model)]` record's declaration is its type's: it publishes the
153/// record's words too.
154impl<T: crate::Resource> RowType for T {
155 const KIND: &'static str = T::KIND;
156 const LABEL: &'static str = T::LABEL;
157}
158
159/// What the items of the collection `field` of a resource of `kind` are: the
160/// collection's noun in the singular (`video` for a `videos` field), with its
161/// plural, and the app named as the resource names it. An item is not its
162/// app's root, and the app's catalogue may refine the noun as it may a
163/// type's.
164#[must_use]
165pub fn item_words(kind: &str, field: &str) -> ResourceType {
166 let (app, _) = app_words_of(kind);
167 let many = tilo_actions::in_words(field);
168 let one = tilo_actions::singular(&many);
169 ResourceType {
170 noun: crate::label::localized_noun(&format!("{kind}.{field}"), Noun::new(one, many)),
171 app,
172 root: false,
173 }
174}
175
176#[cfg(test)]
177mod tests {
178 use super::*;
179
180 tilo_test_env::scenario! {
181 fn apps_kinds_and_items_read_as_words;
182
183 phase an_app_names_every_kind_it_publishes;
184 {
185 name_app("Word Smith", "wordsmith.library");
186 let (app, root) = app_words_of("wordsmith.library");
187 assert_eq!(app, Noun::new("Word Smith", "word smiths"));
188 assert!(root, "the kind the app is named by is its root");
189 let (also, root) = app_words_of("wordsmith.document");
190 assert_eq!(also, app, "another kind of the app says the same app");
191 assert!(!root);
192 }
193
194 phase a_kind_no_app_named_reads_its_app_in_words;
195 {
196 let (app, root) = app_words_of("no-such-app.thing");
197 assert_eq!(app, Noun::new("no such app", "no such apps"));
198 assert!(!root);
199 }
200
201 phase a_type_reads_its_noun_from_its_name_in_words;
202 {
203 name_app("Tube", "tube.library");
204 let words = type_words("tube.channel", "Channel");
205 assert_eq!(words.noun, Noun::new("channel", "channels"));
206 assert_eq!(words.app, Noun::new("Tube", "tubes"));
207 assert!(!words.root);
208 assert!(type_words("tube.library", "Tube").root);
209 // A noun an app words otherwise is its catalogue's, never code's.
210 assert_eq!(
211 type_words("tube.person", "Person").noun,
212 Noun::new("person", "people"),
213 "an irregular plural English writes is derived"
214 );
215 }
216
217 phase an_items_noun_is_its_collections_in_the_singular;
218 {
219 name_app("Tube", "tube.library");
220 let words = item_words("tube.library", "videos");
221 assert_eq!(words.noun, Noun::new("video", "videos"));
222 assert_eq!(words.app, Noun::new("Tube", "tubes"));
223 assert!(!words.root);
224 }
225 }
226}