Skip to main content

devimg_core/
review.rs

1use std::collections::BTreeMap;
2
3use crate::manifest::{Manifest, ManifestOutput};
4use crate::quality::manifest_quality_warnings;
5
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct ManifestReviewOptions {
8    pub asset_path_prefix: String,
9    pub top_limit: usize,
10}
11
12impl Default for ManifestReviewOptions {
13    fn default() -> Self {
14        Self {
15            asset_path_prefix: String::new(),
16            top_limit: 8,
17        }
18    }
19}
20
21pub fn render_manifest_review(manifest: &Manifest, options: &ManifestReviewOptions) -> String {
22    let groups = grouped_outputs(manifest);
23    let warnings = review_warnings(manifest);
24    let source_count = groups.len();
25    let top_limit = options.top_limit.max(1);
26
27    let mut out = String::new();
28    out.push_str("<!doctype html>\n");
29    out.push_str("<html lang=\"en\">\n");
30    out.push_str("<head>\n");
31    out.push_str("  <meta charset=\"utf-8\">\n");
32    out.push_str("  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n");
33    out.push_str("  <title>DevImg Review</title>\n");
34    out.push_str("  <style>\n");
35    out.push_str(STYLES);
36    out.push_str("  </style>\n");
37    out.push_str("</head>\n");
38    out.push_str("<body>\n");
39    out.push_str("  <main class=\"page\">\n");
40    out.push_str("    <header class=\"hero\">\n");
41    out.push_str("      <p class=\"eyebrow\">DevImg visual review</p>\n");
42    out.push_str("      <h1>Generated image variants</h1>\n");
43    out.push_str("      <dl class=\"meta-grid\">\n");
44    push_stat(&mut out, "Sources", &source_count.to_string());
45    push_stat(&mut out, "Variants", &manifest.outputs.len().to_string());
46    push_stat(
47        &mut out,
48        "Source bytes",
49        &format_bytes(manifest.source_bytes_total()),
50    );
51    push_stat(
52        &mut out,
53        "Output bytes",
54        &format_bytes(manifest.output_bytes_total()),
55    );
56    push_stat(&mut out, "Budget status", "not evaluated from manifest");
57    push_stat(&mut out, "Config hash", &short_hash(&manifest.config_hash));
58    out.push_str("      </dl>\n");
59    out.push_str("    </header>\n");
60
61    out.push_str("    <section class=\"panel\">\n");
62    out.push_str("      <div class=\"section-heading\">\n");
63    out.push_str("        <h2>Manifest</h2>\n");
64    out.push_str("      </div>\n");
65    out.push_str("      <div class=\"manifest-grid\">\n");
66    push_key_value(&mut out, "Generated at", &manifest.generated_at);
67    push_key_value(&mut out, "Config path", &manifest.config_path);
68    push_key_value(&mut out, "Config hash", &manifest.config_hash);
69    push_key_value(&mut out, "Budget status", "not evaluated; run devimg check");
70    out.push_str("      </div>\n");
71    out.push_str("    </section>\n");
72
73    out.push_str("    <section class=\"panel\">\n");
74    out.push_str("      <div class=\"section-heading\">\n");
75    out.push_str("        <h2>Review signals</h2>\n");
76    out.push_str("      </div>\n");
77    if warnings.is_empty() {
78        out.push_str("      <p class=\"empty-state\">No manifest-only warnings.</p>\n");
79    } else {
80        out.push_str("      <ul class=\"signal-list\">\n");
81        for warning in &warnings {
82            out.push_str("        <li>");
83            push_escaped(&mut out, warning);
84            out.push_str("</li>\n");
85        }
86        out.push_str("      </ul>\n");
87    }
88    out.push_str("    </section>\n");
89
90    out.push_str("    <section class=\"panel split-panel\">\n");
91    push_top_sources(&mut out, &groups, top_limit);
92    push_top_outputs(&mut out, &manifest.outputs, top_limit);
93    out.push_str("    </section>\n");
94
95    for (source_path, outputs) in &groups {
96        push_source_group(&mut out, source_path, outputs, options);
97    }
98
99    out.push_str("  </main>\n");
100    out.push_str("</body>\n");
101    out.push_str("</html>\n");
102    out
103}
104
105fn grouped_outputs(manifest: &Manifest) -> BTreeMap<&str, Vec<&ManifestOutput>> {
106    let mut groups = BTreeMap::<&str, Vec<&ManifestOutput>>::new();
107    for output in &manifest.outputs {
108        groups
109            .entry(output.source_path.as_str())
110            .or_default()
111            .push(output);
112    }
113    for outputs in groups.values_mut() {
114        outputs.sort_by(|left, right| {
115            left.preset
116                .cmp(&right.preset)
117                .then(left.width.cmp(&right.width))
118                .then(left.height.cmp(&right.height))
119                .then(left.format.cmp(&right.format))
120                .then(left.output_path.cmp(&right.output_path))
121        });
122    }
123    groups
124}
125
126fn review_warnings(manifest: &Manifest) -> Vec<String> {
127    if manifest.outputs.is_empty() {
128        return vec!["Manifest has no generated variants.".to_string()];
129    }
130    manifest_quality_warnings(manifest)
131}
132
133fn push_source_group(
134    out: &mut String,
135    source_path: &str,
136    outputs: &[&ManifestOutput],
137    options: &ManifestReviewOptions,
138) {
139    let first = outputs[0];
140    let source_href = asset_href(source_path, &options.asset_path_prefix);
141    let variant_total: u64 = outputs.iter().map(|output| output.bytes).sum();
142
143    out.push_str("    <section class=\"source-group\">\n");
144    out.push_str("      <div class=\"source-intro\">\n");
145    out.push_str("        <div>\n");
146    out.push_str("          <p class=\"eyebrow\">Source</p>\n");
147    out.push_str("          <h2>");
148    push_escaped(out, source_path);
149    out.push_str("</h2>\n");
150    out.push_str("        </div>\n");
151    out.push_str("        <dl class=\"source-stats\">\n");
152    push_stat(
153        out,
154        "Dimensions",
155        &format!("{}x{}", first.source_width, first.source_height),
156    );
157    push_stat(out, "Source bytes", &format_bytes(first.source_bytes));
158    push_stat(out, "Variant bytes", &format_bytes(variant_total));
159    push_stat(out, "Variants", &outputs.len().to_string());
160    out.push_str("        </dl>\n");
161    out.push_str("      </div>\n");
162
163    out.push_str("      <div class=\"source-preview\">\n");
164    push_image(
165        out,
166        &source_href,
167        &format!("Source image: {source_path}"),
168        first.source_width,
169        first.source_height,
170    );
171    out.push_str("      </div>\n");
172
173    out.push_str("      <div class=\"variant-grid\">\n");
174    for output in outputs {
175        push_variant_card(out, output, options);
176    }
177    out.push_str("      </div>\n");
178    out.push_str("    </section>\n");
179}
180
181fn push_variant_card(out: &mut String, output: &ManifestOutput, options: &ManifestReviewOptions) {
182    let output_href = asset_href(&output.output_path, &options.asset_path_prefix);
183    out.push_str("        <article class=\"variant-card\">\n");
184    out.push_str("          <div class=\"variant-image\">\n");
185    push_image(
186        out,
187        &output_href,
188        &format!(
189            "{} {} {}x{}",
190            output.preset, output.format, output.width, output.height
191        ),
192        output.width,
193        output.height,
194    );
195    out.push_str("          </div>\n");
196    out.push_str("          <div class=\"variant-body\">\n");
197    out.push_str("            <div class=\"variant-title\">\n");
198    out.push_str("              <strong>");
199    push_escaped(out, &output.preset);
200    out.push_str("</strong>\n");
201    out.push_str("              <span>");
202    push_escaped(out, &output.format);
203    out.push_str("</span>\n");
204    out.push_str("            </div>\n");
205    out.push_str("            <dl class=\"variant-meta\">\n");
206    push_key_value(out, "Size", &format!("{}x{}", output.width, output.height));
207    push_key_value(out, "Fit", &output.fit);
208    push_key_value(out, "Bytes", &format_bytes(output.bytes));
209    push_key_value(out, "Hash", &short_hash(&output.hash));
210    out.push_str("            </dl>\n");
211    out.push_str("            <a class=\"path-link\" href=\"");
212    push_attr_escaped(out, &output_href);
213    out.push_str("\">");
214    push_escaped(out, &output.output_path);
215    out.push_str("</a>\n");
216    out.push_str("          </div>\n");
217    out.push_str("        </article>\n");
218}
219
220fn push_top_sources(
221    out: &mut String,
222    groups: &BTreeMap<&str, Vec<&ManifestOutput>>,
223    top_limit: usize,
224) {
225    let mut sources: Vec<(&str, u64)> = groups
226        .iter()
227        .map(|(source, outputs)| (*source, outputs[0].source_bytes))
228        .collect();
229    sources.sort_by(|left, right| right.1.cmp(&left.1).then(left.0.cmp(right.0)));
230
231    out.push_str("      <div>\n");
232    out.push_str("        <h2>Largest sources</h2>\n");
233    push_ranked_list(out, sources.into_iter().take(top_limit));
234    out.push_str("      </div>\n");
235}
236
237fn push_top_outputs(out: &mut String, outputs: &[ManifestOutput], top_limit: usize) {
238    let mut ranked: Vec<(&str, u64)> = outputs
239        .iter()
240        .map(|output| (output.output_path.as_str(), output.bytes))
241        .collect();
242    ranked.sort_by(|left, right| right.1.cmp(&left.1).then(left.0.cmp(right.0)));
243
244    out.push_str("      <div>\n");
245    out.push_str("        <h2>Largest outputs</h2>\n");
246    push_ranked_list(out, ranked.into_iter().take(top_limit));
247    out.push_str("      </div>\n");
248}
249
250fn push_ranked_list<'a>(out: &mut String, items: impl Iterator<Item = (&'a str, u64)>) {
251    let mut count = 0;
252    out.push_str("        <ol class=\"ranked-list\">\n");
253    for (path, bytes) in items {
254        count += 1;
255        out.push_str("          <li><span>");
256        push_escaped(out, path);
257        out.push_str("</span><strong>");
258        push_escaped(out, &format_bytes(bytes));
259        out.push_str("</strong></li>\n");
260    }
261    if count == 0 {
262        out.push_str("          <li><span>No entries.</span><strong>0 B</strong></li>\n");
263    }
264    out.push_str("        </ol>\n");
265}
266
267fn push_stat(out: &mut String, label: &str, value: &str) {
268    out.push_str("        <div>\n");
269    out.push_str("          <dt>");
270    push_escaped(out, label);
271    out.push_str("</dt>\n");
272    out.push_str("          <dd>");
273    push_escaped(out, value);
274    out.push_str("</dd>\n");
275    out.push_str("        </div>\n");
276}
277
278fn push_key_value(out: &mut String, label: &str, value: &str) {
279    out.push_str("              <div><dt>");
280    push_escaped(out, label);
281    out.push_str("</dt><dd>");
282    push_escaped(out, value);
283    out.push_str("</dd></div>\n");
284}
285
286fn push_image(out: &mut String, href: &str, alt: &str, width: u32, height: u32) {
287    out.push_str("            <a href=\"");
288    push_attr_escaped(out, href);
289    out.push_str("\"><img src=\"");
290    push_attr_escaped(out, href);
291    out.push_str("\" alt=\"");
292    push_attr_escaped(out, alt);
293    out.push_str("\" width=\"");
294    out.push_str(&width.to_string());
295    out.push_str("\" height=\"");
296    out.push_str(&height.to_string());
297    out.push_str("\" loading=\"lazy\" decoding=\"async\"></a>\n");
298}
299
300fn asset_href(path: &str, prefix: &str) -> String {
301    let path = path.replace('\\', "/");
302    let prefix = prefix.replace('\\', "/");
303    let prefix = prefix.trim_end_matches('/');
304    if prefix.is_empty() {
305        path
306    } else if path.is_empty() {
307        prefix.to_string()
308    } else {
309        format!("{prefix}/{}", path.trim_start_matches('/'))
310    }
311}
312
313fn short_hash(hash: &str) -> String {
314    hash.split_once(':')
315        .map(|(_, value)| value)
316        .unwrap_or(hash)
317        .chars()
318        .take(12)
319        .collect()
320}
321
322fn format_bytes(bytes: u64) -> String {
323    const KB: f64 = 1024.0;
324    const MB: f64 = KB * 1024.0;
325    let bytes = bytes as f64;
326    if bytes >= MB {
327        format!("{:.2} MB", bytes / MB)
328    } else if bytes >= KB {
329        format!("{:.1} KB", bytes / KB)
330    } else {
331        format!("{} B", bytes as u64)
332    }
333}
334
335fn push_escaped(out: &mut String, value: &str) {
336    for ch in value.chars() {
337        match ch {
338            '&' => out.push_str("&amp;"),
339            '<' => out.push_str("&lt;"),
340            '>' => out.push_str("&gt;"),
341            '"' => out.push_str("&quot;"),
342            '\'' => out.push_str("&#39;"),
343            _ => out.push(ch),
344        }
345    }
346}
347
348fn push_attr_escaped(out: &mut String, value: &str) {
349    push_escaped(out, value);
350}
351
352const STYLES: &str = r#":root {
353    color-scheme: light;
354    --paper: #f7f4ee;
355    --ink: #1c211f;
356    --muted: #646b67;
357    --line: #d8d1c5;
358    --panel: #fffdf8;
359    --accent: #0e6f68;
360    --accent-ink: #06443f;
361    --warn: #9a5a00;
362    --danger: #9d2f2f;
363    --shadow: 0 16px 38px rgba(44, 38, 28, 0.08);
364  }
365
366  * {
367    box-sizing: border-box;
368  }
369
370  body {
371    margin: 0;
372    background:
373      linear-gradient(90deg, rgba(28, 33, 31, 0.035) 1px, transparent 1px),
374      linear-gradient(180deg, rgba(28, 33, 31, 0.035) 1px, transparent 1px),
375      var(--paper);
376    background-size: 36px 36px;
377    color: var(--ink);
378    font-family: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
379  }
380
381  .page {
382    width: min(1440px, calc(100% - 40px));
383    margin: 0 auto;
384    padding: 40px 0 56px;
385  }
386
387  .hero,
388  .panel,
389  .source-group {
390    background: rgba(255, 253, 248, 0.94);
391    border: 1px solid var(--line);
392    box-shadow: var(--shadow);
393  }
394
395  .hero {
396    padding: 32px;
397    border-radius: 6px;
398  }
399
400  .eyebrow {
401    margin: 0 0 10px;
402    color: var(--accent-ink);
403    font-size: 12px;
404    font-weight: 800;
405    letter-spacing: 0;
406    text-transform: uppercase;
407  }
408
409  h1,
410  h2 {
411    margin: 0;
412    letter-spacing: 0;
413  }
414
415  h1 {
416    max-width: 920px;
417    font-size: 72px;
418    line-height: 0.95;
419  }
420
421  h2 {
422    font-size: 20px;
423  }
424
425  .meta-grid,
426  .source-stats {
427    display: grid;
428    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
429    gap: 12px;
430    margin: 28px 0 0;
431  }
432
433  .meta-grid div,
434  .source-stats div {
435    min-width: 0;
436    border-top: 3px solid var(--accent);
437    padding-top: 10px;
438  }
439
440  dt {
441    color: var(--muted);
442    font-size: 12px;
443    font-weight: 700;
444    text-transform: uppercase;
445  }
446
447  dd {
448    margin: 5px 0 0;
449    min-width: 0;
450    overflow-wrap: anywhere;
451    font-weight: 750;
452  }
453
454  .panel,
455  .source-group {
456    margin-top: 18px;
457    border-radius: 6px;
458    padding: 24px;
459  }
460
461  .section-heading {
462    display: flex;
463    align-items: end;
464    justify-content: space-between;
465    gap: 18px;
466    margin-bottom: 18px;
467  }
468
469  .manifest-grid,
470  .variant-meta {
471    display: grid;
472    gap: 10px;
473  }
474
475  .manifest-grid {
476    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
477  }
478
479  .manifest-grid div,
480  .variant-meta div {
481    min-width: 0;
482  }
483
484  .signal-list,
485  .ranked-list {
486    margin: 0;
487    padding-left: 20px;
488  }
489
490  .signal-list li {
491    margin: 8px 0;
492    color: var(--warn);
493    font-weight: 700;
494    overflow-wrap: anywhere;
495  }
496
497  .empty-state {
498    margin: 0;
499    color: var(--muted);
500  }
501
502  .split-panel {
503    display: grid;
504    grid-template-columns: repeat(2, minmax(0, 1fr));
505    gap: 28px;
506  }
507
508  .ranked-list {
509    display: grid;
510    gap: 8px;
511    margin-top: 14px;
512  }
513
514  .ranked-list li {
515    display: flex;
516    justify-content: space-between;
517    gap: 18px;
518    border-bottom: 1px solid var(--line);
519    padding-bottom: 8px;
520  }
521
522  .ranked-list span {
523    min-width: 0;
524    overflow-wrap: anywhere;
525  }
526
527  .ranked-list strong {
528    flex: 0 0 auto;
529  }
530
531  .source-group {
532    display: grid;
533    grid-template-columns: minmax(280px, 380px) minmax(0, 1fr);
534    gap: 22px;
535  }
536
537  .source-intro {
538    grid-column: 1 / -1;
539    display: grid;
540    grid-template-columns: minmax(0, 1fr) minmax(260px, 0.8fr);
541    gap: 24px;
542  }
543
544  .source-intro h2 {
545    overflow-wrap: anywhere;
546  }
547
548  .source-preview,
549  .variant-image {
550    display: grid;
551    place-items: center;
552    min-height: 180px;
553    overflow: hidden;
554    border: 1px solid var(--line);
555    background: #ece7dc;
556  }
557
558  .source-preview {
559    align-self: start;
560  }
561
562  img {
563    display: block;
564    width: 100%;
565    height: auto;
566    max-height: 520px;
567    object-fit: contain;
568  }
569
570  .source-preview img {
571    max-height: 420px;
572  }
573
574  .variant-grid {
575    display: grid;
576    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
577    gap: 14px;
578  }
579
580  .variant-card {
581    min-width: 0;
582    overflow: hidden;
583    border: 1px solid var(--line);
584    border-radius: 6px;
585    background: var(--panel);
586  }
587
588  .variant-body {
589    display: grid;
590    gap: 12px;
591    padding: 14px;
592  }
593
594  .variant-title {
595    display: flex;
596    align-items: center;
597    justify-content: space-between;
598    gap: 10px;
599  }
600
601  .variant-title span {
602    border: 1px solid var(--line);
603    border-radius: 999px;
604    padding: 3px 8px;
605    color: var(--accent-ink);
606    font-size: 12px;
607    font-weight: 800;
608    text-transform: uppercase;
609  }
610
611  .path-link {
612    color: var(--accent-ink);
613    overflow-wrap: anywhere;
614    font-size: 12px;
615    font-weight: 700;
616    text-decoration-thickness: 2px;
617    text-underline-offset: 3px;
618  }
619
620  @media (max-width: 860px) {
621    .page {
622      width: min(100% - 24px, 720px);
623      padding-top: 20px;
624    }
625
626    .hero,
627    .panel,
628    .source-group {
629      padding: 18px;
630    }
631
632    h1 {
633      font-size: 42px;
634    }
635
636    .split-panel,
637    .source-group,
638    .source-intro {
639      grid-template-columns: 1fr;
640    }
641  }
642"#;
643
644#[cfg(test)]
645#[path = "review_tests.rs"]
646mod tests;