use anyhow::{Context, Result};
use std::path::Path;
pub fn generate_item(
name: &str,
dir: &Path,
kind: &str,
template: &str,
extra_replacements: &[(&str, &str)],
) -> Result<()> {
validate_identifier(name)?;
let mod_name = to_snake_case(name);
let mut content = template.replace("__NAME__", name);
for (placeholder, value) in extra_replacements {
content = content.replace(placeholder, value);
}
generate_file(dir, &mod_name, kind, &content, Some(name))
}
pub fn generate_file(
dir: &Path,
mod_name: &str,
kind: &str,
content: &str,
export: Option<&str>,
) -> Result<()> {
anyhow::ensure!(
dir.is_dir(),
"no {} directory found here (run this from a Larust app's root)",
dir.display()
);
let path = dir.join(format!("{mod_name}.rs"));
anyhow::ensure!(
!path.exists(),
"a {kind} already exists at {}",
path.display()
);
std::fs::write(&path, content).with_context(|| format!("writing {}", path.display()))?;
if let Err(err) = append_to_mod_rs(&dir.join("mod.rs"), mod_name, export) {
let _ = std::fs::remove_file(&path);
return Err(err);
}
println!("Created {kind}: {}", path.display());
Ok(())
}
pub fn generate_nested_file(
root: &Path,
path_segments: &[String],
mod_name: &str,
kind: &str,
content: &str,
export: Option<&str>,
) -> Result<()> {
let mut dir = root.to_path_buf();
for segment in path_segments {
let parent_mod_rs = dir.join("mod.rs");
std::fs::create_dir_all(&dir).with_context(|| format!("creating {}", dir.display()))?;
append_to_mod_rs(&parent_mod_rs, segment, None)?;
dir = dir.join(segment);
}
std::fs::create_dir_all(&dir).with_context(|| format!("creating {}", dir.display()))?;
generate_file(&dir, mod_name, kind, content, export)
}
pub fn append_to_mod_rs(mod_path: &Path, mod_name: &str, export: Option<&str>) -> Result<()> {
let mut existing = std::fs::read_to_string(mod_path).unwrap_or_default();
let mod_line = format!("pub mod {mod_name};");
if existing.contains(&mod_line) {
return Ok(());
}
if !existing.is_empty() && !existing.ends_with('\n') {
existing.push('\n');
}
existing.push_str(&mod_line);
existing.push('\n');
if let Some(export) = export {
existing.push_str(&format!("\npub use {mod_name}::{export};\n"));
}
std::fs::write(mod_path, existing).with_context(|| format!("writing {}", mod_path.display()))
}
const RUST_KEYWORDS: &[&str] = &[
"as", "break", "const", "continue", "crate", "else", "enum", "extern", "false", "fn", "for",
"if", "impl", "in", "let", "loop", "match", "mod", "move", "mut", "pub", "ref", "return",
"self", "Self", "static", "struct", "super", "trait", "true", "type", "unsafe", "use", "where",
"while", "async", "await", "dyn", "abstract", "become", "box", "do", "final", "macro",
"override", "priv", "typeof", "unsized", "virtual", "yield", "try",
];
pub(crate) fn is_rust_keyword(name: &str) -> bool {
let snake = to_snake_case(name);
RUST_KEYWORDS.iter().any(|kw| *kw == name || *kw == snake)
}
pub fn validate_identifier(name: &str) -> Result<()> {
let mut chars = name.chars();
let starts_ok = matches!(chars.next(), Some(c) if c.is_ascii_alphabetic() || c == '_');
let rest_ok = chars.all(|c| c.is_ascii_alphanumeric() || c == '_');
anyhow::ensure!(
starts_ok && rest_ok,
"invalid name `{name}`: must be a valid Rust identifier (letters, digits, underscore; can't start with a digit)"
);
anyhow::ensure!(
!(name.starts_with("__") && name.ends_with("__")),
"invalid name `{name}`: names shaped like `__WORD__` collide with this generator's own template placeholders"
);
let snake = to_snake_case(name);
if let Some(keyword) = RUST_KEYWORDS
.iter()
.find(|kw| **kw == name || **kw == snake)
{
anyhow::bail!(
"invalid name `{name}`: `{keyword}` is a Rust keyword and can't be used as a generated identifier"
);
}
Ok(())
}
pub fn to_snake_case(name: &str) -> String {
let mut result = String::with_capacity(name.len() + 4);
for (i, c) in name.chars().enumerate() {
if c.is_uppercase() {
if i != 0 {
result.push('_');
}
result.extend(c.to_lowercase());
} else {
result.push(c);
}
}
result
}
pub fn pluralize(word: &str) -> String {
let preceded_by_vowel = word
.len()
.checked_sub(2)
.and_then(|i| word.as_bytes().get(i))
.is_some_and(|b| matches!(b, b'a' | b'e' | b'i' | b'o' | b'u'));
if word.ends_with('y') && !preceded_by_vowel {
format!("{}ies", &word[..word.len() - 1])
} else if word.ends_with("ss")
|| word.ends_with('x')
|| word.ends_with('z')
|| word.ends_with("ch")
|| word.ends_with("sh")
{
format!("{word}es")
} else if word.ends_with('s') {
word.to_string()
} else {
format!("{word}s")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn validate_identifier_accepts_pascal_case() {
assert!(validate_identifier("PostController").is_ok());
assert!(validate_identifier("_Leading").is_ok());
}
#[test]
fn validate_identifier_rejects_invalid_names() {
assert!(validate_identifier("").is_err());
assert!(validate_identifier("Foo Bar").is_err());
assert!(validate_identifier("123Foo").is_err());
assert!(validate_identifier("Foo-Bar").is_err());
}
#[test]
fn validate_identifier_rejects_keywords_used_directly() {
assert!(validate_identifier("type").is_err());
assert!(validate_identifier("self").is_err());
assert!(validate_identifier("Self").is_err());
}
#[test]
fn validate_identifier_rejects_names_that_snake_case_into_a_keyword() {
assert!(validate_identifier("Type").is_err());
assert!(validate_identifier("Move").is_err());
assert!(validate_identifier("Use").is_err());
}
#[test]
fn validate_identifier_rejects_names_shaped_like_a_template_placeholder() {
assert!(validate_identifier("__NAME__").is_err());
assert!(validate_identifier("__USER__").is_err());
}
#[test]
fn to_snake_case_converts_pascal_case() {
assert_eq!(to_snake_case("PostController"), "post_controller");
assert_eq!(to_snake_case("Post"), "post");
}
#[test]
fn pluralize_handles_common_cases() {
assert_eq!(pluralize("post"), "posts");
assert_eq!(pluralize("category"), "categories");
assert_eq!(pluralize("box"), "boxes");
assert_eq!(pluralize("class"), "classes");
assert_eq!(pluralize("day"), "days");
}
#[test]
fn pluralize_treats_a_word_already_ending_in_a_bare_s_as_already_plural() {
assert_eq!(pluralize("blogs"), "blogs");
assert_eq!(pluralize("terms"), "terms");
assert_eq!(pluralize("bus"), "bus");
}
#[test]
fn generate_file_cleans_up_orphaned_rs_file_if_mod_rs_write_fails() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path();
let mod_path = dir.join("mod.rs");
std::fs::write(&mod_path, "").unwrap();
let original_perms = std::fs::metadata(&mod_path).unwrap().permissions();
let mut readonly_perms = original_perms.clone();
readonly_perms.set_readonly(true);
std::fs::set_permissions(&mod_path, readonly_perms).unwrap();
let result = generate_file(dir, "thing", "widget", "pub struct Thing;\n", Some("Thing"));
std::fs::set_permissions(&mod_path, original_perms).unwrap();
assert!(result.is_err());
assert!(
!dir.join("thing.rs").exists(),
"orphaned .rs file should be cleaned up when registering it in mod.rs fails"
);
}
#[test]
fn generate_nested_file_creates_every_intermediate_directory_and_mod_rs() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
std::fs::write(root.join("mod.rs"), "").unwrap();
generate_nested_file(
root,
&["pages".to_string(), "webservices".to_string()],
"compare",
"widget",
"pub struct Compare;\n",
Some("Compare"),
)
.unwrap();
assert!(root.join("pages/webservices/compare.rs").exists());
assert_eq!(
std::fs::read_to_string(root.join("mod.rs")).unwrap(),
"pub mod pages;\n"
);
assert_eq!(
std::fs::read_to_string(root.join("pages/mod.rs")).unwrap(),
"pub mod webservices;\n"
);
let leaf_mod = std::fs::read_to_string(root.join("pages/webservices/mod.rs")).unwrap();
assert!(leaf_mod.contains("pub mod compare;"));
assert!(leaf_mod.contains("pub use compare::Compare;"));
}
#[test]
fn generate_nested_file_with_no_segments_behaves_like_generate_file() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
std::fs::write(root.join("mod.rs"), "").unwrap();
generate_nested_file(
root,
&[],
"home",
"widget",
"pub struct Home;\n",
Some("Home"),
)
.unwrap();
assert!(root.join("home.rs").exists());
let mod_rs = std::fs::read_to_string(root.join("mod.rs")).unwrap();
assert!(mod_rs.contains("pub mod home;"));
}
#[test]
fn generate_nested_file_is_idempotent_across_siblings_sharing_a_directory() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
std::fs::write(root.join("mod.rs"), "").unwrap();
generate_nested_file(
root,
&["pages".to_string()],
"blog",
"widget",
"pub struct Blog;\n",
Some("Blog"),
)
.unwrap();
generate_nested_file(
root,
&["pages".to_string()],
"about",
"widget",
"pub struct About;\n",
Some("About"),
)
.unwrap();
let mod_rs = std::fs::read_to_string(root.join("mod.rs")).unwrap();
assert_eq!(mod_rs.matches("pub mod pages;").count(), 1);
let pages_mod = std::fs::read_to_string(root.join("pages/mod.rs")).unwrap();
assert!(pages_mod.contains("pub mod blog;"));
assert!(pages_mod.contains("pub mod about;"));
}
}