Skip to main content

larust_convert/blade/
mod.rs

1//! `resources/views/**/*.blade.php` → `resources/views/**/*.blade.xr`.
2//! See `docs/ARCHITECTURE.md`'s "Laravel conversion" section for the
3//! whole-file (not per-item) safety rationale: `crates/larust-macros/src/
4//! view.rs`'s `view!` macro consumes a template as one indivisible unit,
5//! so a bad directive or expression can't be safely omitted mid-file -
6//! either the whole file translates cleanly, or none of it is trusted.
7//! That rationale still holds for *most* failures - see `scan.rs`'s own
8//! module doc comment for the one deliberate exception (a top-level
9//! `@php` block) and why it's safe.
10
11pub mod expr;
12pub mod scan;
13
14use std::cell::{Cell, RefCell};
15use std::collections::HashSet;
16use std::path::Path;
17
18/// Convert-time context threaded through every Blade-conversion call -
19/// `scan.rs`'s own directive/tag scanning and, through it,
20/// `expr.rs`'s expression translation. Lives here, one level up from
21/// both, rather than in either - `scan` already depends on `expr`
22/// (`use super::expr;`), so defining this type in `scan` and having
23/// `expr` reference it back would make the two modules mutually
24/// dependent for no reason beyond avoiding one extra `use` at this
25/// shared level.
26pub struct ConvertContext<'a> {
27    /// Only ever read by `scan::scan_livewire_tag`, to resolve a nested
28    /// `<livewire:X>` component's own PHP class and enrich its
29    /// translation with whatever default property values it declares.
30    pub laravel_root: &'a Path,
31    /// Every `"{config file stem}.{key}"` pair a generated
32    /// `config/{file}.rs` module successfully resolved (see
33    /// `larust_convert::config::convert_body`) - `expr.rs`'s `"config"`
34    /// function-call arm checks membership here to decide whether
35    /// `config('file.key')` has a real generated home to reference,
36    /// never touching the underlying value itself (that already lives in
37    /// the generated file by the time this runs).
38    pub resolved_config_keys: &'a HashSet<String>,
39    /// Variable names a dropped top-level `@php` block *would* have
40    /// assigned (see `scan.rs`'s own doc comment for the full mechanism) -
41    /// `expr::translate`'s `"variable_name"` arm treats a reference to
42    /// any name in here as unsupported, the same as an undefined
43    /// superglobal, so it degrades in place instead of translating into a
44    /// reference to a binding that no longer exists. Mutated inline
45    /// during a single left-to-right scan (never re-read from an earlier
46    /// position - Blade renders top-to-bottom and PHP has no forward
47    /// declarations, so a reference can only follow the assignment that
48    /// taints it), hence the interior mutability despite `ConvertContext`
49    /// otherwise being pure borrowed, read-only config.
50    ///
51    /// **Must be constructed fresh per file**, never reused across a
52    /// whole conversion run - taint from one file's own `@php` block has
53    /// no meaning for the next file's variables of the same name.
54    pub tainted_vars: RefCell<HashSet<String>>,
55    /// How many spots in the current file have degraded so far - every
56    /// degrade site calls `scan::next_degraded_spot(ctx)` to claim the
57    /// next number, embedded in *both* the inline placeholder
58    /// (`<!-- ... (spot #N) ... -->`, always safe to embed - a bare
59    /// integer, never raw source) and the matching `CONVERSION_REPORT.md`
60    /// note (which also carries the real original source text, safe
61    /// there because a Markdown report is never re-parsed as a template
62    /// the way a `.blade.xr` file is). Lets a file with several drops
63    /// actually be navigated - a spot in the output can be traced to its
64    /// exact report entry instead of a pile of identical, anonymous
65    /// placeholder comments. **Must be constructed fresh per file**, same
66    /// reasoning as `tainted_vars`.
67    pub degraded_spot_count: Cell<usize>,
68}