tilo_actions/noun.rs
1//! What a resource is, in the words people use for it: its type's noun, the
2//! name of the app whose type it is, and whether it is that app's root.
3//!
4//! [`ResourceType`] is what search matches a query naming *what* something is
5//! against ("my mails", "notes", "youtube videos of mkbdc"), rather than its
6//! title. A tilolang type publishes one from its declaration; a Rust app's
7//! `#[resource]` type publishes the same one
8//! ([`tilo_resource::words::type_words`]), so the one search names either
9//! alike. The words are [`tilo_words`] for both: a type is called by its own
10//! name (`People` is one `person`, several `people`), and a noun English does
11//! not derive that an app words otherwise (a `Note` it calls a `notebook`) is
12//! the app's catalogue's to say.
13
14pub use tilo_words::{in_words, plural, singular};
15
16/// An app's name as a person reads it: the last part of its id in words,
17/// capitalised (`so.tilo.word-processor` is "Word processor").
18#[must_use]
19pub fn app_label(app: &str) -> String {
20 let last = app.rsplit('.').next().unwrap_or(app);
21 capitalised(&in_words(last)).unwrap_or_else(|| app.to_owned())
22}
23
24/// An action's id as the words a person reads (`append-paragraph` is
25/// "Append paragraph").
26#[must_use]
27pub fn action_label(action: &str) -> String {
28 capitalised(action.replace(['-', '_'], " ").trim()).unwrap_or_else(|| action.to_owned())
29}
30
31fn capitalised(words: &str) -> Option<String> {
32 let mut characters = words.chars();
33 let first = characters.next()?;
34 Some(first.to_uppercase().chain(characters).collect())
35}
36
37/// What one of something is called and what several are, in a sentence:
38/// `mailbox` and `mailboxes`, `person` and `people`.
39#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
40pub struct Noun {
41 pub one: String,
42 pub many: String,
43}
44
45impl Noun {
46 #[must_use]
47 pub fn new(one: impl Into<String>, many: impl Into<String>) -> Self {
48 Self {
49 one: one.into(),
50 many: many.into(),
51 }
52 }
53
54 /// What one of a type called `name` is called in a sentence, and what
55 /// several are: its name in words, lower case, in the singular and in the
56 /// plural (`Channel` is `channel` and `channels`, `WatchHistory` is
57 /// `watch history` and `watch histories`, `Tasks` is `task` and `tasks`).
58 ///
59 /// A noun an app words otherwise (a `Note` it calls a `notebook`) is the
60 /// source catalogue's to say.
61 #[must_use]
62 pub fn of_type(name: &str) -> Self {
63 let words = in_words(name);
64 Self {
65 many: plural(&words),
66 one: singular(&words),
67 }
68 }
69}
70
71/// What a resource is, in the words people use for it: its type's noun, the
72/// name of the app whose type it is, and whether it is that app's root.
73/// What search matches a query naming what something is ("my mails",
74/// "notes") against, rather than its title.
75#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
76pub struct ResourceType {
77 /// Its type: `mailbox`, `mailboxes`.
78 pub noun: Noun,
79 /// Its app, as people call it: `Mail`, `mails`.
80 pub app: Noun,
81 /// The app's root: its one resource of the type, which a search naming
82 /// the app means ("my mails" is Mail's root, placed through its place
83 /// view; "Mail" alone through its default view, the app's window).
84 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
85 pub root: bool,
86}
87
88#[cfg(test)]
89mod tests {
90 use super::*;
91
92 tilo_test_env::scenario! {
93 fn a_noun_reads_in_singular_and_plural;
94
95 phase a_declared_name_reads_as_words_and_its_plural;
96 {
97 assert_eq!(Noun::of_type("Channel"), Noun::new("channel", "channels"));
98 assert_eq!(Noun::of_type("Entry"), Noun::new("entry", "entries"));
99 assert_eq!(Noun::of_type("Box"), Noun::new("box", "boxes"));
100 assert_eq!(
101 Noun::of_type("Mailbox"),
102 Noun::new("mailbox", "mailboxes"),
103 "a `box` takes `es`"
104 );
105 assert_eq!(
106 Noun::of_type("Person"),
107 Noun::new("person", "people"),
108 "an irregular plural is English's, not an app's"
109 );
110 assert_eq!(
111 Noun::of_type("Conversations"),
112 Noun::new("conversation", "conversations"),
113 "a name that already names several is one of them and all of them"
114 );
115 }
116
117 phase a_plural_reads_back_to_its_singular;
118 {
119 for (many, one) in [
120 ("notes", "note"),
121 ("entries", "entry"),
122 ("boxes", "box"),
123 ("channels", "channel"),
124 ("history", "history"),
125 ] {
126 assert_eq!(singular(many), one, "{many} is several {one}");
127 }
128 }
129 }
130}