Skip to main content

doido_generators/generators/
mod.rs

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