Skip to main content

larust_convert/
report.rs

1//! `CONVERSION_REPORT.md` - the trust mechanism this whole tool is built
2//! around. Every item a converter touches lands in exactly one bucket
3//! below; nothing is silently dropped, nothing is silently guessed at.
4//! Expands on `rust-laravel.md`'s own two-bucket sketch ("Converted
5//! automatically" / "Requires manual review") with a third bucket for
6//! third-party (composer) packages, split into the two-tier design agreed
7//! before this crate was built: packages with a hand-curated Larust
8//! equivalent, and packages with none (named, never guessed at).
9
10/// One "requires manual review" category - a heading plus the specific
11/// items that triggered it. Per-item file-path detail, not just a count:
12/// a bare "8 dynamic Eloquent scopes" is useless for a design whose whole
13/// point is "never silently drop, always name it."
14#[derive(Debug, Clone)]
15pub struct ManualReviewSection {
16    pub heading: String,
17    pub items: Vec<String>,
18}
19
20/// One composer package entry - `note` is either the Larust equivalent
21/// pointer (Tier 1) or a short "no mapping" explanation (Tier 2).
22#[derive(Debug, Clone)]
23pub struct PackageNote {
24    pub name: String,
25    pub version: String,
26    pub note: String,
27}
28
29#[derive(Debug, Clone, Default)]
30pub struct ConversionReport {
31    /// One line per fully-mechanical category, e.g. "42 routes
32    /// (routes/web.php, routes/api.php)" - a bare count is fine here,
33    /// since nothing in this bucket needs a human to go look at it.
34    pub converted_automatically: Vec<String>,
35    pub manual_review: Vec<ManualReviewSection>,
36    pub packages_mapped: Vec<PackageNote>,
37    pub packages_unmapped: Vec<PackageNote>,
38    /// Structure not attempted at all in this phase - named so nothing
39    /// looks like it was silently ignored by accident.
40    pub not_attempted: Vec<String>,
41}
42
43impl ConversionReport {
44    pub fn new() -> Self {
45        Self::default()
46    }
47
48    /// Always records the section, even with zero items - a re-run of
49    /// `xr convert` against a fixed-up Laravel app (or a newer `xr`
50    /// build that now handles a category it didn't before) should make
51    /// that visibly true in the fresh report, not just make the section
52    /// silently vanish. A vanished section and a stale, never-regenerated
53    /// `CONVERSION_REPORT.md` look identical from the outside; an
54    /// explicit "(0)" with a "(none)" body is unambiguous proof this run
55    /// actually checked that category and found nothing, matching this
56    /// module's own "nothing is silently dropped" doc comment for
57    /// categories, not just individual items.
58    pub fn add_manual_review(&mut self, heading: impl Into<String>, items: Vec<String>) {
59        self.manual_review.push(ManualReviewSection {
60            heading: heading.into(),
61            items,
62        });
63    }
64
65    pub fn render(&self) -> String {
66        let mut out = String::from("# Conversion Report\n\n");
67
68        out.push_str("## Converted automatically\n");
69        if self.converted_automatically.is_empty() {
70            out.push_str("(nothing converted automatically)\n");
71        } else {
72            for line in &self.converted_automatically {
73                out.push_str(&format!("- {line}\n"));
74            }
75        }
76        out.push('\n');
77
78        out.push_str("## Requires manual review\n");
79        if self.manual_review.is_empty() {
80            out.push_str("(nothing flagged)\n");
81        } else {
82            for section in &self.manual_review {
83                out.push_str(&format!(
84                    "### {} ({})\n",
85                    section.heading,
86                    section.items.len()
87                ));
88                if section.items.is_empty() {
89                    out.push_str("(none)\n");
90                } else {
91                    for item in &section.items {
92                        out.push_str(&format!("- {item}\n"));
93                    }
94                }
95            }
96        }
97        out.push('\n');
98
99        out.push_str("## Third-party packages\n");
100        out.push_str("### Tier 1 - mapped to a Larust equivalent\n");
101        if self.packages_mapped.is_empty() {
102            out.push_str("(none yet - the mapping table is populated deliberately, one package at a time; see `composer.rs`)\n");
103        } else {
104            for pkg in &self.packages_mapped {
105                out.push_str(&format!("- {} {} - {}\n", pkg.name, pkg.version, pkg.note));
106            }
107        }
108        out.push_str("### Tier 2 - no mapping, flagged\n");
109        if self.packages_unmapped.is_empty() {
110            out.push_str("(none)\n");
111        } else {
112            for pkg in &self.packages_unmapped {
113                out.push_str(&format!("- {} {} - {}\n", pkg.name, pkg.version, pkg.note));
114            }
115        }
116        out.push('\n');
117
118        out.push_str("## Not attempted in this phase\n");
119        if self.not_attempted.is_empty() {
120            out.push_str("(nothing)\n");
121        } else {
122            for line in &self.not_attempted {
123                out.push_str(&format!("- {line}\n"));
124            }
125        }
126
127        out
128    }
129}
130
131#[cfg(test)]
132mod tests {
133    use super::*;
134
135    #[test]
136    fn render_produces_all_five_sections() {
137        let report = ConversionReport::new();
138        let rendered = report.render();
139        assert!(rendered.contains("## Converted automatically"));
140        assert!(rendered.contains("## Requires manual review"));
141        assert!(rendered.contains("## Third-party packages"));
142        assert!(rendered.contains("### Tier 1"));
143        assert!(rendered.contains("### Tier 2"));
144        assert!(rendered.contains("## Not attempted in this phase"));
145    }
146
147    #[test]
148    fn an_empty_manual_review_section_is_still_recorded_and_rendered() {
149        // A vanished section and a stale, never-regenerated report look
150        // identical from the outside - an explicit "(0)"/"(none)" is
151        // what proves this run actually checked the category.
152        let mut report = ConversionReport::new();
153        report.add_manual_review("Nothing here", vec![]);
154        assert_eq!(report.manual_review.len(), 1);
155        let rendered = report.render();
156        assert!(rendered.contains("### Nothing here (0)"));
157        assert!(rendered.contains("(none)"));
158    }
159
160    #[test]
161    fn manual_review_section_shows_count_and_every_item() {
162        let mut report = ConversionReport::new();
163        report.add_manual_review(
164            "Migrations using timestamps()",
165            vec!["database/migrations/0001_create_posts_table.php - created_at/updated_at columns emitted; no automatic population".to_string()],
166        );
167        let rendered = report.render();
168        assert!(rendered.contains("### Migrations using timestamps() (1)"));
169        assert!(rendered.contains("0001_create_posts_table.php"));
170    }
171
172    #[test]
173    fn tier_1_and_tier_2_packages_render_separately() {
174        let mut report = ConversionReport::new();
175        report.packages_unmapped.push(PackageNote {
176            name: "spatie/laravel-permission".to_string(),
177            version: "^6.0".to_string(),
178            note: "no Larust equivalent; port manually".to_string(),
179        });
180        let rendered = report.render();
181        assert!(rendered.contains("spatie/laravel-permission ^6.0 - no Larust equivalent"));
182    }
183}