use std::collections::BTreeMap;
use std::sync::{Mutex, OnceLock};
use tilo_actions::{Noun, ResourceType};
use tilo_lock::MutexRecover as _;
#[derive(Clone, Debug)]
struct AppWords {
app: Noun,
root: &'static str,
}
fn registry() -> &'static Mutex<BTreeMap<&'static str, AppWords>> {
static APPS: OnceLock<Mutex<BTreeMap<&'static str, AppWords>>> = OnceLock::new();
APPS.get_or_init(Mutex::default)
}
pub fn name_app(title: &'static str, root: &'static str) {
let Some(segment) = segment_of(root) else {
return;
};
let mut app = registry().lock_recover();
let many = tilo_actions::plural(&title.to_lowercase());
app.insert(
segment,
AppWords {
app: Noun::new(title, many),
root,
},
);
}
fn segment_of(kind: &'static str) -> Option<&'static str> {
let end = kind.find('.').unwrap_or(kind.len());
let segment = kind.get(..end)?;
(!segment.is_empty()).then_some(segment)
}
#[must_use]
pub fn app_words_of(kind: &str) -> (Noun, bool) {
let segment = kind.split('.').next().unwrap_or(kind);
let words = registry().lock_recover().get(segment).cloned();
match words {
Some(words) => (words.app, words.root == kind),
None => (Noun::of_type(segment), false),
}
}
#[must_use]
pub fn type_words(kind: &str, label: &str) -> ResourceType {
let (app, root) = app_words_of(kind);
ResourceType {
noun: crate::label::localized_noun(kind, Noun::of_type(label)),
app,
root,
}
}
pub trait RowType {
const KIND: &'static str;
const LABEL: &'static str;
fn words() -> ResourceType {
type_words(Self::KIND, Self::LABEL)
}
fn effects(_id: &crate::Id) -> Vec<tilo_actions::ActionSpec> {
Vec::new()
}
fn effects_with(
_id: &crate::Id,
_bindings: &crate::row::RowBindings,
) -> Vec<tilo_actions::ActionSpec> {
Vec::new()
}
}
impl<T: crate::Resource> RowType for T {
const KIND: &'static str = T::KIND;
const LABEL: &'static str = T::LABEL;
}
#[must_use]
pub fn item_words(kind: &str, field: &str) -> ResourceType {
let (app, _) = app_words_of(kind);
let many = tilo_actions::in_words(field);
let one = tilo_actions::singular(&many);
ResourceType {
noun: crate::label::localized_noun(&format!("{kind}.{field}"), Noun::new(one, many)),
app,
root: false,
}
}
#[cfg(test)]
mod tests {
use super::*;
tilo_test_env::scenario! {
fn apps_kinds_and_items_read_as_words;
phase an_app_names_every_kind_it_publishes;
{
name_app("Word Smith", "wordsmith.library");
let (app, root) = app_words_of("wordsmith.library");
assert_eq!(app, Noun::new("Word Smith", "word smiths"));
assert!(root, "the kind the app is named by is its root");
let (also, root) = app_words_of("wordsmith.document");
assert_eq!(also, app, "another kind of the app says the same app");
assert!(!root);
}
phase a_kind_no_app_named_reads_its_app_in_words;
{
let (app, root) = app_words_of("no-such-app.thing");
assert_eq!(app, Noun::new("no such app", "no such apps"));
assert!(!root);
}
phase a_type_reads_its_noun_from_its_name_in_words;
{
name_app("Tube", "tube.library");
let words = type_words("tube.channel", "Channel");
assert_eq!(words.noun, Noun::new("channel", "channels"));
assert_eq!(words.app, Noun::new("Tube", "tubes"));
assert!(!words.root);
assert!(type_words("tube.library", "Tube").root);
assert_eq!(
type_words("tube.person", "Person").noun,
Noun::new("person", "people"),
"an irregular plural English writes is derived"
);
}
phase an_items_noun_is_its_collections_in_the_singular;
{
name_app("Tube", "tube.library");
let words = item_words("tube.library", "videos");
assert_eq!(words.noun, Noun::new("video", "videos"));
assert_eq!(words.app, Noun::new("Tube", "tubes"));
assert!(!words.root);
}
}
}