larust-convert 0.12.0

A Laravel-shaped web framework for Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! `composer.json` → third-party package report. No PHP parsing needed
//! here - `composer.json` is plain JSON, read with `serde_json` (already a
//! workspace dependency).
//!
//! **Packages are never auto-ported.** A small, hand-curated mapping table
//! (below) is populated deliberately over time, the same one-at-a-time way
//! `larust-mail`/`larust-queue`/`larust-scheduler`/`larust-notifications`
//! were each built as individual crates - never auto-generated PHP-to-Rust
//! translation of a package's internals. Anything not in the table is
//! named, with its version constraint, in the report - never silently
//! dropped, never guessed at.

use crate::report::PackageNote;
use anyhow::{Context, Result};
use std::collections::BTreeMap;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Package {
    pub name: String,
    pub version: String,
}

/// A single hand-curated entry: a composer package name mapped to a short
/// note pointing at its Larust equivalent. Starts empty/near-empty - its
/// value is the mechanism and the detection, not a pre-built library of
/// ports (see this module's own doc comment). Add an entry here only once
/// a real Larust equivalent actually exists and has been verified to cover
/// the package's common usage - never as a hopeful placeholder.
///
/// Not every entry here points at a hand-built Larust crate - three
/// shapes of note actually land in this table:
/// - a real, hand-built crate in this workspace (`spatie/laravel-permission`);
/// - a package whose role is already fully covered by Larust's own
///   architecture, nothing to port (`laravel/octane` - its whole reason
///   to exist, avoiding PHP's per-request bootstrap cost, doesn't apply
///   to an already-compiled, already-long-running native binary);
/// - a package that trivially maps to an existing crates.io crate, no
///   Larust-specific wrapper needed at all (`stripe/stripe-php` →
///   `async-stripe`).
///
/// The third field is the `larust-support` Cargo feature this package's
/// mapping needs turned on in the generated app (see [`required_features`]) -
/// `None` when a mapping doesn't correspond to one of the five optional
/// Tier-1 shim crates at all: `laravel/octane` is a no-op, `stripe/
/// stripe-php` maps to an external crates.io crate (not `larust-support`),
/// and `spatie/laravel-responsecache` maps to `larust_http::responsecache`,
/// which is unconditional (core `larust-http`, not one of the five
/// optional shims).
const TIER_1: &[(&str, &str, Option<&str>)] = &[
    (
        "laravel/cashier",
        "maps to larust_support::cashier::stripe (this workspace, backed by the \
         larust-cashier-stripe crate) - subscription billing via create_customer()/ \
         checkout_url()/cancel_subscription()/resume_subscription()/swap_subscription(), with \
         handle_webhook() keeping a local subscription mirror in sync; one of possibly several \
         independently-selectable Cashier backends (see its own doc comment), with no \
         Checkout UI/customer portal, one subscription item per subscription, no invoice \
         listing or payment-method management",
        Some("cashier-stripe"),
    ),
    (
        "spatie/laravel-permission",
        "maps to larust-permissions (this workspace) - compile-checked permission/role names, \
         DB-backed assignment; see its own doc comment for the hybrid design and what's still \
         manual (Blade @can/@role directives, role:/permission: middleware strings)",
        Some("permissions"),
    ),
    (
        "laravel/octane",
        "not needed - Larust's own compiled, long-running native binary already avoids the \
         per-request PHP bootstrap cost Octane exists to eliminate",
        None,
    ),
    (
        "stripe/stripe-php",
        "maps to the async-stripe crate (crates.io) - Stripe's own actively-maintained Rust \
         SDK; add it directly, no Larust-specific wrapper needed",
        None,
    ),
    (
        "spatie/laravel-responsecache",
        "maps to larust_http::responsecache (this workspace) - GET/200-only page caching \
         keyed by URL, backed by larust-cache; see its own doc comment for what's \
         deliberately out of scope (Vary/per-user caching, auto-invalidation on writes, \
         bulk cache-clear - only per-URL forget(url) and TTL expiry)",
        None,
    ),
    (
        "laravel/sanctum",
        "maps to larust_support::sanctum (this workspace, backed by the larust-sanctum \
         crate) - bearer-token API authentication via the ApiAuth<U> extractor, sibling \
         to larust_auth::Auth<U>; see its own doc comment for what's deliberately out of \
         scope (token abilities/scopes, SPA stateful-cookie mode - Auth<U> already covers \
         that, auth:sanctum middleware-string recognition)",
        Some("sanctum"),
    ),
    (
        "spatie/laravel-sitemap",
        "maps to larust_support::sitemap (this workspace, backed by the larust-sitemap \
         crate) - an XML <urlset> builder plus a response wrapper, not an auto-mounted \
         route; from_static_routes() covers static GET routes, dynamic per-model URLs \
         are supplied by the app itself; see its own doc comment for what's deliberately \
         out of scope (sitemap index/pagination past 50k URLs, built-in caching - wrap \
         the route in larust_http::responsecache instead)",
        Some("sitemap"),
    ),
    (
        "laravel/socialite",
        "maps to larust_support::socialite (this workspace, backed by the larust-socialite \
         crate) - OAuth Authorization Code login (GitHub/Google built in), via \
         redirect_url()/user_from_callback(); see its own doc comment for what's \
         deliberately out of scope (OIDC ID-token verification, token refresh/storage, \
         an extend()-style provider registry - a third provider is just a hand-built \
         OAuthProvider value)",
        Some("socialite"),
    ),
    (
        "laravel/reverb",
        "maps to larust_support::reverb (this workspace, backed by the larust-reverb \
         crate) - generic WebSocket pub/sub broadcasting, public and private channels, \
         via broadcast_event()/authorize()/socket(); see its own doc comment for what's \
         deliberately out of scope (presence channels, Pusher-wire-protocol \
         compatibility, a per-channel-pattern authorization registry - one global \
         authorize() callback does its own channel-name matching instead)",
        Some("reverb"),
    ),
    (
        "bezhansalleh/filament-shield",
        "maps to larust_support::shield (this workspace, backed by the larust-shield crate) - \
         resource/ability permission bundles on top of larust-permissions, filament-shield's \
         own `view`/`create`/`update`/`delete`-per-resource shape narrowed to those four plus \
         `view_any`; see its own doc comment for what's deliberately out of scope (no \
         auto-scanning of your models/Filament resources - list your own resources by hand, \
         no generated admin UI for editing role grants, no abilities beyond those five)",
        Some("shield"),
    ),
    (
        "laravel/passport",
        "maps to larust_support::passport (this workspace, backed by the larust-passport \
         crate) - a full OAuth2 authorization server: create_client(), \
         create_authorization_code() + exchange_token() (Authorization Code with mandatory \
         PKCE, Client Credentials, Refresh Token rotation), OAuthAuth<U>/OAuthClientAuth \
         request extractors; see its own doc comment for what's deliberately out of scope \
         (one redirect URI per client, no `passport:client`-equivalent interactive CLI, no \
         scopes UI/consent-screen rendering - your own /oauth/authorize route owns that)",
        Some("passport"),
    ),
    (
        "laravel/pennant",
        "maps to larust_support::pennant (this workspace, backed by the larust-pennant \
         crate) - persisted, scope-aware boolean feature flags via active()/activate()/ \
         deactivate()/forget()/purge(); see its own doc comment for what's deliberately out \
         of scope (no Feature::define() resolver registry - active()'s own default argument \
         replaces it, no @feature/@endfeature Blade directive - larust-view's directive set \
         isn't an extensible registry, no non-boolean rich values)",
        Some("pennant"),
    ),
    (
        "laravel/scout",
        "maps to larust_support::scout (this workspace, backed by the larust-scout crate) - \
         full-text search via index()/search()/remove() against a Meilisearch instance; see \
         its own doc comment for what's deliberately out of scope (Meilisearch only, no \
         Algolia/database/collection drivers, no Searchable trait/automatic model-lifecycle \
         indexing - explicit index()/remove() calls instead, no index configuration, \
         filters, sorting, or facets)",
        Some("scout"),
    ),
];

/// The `larust-support` Cargo features `packages` implies, derived from
/// [`TIER_1`]'s own third field - this is what turns `composer.json`'s own
/// `require` block into the "required file" driving which optional Tier-1
/// shim crates a converted app's generated `Cargo.toml` actually compiles
/// in (see `xr convert`'s own doc comment/`docs/GOTCHAS.md`). Sorted and
/// deduped so the generated `features = [...]` list is deterministic.
pub fn required_features(packages: &[Package]) -> Vec<&'static str> {
    let names: std::collections::HashSet<&str> = packages.iter().map(|p| p.name.as_str()).collect();
    let mut features: Vec<&'static str> = TIER_1
        .iter()
        .filter_map(|(name, _, feature)| feature.filter(|_| names.contains(name)))
        .collect();
    features.sort_unstable();
    features.dedup();
    features
}

/// Parses `composer.json`'s `require` object into `(package, version)`
/// pairs, in the order they appear in the file. Skips `php` itself (a
/// runtime version constraint, not a package). Malformed JSON or a missing
/// `require` object is a real error - this is called only after the
/// caller has already confirmed the file exists.
pub fn parse_require(source: &str) -> Result<Vec<Package>> {
    let value: serde_json::Value =
        serde_json::from_str(source).context("parsing composer.json as JSON")?;
    let require = value
        .get("require")
        .and_then(|r| r.as_object())
        .context("composer.json has no `require` object")?;

    let mut packages = Vec::new();
    for (name, version) in require {
        if name == "php" {
            continue;
        }
        let version = version.as_str().unwrap_or("*").to_string();
        packages.push(Package {
            name: name.clone(),
            version,
        });
    }
    // `serde_json::Value::Object` preserves insertion order (the `preserve_order`
    // feature isn't enabled here, so it's a `BTreeMap` internally - sort
    // explicitly so output is deterministic regardless of that).
    packages.sort_by(|a, b| a.name.cmp(&b.name));
    Ok(packages)
}

/// `true` if `packages` looks like a real Laravel application - used to
/// fail `xr convert` fast on an unrelated directory rather than three
/// minutes into a partial conversion.
pub fn looks_like_laravel(packages: &[Package]) -> bool {
    packages.iter().any(|p| p.name == "laravel/framework")
}

/// Splits `packages` into the two report tiers. `laravel/framework` itself
/// is excluded - it isn't a third-party dependency to port, Larust *is*
/// its wholesale replacement, so listing it as "no Larust equivalent;
/// port manually" would be misleading noise rather than a real gap.
pub fn classify(packages: &[Package]) -> (Vec<PackageNote>, Vec<PackageNote>) {
    let tier_1: BTreeMap<&str, &str> = TIER_1
        .iter()
        .map(|(name, note, _)| (*name, *note))
        .collect();
    let mut mapped = Vec::new();
    let mut unmapped = Vec::new();

    for package in packages {
        if package.name == "laravel/framework" {
            continue;
        }
        if let Some(note) = tier_1.get(package.name.as_str()) {
            mapped.push(PackageNote {
                name: package.name.clone(),
                version: package.version.clone(),
                note: note.to_string(),
            });
        } else {
            unmapped.push(PackageNote {
                name: package.name.clone(),
                version: package.version.clone(),
                note: "no Larust equivalent; port manually".to_string(),
            });
        }
    }

    (mapped, unmapped)
}

#[cfg(test)]
mod tests {
    use super::*;

    const SAMPLE: &str = r#"{
        "require": {
            "php": "^8.2",
            "laravel/framework": "^11.0",
            "spatie/laravel-permission": "^6.0",
            "spatie/laravel-activitylog": "^4.0"
        }
    }"#;

    #[test]
    fn parse_require_skips_php_and_extracts_the_rest() {
        let packages = parse_require(SAMPLE).unwrap();
        assert_eq!(packages.len(), 3);
        assert!(packages
            .iter()
            .any(|p| p.name == "laravel/framework" && p.version == "^11.0"));
        assert!(packages
            .iter()
            .any(|p| p.name == "spatie/laravel-permission" && p.version == "^6.0"));
    }

    #[test]
    fn looks_like_laravel_checks_for_the_framework_package() {
        let packages = parse_require(SAMPLE).unwrap();
        assert!(looks_like_laravel(&packages));
        assert!(!looks_like_laravel(&[]));
    }

    #[test]
    fn classify_splits_mapped_and_unmapped_packages() {
        // `spatie/laravel-permission` is a real TIER_1 entry (points at
        // `larust-permissions`); `spatie/laravel-activitylog` has no
        // mapping yet and stays unmapped - this is the mapped/unmapped
        // split actually exercised, not just "everything's unmapped
        // because the table happens to be empty."
        let packages = parse_require(SAMPLE).unwrap();
        let (mapped, unmapped) = classify(&packages);
        assert_eq!(mapped.len(), 1);
        assert_eq!(mapped[0].name, "spatie/laravel-permission");
        assert!(mapped[0].note.contains("larust-permissions"));
        assert_eq!(unmapped.len(), 1);
        assert_eq!(unmapped[0].name, "spatie/laravel-activitylog");
    }

    #[test]
    fn classify_maps_responsecache_too() {
        let packages = [Package {
            name: "spatie/laravel-responsecache".to_string(),
            version: "^7.0".to_string(),
        }];
        let (mapped, unmapped) = classify(&packages);
        assert_eq!(mapped.len(), 1);
        assert_eq!(mapped[0].name, "spatie/laravel-responsecache");
        assert!(mapped[0].note.contains("larust_http::responsecache"));
        assert!(unmapped.is_empty());
    }

    #[test]
    fn classify_maps_sanctum_too() {
        let packages = [Package {
            name: "laravel/sanctum".to_string(),
            version: "^4.0".to_string(),
        }];
        let (mapped, unmapped) = classify(&packages);
        assert_eq!(mapped.len(), 1);
        assert_eq!(mapped[0].name, "laravel/sanctum");
        assert!(mapped[0].note.contains("larust_support::sanctum"));
        assert!(unmapped.is_empty());
    }

    #[test]
    fn classify_maps_sitemap_too() {
        let packages = [Package {
            name: "spatie/laravel-sitemap".to_string(),
            version: "^7.0".to_string(),
        }];
        let (mapped, unmapped) = classify(&packages);
        assert_eq!(mapped.len(), 1);
        assert_eq!(mapped[0].name, "spatie/laravel-sitemap");
        assert!(mapped[0].note.contains("larust_support::sitemap"));
        assert!(unmapped.is_empty());
    }

    #[test]
    fn classify_maps_socialite_too() {
        let packages = [Package {
            name: "laravel/socialite".to_string(),
            version: "^5.0".to_string(),
        }];
        let (mapped, unmapped) = classify(&packages);
        assert_eq!(mapped.len(), 1);
        assert_eq!(mapped[0].name, "laravel/socialite");
        assert!(mapped[0].note.contains("larust_support::socialite"));
        assert!(unmapped.is_empty());
    }

    #[test]
    fn classify_maps_reverb_too() {
        let packages = [Package {
            name: "laravel/reverb".to_string(),
            version: "^1.0".to_string(),
        }];
        let (mapped, unmapped) = classify(&packages);
        assert_eq!(mapped.len(), 1);
        assert_eq!(mapped[0].name, "laravel/reverb");
        assert!(mapped[0].note.contains("larust_support::reverb"));
        assert!(unmapped.is_empty());
    }

    #[test]
    fn classify_maps_passport_too() {
        let packages = [Package {
            name: "laravel/passport".to_string(),
            version: "^12.0".to_string(),
        }];
        let (mapped, unmapped) = classify(&packages);
        assert_eq!(mapped.len(), 1);
        assert_eq!(mapped[0].name, "laravel/passport");
        assert!(mapped[0].note.contains("larust_support::passport"));
        assert!(unmapped.is_empty());
    }

    #[test]
    fn classify_maps_pennant_too() {
        let packages = [Package {
            name: "laravel/pennant".to_string(),
            version: "^1.0".to_string(),
        }];
        let (mapped, unmapped) = classify(&packages);
        assert_eq!(mapped.len(), 1);
        assert_eq!(mapped[0].name, "laravel/pennant");
        assert!(mapped[0].note.contains("larust_support::pennant"));
        assert!(unmapped.is_empty());
    }

    #[test]
    fn classify_maps_scout_too() {
        let packages = [Package {
            name: "laravel/scout".to_string(),
            version: "^10.0".to_string(),
        }];
        let (mapped, unmapped) = classify(&packages);
        assert_eq!(mapped.len(), 1);
        assert_eq!(mapped[0].name, "laravel/scout");
        assert!(mapped[0].note.contains("larust_support::scout"));
        assert!(unmapped.is_empty());
    }

    #[test]
    fn classify_maps_cashier_too() {
        let packages = [Package {
            name: "laravel/cashier".to_string(),
            version: "^15.0".to_string(),
        }];
        let (mapped, unmapped) = classify(&packages);
        assert_eq!(mapped.len(), 1);
        assert_eq!(mapped[0].name, "laravel/cashier");
        assert!(mapped[0].note.contains("larust_support::cashier"));
        assert!(unmapped.is_empty());
    }

    #[test]
    fn required_features_detects_every_tier_1_shim_sorted_and_deduped() {
        let packages = [
            Package {
                name: "laravel/sanctum".to_string(),
                version: "^4.0".to_string(),
            },
            Package {
                name: "spatie/laravel-permission".to_string(),
                version: "^6.0".to_string(),
            },
            Package {
                name: "laravel/reverb".to_string(),
                version: "^1.0".to_string(),
            },
            // A duplicate-feature package (there isn't a real second one
            // mapping to "sanctum", so this just re-asserts sanctum is
            // still present exactly once) - dedup is exercised by the
            // sort_unstable/dedup call itself regardless.
            Package {
                name: "laravel/sanctum".to_string(),
                version: "^4.0".to_string(),
            },
        ];
        assert_eq!(
            required_features(&packages),
            vec!["permissions", "reverb", "sanctum"]
        );
    }

    #[test]
    fn required_features_is_empty_for_unmapped_or_feature_less_packages() {
        let packages = [
            Package {
                name: "spatie/laravel-activitylog".to_string(),
                version: "^4.0".to_string(),
            },
            Package {
                name: "laravel/octane".to_string(),
                version: "^2.0".to_string(),
            },
            Package {
                name: "stripe/stripe-php".to_string(),
                version: "^15.0".to_string(),
            },
            Package {
                name: "spatie/laravel-responsecache".to_string(),
                version: "^7.0".to_string(),
            },
        ];
        assert!(required_features(&packages).is_empty());
    }

    #[test]
    fn classify_excludes_laravel_framework_itself() {
        let packages = parse_require(SAMPLE).unwrap();
        let (_, unmapped) = classify(&packages);
        assert!(!unmapped.iter().any(|p| p.name == "laravel/framework"));
    }

    #[test]
    fn parse_require_rejects_missing_require_object() {
        assert!(parse_require("{}").is_err());
    }
}