Skip to main content

doido_generators/generators/
mod.rs

1pub mod channel;
2pub mod controller;
3pub mod field;
4pub mod generator_gen;
5pub mod job;
6pub mod locale;
7pub mod mailer;
8pub mod migration;
9pub mod migration_support;
10pub mod model;
11pub mod new;
12pub mod resource;
13pub mod scaffold;
14pub mod storage_adapter;
15pub mod storage_install;
16pub mod templates_gen;
17
18use doido_core::Inflector;
19
20/// `BlogPost`/`blog-post` → `blog_post`.
21pub fn to_snake(s: &str) -> String {
22    Inflector::underscore(s)
23}
24
25/// Insert `pub mod <module>;` into a directory `mod.rs`, just above `marker`
26/// (appending if the marker is absent). Idempotent: an already-registered module
27/// leaves the file unchanged. Shared by the job/mailer/channel generators.
28pub(crate) fn register_module(existing: &str, module: &str, marker: &str) -> String {
29    let decl = format!("pub mod {module};");
30    if existing.lines().any(|l| l.trim() == decl) {
31        return existing.to_string();
32    }
33    let mut lines: Vec<String> = existing.lines().map(String::from).collect();
34    match lines.iter().position(|l| l.contains(marker)) {
35        Some(i) => lines.insert(i, decl),
36        None => lines.push(decl),
37    }
38    let mut out = lines.join("\n");
39    out.push('\n');
40    out
41}
42
43/// `blog_post`/`blog-post` → `BlogPost`.
44pub fn to_pascal(s: &str) -> String {
45    // Normalise dashes/casing first so `camelize` (which splits on `_`) sees
46    // clean snake_case input.
47    Inflector::camelize(&Inflector::underscore(s))
48}
49
50/// `BlogPost`/`blog_post` → `blog_posts` — the pluralized, snake_cased table
51/// name, honouring any custom rules from `config/inflection.yaml`.
52pub fn to_table_name(s: &str) -> String {
53    Inflector::tableize(s)
54}