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};tomod_path- pluspub use {mod_name}::{export};whenexportisSome- creating the file if it doesn’t exist yet. A no-op if the module is already registered (re-running a generator after manually editingmod.rsshouldn’t duplicate the declaration). - generate_
file - Writes
{dir}/{mod_name}.rs(must not already exist) and registers it in{dir}/mod.rsaspub mod {mod_name};, pluspub use {mod_name}::{export};whenexportisSome- a struct name for controllers/models/requests, a function name for middleware.Nonefor a file with nothing nameable to re-export (a policy’s traitimplblock 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}.rsfromtemplate(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 ownmod.rs(pub mod {next_segment};, chained fromroot’s ownmod.rsdown) 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_segmentsempty is equivalent to callinggenerate_filedirectly. - 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.