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
//! What a resource is, in the words people use for it: its type's noun, the
//! name of the app whose type it is, and whether it is that app's root.
//!
//! [`ResourceType`] is what search matches a query naming *what* something is
//! against ("my mails", "notes", "youtube videos of mkbdc"), rather than its
//! title. A tilolang type publishes one from its declaration; a Rust app's
//! `#[resource]` type publishes the same one
//! ([`tilo_resource::words::type_words`]), so the one search names either
//! alike. The words are [`tilo_words`] for both: a type is called by its own
//! name (`People` is one `person`, several `people`), and a noun English does
//! not derive that an app words otherwise (a `Note` it calls a `notebook`) is
//! the app's catalogue's to say.
pub use tilo_words::{in_words, plural, singular};
/// An app's name as a person reads it: the last part of its id in words,
/// capitalised (`so.tilo.word-processor` is "Word processor").
#[must_use]
pub fn app_label(app: &str) -> String {
let last = app.rsplit('.').next().unwrap_or(app);
capitalised(&in_words(last)).unwrap_or_else(|| app.to_owned())
}
/// An action's id as the words a person reads (`append-paragraph` is
/// "Append paragraph").
#[must_use]
pub fn action_label(action: &str) -> String {
capitalised(action.replace(['-', '_'], " ").trim()).unwrap_or_else(|| action.to_owned())
}
fn capitalised(words: &str) -> Option<String> {
let mut characters = words.chars();
let first = characters.next()?;
Some(first.to_uppercase().chain(characters).collect())
}
/// What one of something is called and what several are, in a sentence:
/// `mailbox` and `mailboxes`, `person` and `people`.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct Noun {
pub one: String,
pub many: String,
}
impl Noun {
#[must_use]
pub fn new(one: impl Into<String>, many: impl Into<String>) -> Self {
Self {
one: one.into(),
many: many.into(),
}
}
/// What one of a type called `name` is called in a sentence, and what
/// several are: its name in words, lower case, in the singular and in the
/// plural (`Channel` is `channel` and `channels`, `WatchHistory` is
/// `watch history` and `watch histories`, `Tasks` is `task` and `tasks`).
///
/// A noun an app words otherwise (a `Note` it calls a `notebook`) is the
/// source catalogue's to say.
#[must_use]
pub fn of_type(name: &str) -> Self {
let words = in_words(name);
Self {
many: plural(&words),
one: singular(&words),
}
}
}
/// What a resource is, in the words people use for it: its type's noun, the
/// name of the app whose type it is, and whether it is that app's root.
/// What search matches a query naming what something is ("my mails",
/// "notes") against, rather than its title.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct ResourceType {
/// Its type: `mailbox`, `mailboxes`.
pub noun: Noun,
/// Its app, as people call it: `Mail`, `mails`.
pub app: Noun,
/// The app's root: its one resource of the type, which a search naming
/// the app means ("my mails" is Mail's root, placed through its place
/// view; "Mail" alone through its default view, the app's window).
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub root: bool,
}
#[cfg(test)]
mod tests {
use super::*;
tilo_test_env::scenario! {
fn a_noun_reads_in_singular_and_plural;
phase a_declared_name_reads_as_words_and_its_plural;
{
assert_eq!(Noun::of_type("Channel"), Noun::new("channel", "channels"));
assert_eq!(Noun::of_type("Entry"), Noun::new("entry", "entries"));
assert_eq!(Noun::of_type("Box"), Noun::new("box", "boxes"));
assert_eq!(
Noun::of_type("Mailbox"),
Noun::new("mailbox", "mailboxes"),
"a `box` takes `es`"
);
assert_eq!(
Noun::of_type("Person"),
Noun::new("person", "people"),
"an irregular plural is English's, not an app's"
);
assert_eq!(
Noun::of_type("Conversations"),
Noun::new("conversation", "conversations"),
"a name that already names several is one of them and all of them"
);
}
phase a_plural_reads_back_to_its_singular;
{
for (many, one) in [
("notes", "note"),
("entries", "entry"),
("boxes", "box"),
("channels", "channel"),
("history", "history"),
] {
assert_eq!(singular(many), one, "{many} is several {one}");
}
}
}
}