Skip to main content

Crate larust_convert

Crate larust_convert 

Source
Expand description

Laravel-to-Larust conversion tooling behind xr convert. Never wired into larust-support’s facade - this is build-time/dev tooling, never a generated app’s own runtime dependency, so it lives outside the “one dependency surface” rule that governs everything apps depend on. See docs/ARCHITECTURE.md’s “Laravel conversion” section for the two core design decisions this whole crate is built around: third-party (composer) packages are never auto-ported, and PHP business logic is never auto-translated - only mechanically-regular structure is.

Modules§

assets
Copies the source Laravel app’s static asset files - public/ and the pre-build resources/css/resources/js source - into the converted project verbatim. Every converted Blade template already references these exact paths (/images/..., /css/app.css, /favicon.ico, …) unchanged (an asset path is a plain string literal, never PHP/Blade syntax blade::expr would translate), and Larust serves public/ directly at the URL root (ServeDir in larust-core’s Application) the same way Laravel’s own webserver docroot does - so without this phase, every converted page renders unstyled and imageless even though its own HTML is otherwise correct.
blade
resources/views/**/*.blade.php → resources/views/**/*.blade.xr. See docs/ARCHITECTURE.md’s “Laravel conversion” section for the whole-file (not per-item) safety rationale: crates/larust-macros/src/ view.rs’s view! macro consumes a template as one indivisible unit, so a bad directive or expression can’t be safely omitted mid-file - either the whole file translates cleanly, or none of it is trusted. That rationale still holds for most failures - see scan.rs’s own module doc comment for the one deliberate exception (a top-level @php block) and why it’s safe.
codegen
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.
composer
composer.json → third-party package report. No PHP parsing needed here - composer.json is plain JSON, read with serde_json (already a workspace dependency).
config
config/*.php → config/app.rs (the framework’s own typed bootstrap fields) plus config/{file}.rs (everything else). Laravel’s config system takes an arbitrary set of dotted keys across many files; Larust’s Config (crates/larust-core/src/config.rs) is a small, fixed, known struct - only [MAPPINGS]’s keys can become a typed Config field, so convert (the narrower of this module’s two converters) really does have to name a key “no known Config field” and leave it out of that struct.
constructor_props
Shared constructor-property extraction for events.rs/jobs.rs - both a converted Event and a converted Job are simple field-only structs whose fields come from the Laravel class’s own constructor. Real Laravel code uses two styles, both detected here: modern constructor-promoted properties (public function __construct(public Order $order) {}) and the older explicit-property-plus-assignment style (public $order; public function __construct(Order $order) { $this->order = $order; }) - both produce the same flat field list.
controllers
app/Http/Controllers/*.php → enriches Phase 1’s already-generated todo!() controller stubs (convert.rs’s generate_controller_stubs, driven by routes::referenced_controllers) with each stubbed method’s original PHP body preserved as a comment - no logic translation, no smarter parameter typing (both explicitly out of scope, natural later enhancements). If the real source controller file doesn’t exist, or a specific stubbed method isn’t found in it, that method just gets Phase 1’s existing bare stub - this module only ever adds a comment, never removes Phase 1’s own guarantee that every referenced method compiles.
discover
Recursive directory discovery - needed for the first time by Blade (resources/views/** nests arbitrarily); Phase 1/2a’s migrations/ config/requests directories are all flat (one read_dir call each), so nothing before this needed it.
env
Source Laravel app’s .env → the new Larust app’s .env. A plain KEY=VALUE text format on both sides, so - unlike config.rs’s PHP source - this needs no real parser, just line-oriented splitting.
events
app/Events/*.php → #[derive(Clone)] pub struct Name { pub field: Type, ... } - larust_events::Event is a pure blanket impl over Clone + Send + Sync + 'static, no derive macro, no required methods (verified against crates/larust-events/src/lib.rs), so a converted event needs nothing beyond a field-only struct matching the real PostCreated shape. Fields come entirely from constructor_props::extract - whole-item safety, matching that module’s own rationale: an event’s field list has to be exactly right or not emitted at all, since it’s the whole struct.
jobs
app/Jobs/*.php → #[derive(Serialize, Deserialize)] pub struct Name { ... } impl Job for Name { const JOB_TYPE = "..."; async fn handle(&self) -> Result<(), AppError> { todo!() } }. Fields via constructor_props::extract, same whole-item safety as events.rs. handle()’s original body preserved as a comment via php::body_as_comment - the same “preserve, never translate” treatment as controllers.rs/policies.rs.
livewire
Reads two more mechanical things straight out of a Laravel Livewire component’s own PHP source, beyond the route/mount shell xr convert’s caller already builds around every livewire_component route entry:
migrations
database/migrations/*.php (Schema::create/Schema::table + Blueprint) → Larust’s own migration format, which is raw SQL files (NNNN_snake_case_description.sql, applied in filename-sort order - see larust_orm::migrate), not a DSL. Column-type mapping verified against the real files under demo/database/migrations/.
models
app/Models/*.php → #[derive(Model)] structs, with relationships. Split into schema (reads Phase 1’s own converted SQL - the authoritative field source), fields (SQL→Rust type mapping, whole-struct safety), relations (relationship detection + verified Laravel default-argument inference, per-attribute safety), and inferred_fields (the no-migration fallback - see its own doc comment) - mirroring larust-macros’s own model.rs/relations.rs/ belongs_to_many.rs split. See docs/ARCHITECTURE.md’s “Laravel conversion” section for the whole-struct-vs-per-attribute safety rationale.
php
Thin wrapper over tree-sitter/tree-sitter-php, shared by every converter module. tree-sitter-php was chosen (over the alternative crates surveyed - php-parser, a brand-new single-maintainer crate with no independent evidence backing its own claims, and php-parser-rs, explicitly alpha) for its error-tolerant CST: a syntax-error-adjacent chunk of real-world PHP still parses into a walkable tree with a detectable ERROR node, rather than aborting the whole file - the right fit for a converter whose core rule is “never silently mistranslate.”
policies
app/Policies/*.php → impl Policy<User> for Model - mirrors xr make:policy’s own POLICY_TEMPLATE exactly (all 5 abilities stubbed false, deny-by-default), with each Laravel ability method’s original body preserved as a comment above its stub, the same “preserve, never translate” treatment as controllers.rs.
report
CONVERSION_REPORT.md - the trust mechanism this whole tool is built around. Every item a converter touches lands in exactly one bucket below; nothing is silently dropped, nothing is silently guessed at. Expands on rust-laravel.md’s own two-bucket sketch (“Converted automatically” / “Requires manual review”) with a third bucket for third-party (composer) packages, split into the two-tier design agreed before this crate was built: packages with a hand-curated Larust equivalent, and packages with none (named, never guessed at).
requests
app/Http/Requests/*.php (a FormRequest subclass’s rules(): array method) → #[derive(FormRequest)] + #[validate(...)] (crates/larust-macros/src/form_request.rs).
routes
routes/web.php/routes/api.php → Larust’s Route:: DSL (crates/larust-http/src/route.rs).