Skip to main content

Module codegen

Module codegen 

Source
Expand description

Shared “write a generated file and wire it into the module tree” primitives, used by both xr make:* (larust-cli::generate) and xr convert (this crate’s own converters). Originally lived as private functions in larust-cli::generate - moved here, pub, because larust-cli depends on larust-convert (not the reverse), so a new crate generating controllers/models/etc. from a Laravel app couldn’t reach them as private functions in a crate that depends on it. One source of truth for the real edge-case handling here (rollback-on- failure, placeholder-collision guards) beats a second copy nothing keeps in sync.

Functions§

append_to_mod_rs
Adds pub mod {mod_name}; to mod_path - plus pub use {mod_name}::{export}; when export is Some - creating the file if it doesn’t exist yet. A no-op if the module is already registered (re-running a generator after manually editing mod.rs shouldn’t duplicate the declaration).
generate_file
Writes {dir}/{mod_name}.rs (must not already exist) and registers it in {dir}/mod.rs as pub mod {mod_name};, plus pub use {mod_name}::{export}; when export is Some - a struct name for controllers/models/requests, a function name for middleware. None for a file with nothing nameable to re-export (a policy’s trait impl block is globally visible once compiled in; no re-export needed).
generate_item
Generates a struct/type-shaped item (controller/model/request): writes {dir}/{snake_name}.rs from template (with __NAME__/other placeholders substituted) and registers it in {dir}/mod.rs.
generate_nested_file
The nested-directory equivalent of generate_file: writes {root}/{path_segments.join("/")}/{mod_name}.rs, creating every intermediate directory and wiring each one’s own mod.rs (pub mod {next_segment};, chained from root’s own mod.rs down) along the way - for a converted structure whose Laravel source used real subdirectories (App\Livewire\Pages\Webservices\Compare) rather than a flat namespace, so the generated output can mirror that instead of flattening it into one prefixed filename. path_segments empty is equivalent to calling generate_file directly.
pluralize
A deliberately simple heuristic (not a full English pluralization engine) covering the common cases: post -> posts, category -> categories, box -> boxes. Good enough for a default table name; nothing stops a developer from editing #[table("...")] by hand for words this doesn’t handle correctly.
to_snake_case
Post -> "post", BlogPost -> "blog_post".
validate_identifier
Rejects names that aren’t valid Rust identifiers, that collide with a Rust keyword, or that look like one of this file’s own __NAME__-style template placeholders - before they get interpolated into generated source. Fail fast rather than emit unparseable (or, worse, silently cross-substituted) code.