devimg-core 0.2.5

Core image pipeline logic for devimg
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
use std::fs;
use std::path::{Path, PathBuf};

use serde::Serialize;

use crate::check::{check_with_options, CheckOptions};
use crate::config::{project_relative, resolve_project_path_checked, Config};
use crate::framework::{inspect_frameworks, FrameworkWarning};
use crate::manifest::{
    manifest_export_to_json, manifest_export_to_typescript_with_options, read_manifest,
    ManifestExportOptions, ManifestTypescriptOptions,
};
use crate::pipeline::{path_to_string, CheckIssue};
use crate::warnings::warning_info;
use crate::{build_plan, scan_sources, DevimgError, Result};

const DEFAULT_CONFIG_PATH: &str = "devimg.toml";

#[derive(Debug, Clone, Default)]
pub struct DoctorOptions {
    pub manifest_export: Option<DoctorManifestExportOptions>,
}

#[derive(Debug, Clone)]
pub struct DoctorManifestExportOptions {
    pub output: PathBuf,
    pub format: DoctorManifestExportFormat,
    pub strip_prefix: Option<String>,
    pub url_prefix: String,
    pub typescript_helpers: bool,
}

#[derive(Debug, Clone, Copy)]
pub enum DoctorManifestExportFormat {
    Json,
    Typescript,
}

#[derive(Debug, Clone, Serialize)]
pub struct DoctorReport {
    pub status: String,
    pub config_path: String,
    pub project_root: String,
    pub manifest_path: String,
    pub report_path: String,
    pub frameworks: Vec<String>,
    pub manifest_helpers: Vec<String>,
    pub source_image_count: usize,
    pub planned_variant_count: usize,
    pub generated_variant_count: usize,
    pub source_bytes: u64,
    pub output_bytes: u64,
    pub budget: DoctorBudget,
    pub checks: Vec<DoctorCheck>,
    pub warnings: Vec<DoctorDiagnostic>,
    pub acknowledged_warnings: Vec<DoctorDiagnostic>,
    pub issues: Vec<DoctorDiagnostic>,
    pub next_command: String,
}

#[derive(Debug, Clone, Serialize)]
pub struct DoctorBudget {
    pub status: String,
    pub max_total_bytes: Option<u64>,
    pub max_file_bytes: Option<u64>,
    pub total_output_bytes: u64,
    pub total_headroom_bytes: Option<i64>,
}

#[derive(Debug, Clone, Serialize)]
pub struct DoctorCheck {
    pub name: String,
    pub status: String,
    pub message: String,
}

#[derive(Debug, Clone, Serialize)]
pub struct DoctorDiagnostic {
    pub code: String,
    pub path: String,
    pub message: String,
    pub hint: String,
}

impl DoctorReport {
    pub fn passed(&self) -> bool {
        self.issues.is_empty()
    }
}

pub fn doctor(config: &Config, options: DoctorOptions) -> Result<DoctorReport> {
    let config_path = path_to_string(&config.path);
    let project_root = display_path(&config.project.root);
    let manifest_path =
        resolve_project_path_checked(config, &config.project.manifest, "manifest path")?;
    let report_path = resolve_project_path_checked(config, &config.project.report, "report path")?;
    let manifest_project_path = path_to_string(&project_relative(config, &manifest_path));
    let report_project_path = path_to_string(&project_relative(config, &report_path));
    let mut checks = Vec::new();
    let mut warnings = Vec::new();
    let mut acknowledged_warnings = Vec::new();
    let mut issues = Vec::new();
    let manifest_export_output = options
        .manifest_export
        .as_ref()
        .map(|export| export.output.as_path());

    checks.push(pass_check(
        "config",
        format!("loaded {}", config.path.display()),
    ));

    inspect_source_dirs(config, &mut checks, &mut warnings, &mut issues)?;
    let framework_inspection = inspect_frameworks(config, manifest_export_output);
    if framework_inspection.frameworks.is_empty() {
        checks.push(pass_check("framework", "no supported framework detected"));
    } else {
        checks.push(pass_check(
            "framework",
            format!("detected {}", framework_inspection.frameworks.join(", ")),
        ));
        checks.push(pass_check(
            "framework_consumption",
            "use generated variants through img/picture, framework Image with unoptimized when DevImg owns sizing and quality, or intentionally layer framework optimization on DevImg-generated source variants",
        ));
    }
    for warning in framework_inspection.warnings {
        warnings.push(framework_warning_diagnostic(warning));
    }

    let mut source_image_count = 0usize;
    let mut planned_variant_count = 0usize;
    let mut generated_variant_count = 0usize;
    let mut source_bytes = 0u64;
    let mut output_bytes = 0u64;
    let mut budget_status = "not evaluated".to_string();

    if issues.is_empty() {
        match scan_sources(config) {
            Ok(sources) => {
                source_image_count = sources.len();
                source_bytes = unique_source_bytes(&sources);
                if source_image_count == 0 {
                    issues.push(diagnostic(
                        "empty_sources",
                        ".",
                        "no source images matched the configured sources",
                        "Add source images, or update `include`/`exclude` in devimg.toml.",
                    ));
                }

                match build_plan(config, &sources) {
                    Ok(plan) => {
                        planned_variant_count = plan.operations.len();
                        checks.push(pass_check(
                            "plan",
                            format!("planned {planned_variant_count} variant(s)"),
                        ));
                    }
                    Err(error) => issues.push(error_diagnostic(
                        "plan_failed",
                        config_path.as_str(),
                        &error,
                        "Fix the plan error, then run `devimg doctor` again.",
                    )),
                }
            }
            Err(error) => issues.push(error_diagnostic(
                "source_scan_failed",
                config_path.as_str(),
                &error,
                "Fix the source image or config problem, then run `devimg doctor` again.",
            )),
        }
    }

    if manifest_path.exists() {
        checks.push(pass_check(
            "manifest",
            format!("found {manifest_project_path}"),
        ));
    } else {
        checks.push(fail_check(
            "manifest",
            format!("missing {manifest_project_path}"),
        ));
    }

    if report_path.exists() {
        checks.push(pass_check("report", format!("found {report_project_path}")));
    } else {
        warnings.push(diagnostic(
            "missing_report",
            report_project_path.as_str(),
            "Markdown report is missing",
            format!("Refresh it with `{}`.", check_command(&config_path)),
        ));
    }

    if issues.is_empty() {
        match check_with_options(
            config,
            CheckOptions {
                write_report: false,
            },
        ) {
            Ok(check_result) => {
                generated_variant_count = check_result.result.manifest.outputs.len();
                output_bytes = check_result.result.output_bytes;
                budget_status = check_result.result.budget_status.clone();
                for warning in check_result.result.warnings {
                    warnings.push(warning_diagnostic(&warning, &config_path));
                }
                for warning in check_result.result.acknowledged_warnings {
                    acknowledged_warnings
                        .push(acknowledged_warning_diagnostic(&warning, &config_path));
                }
                for issue in check_result.result.issues {
                    issues.push(check_issue_diagnostic(&issue, &config_path));
                }
                if check_result.passed {
                    checks.push(pass_check(
                        "outputs",
                        format!("all {generated_variant_count} generated variant(s) are current"),
                    ));
                } else {
                    checks.push(fail_check(
                        "outputs",
                        "generated outputs are missing, stale, modified, or over budget",
                    ));
                }
            }
            Err(error) => issues.push(error_diagnostic(
                "check_failed",
                config_path.as_str(),
                &error,
                "Fix the check error, then run `devimg doctor` again.",
            )),
        }
    }

    if let Some(export_options) = options.manifest_export {
        inspect_manifest_export(&manifest_path, export_options, &mut checks, &mut issues)?;
    }

    let budget = DoctorBudget {
        status: budget_status,
        max_total_bytes: config.budgets.max_total_bytes,
        max_file_bytes: config.budgets.max_file_bytes,
        total_output_bytes: output_bytes,
        total_headroom_bytes: config
            .budgets
            .max_total_bytes
            .map(|max| max as i64 - output_bytes as i64),
    };
    let next_command = next_command(config, &issues, &warnings);
    let status = if issues.is_empty() { "pass" } else { "fail" }.to_string();

    Ok(DoctorReport {
        status,
        config_path,
        project_root,
        manifest_path: manifest_project_path,
        report_path: report_project_path,
        frameworks: framework_inspection.frameworks,
        manifest_helpers: framework_inspection.manifest_helpers,
        source_image_count,
        planned_variant_count,
        generated_variant_count,
        source_bytes,
        output_bytes,
        budget,
        checks,
        warnings,
        acknowledged_warnings,
        issues,
        next_command,
    })
}

pub fn doctor_report_to_json(report: &DoctorReport) -> String {
    serde_json::to_string_pretty(report).expect("doctor report serialization cannot fail") + "\n"
}

fn inspect_source_dirs(
    config: &Config,
    checks: &mut Vec<DoctorCheck>,
    warnings: &mut Vec<DoctorDiagnostic>,
    issues: &mut Vec<DoctorDiagnostic>,
) -> Result<()> {
    for source in &config.sources {
        let input_root = resolve_project_path_checked(config, &source.input, "source input")?;
        let output_root = resolve_project_path_checked(config, &source.output, "source output")?;
        let input_project_path = path_to_string(&project_relative(config, &input_root));
        let output_project_path = path_to_string(&project_relative(config, &output_root));

        if !input_root.exists() {
            checks.push(fail_check(
                "sources",
                format!(
                    "source `{}` input is missing: {}",
                    source.name, input_project_path
                ),
            ));
            issues.push(diagnostic(
                "missing_source_dir",
                input_project_path,
                format!("source `{}` input directory does not exist", source.name),
                "Create the directory, add images, or update `input` in devimg.toml.",
            ));
            continue;
        }
        if !input_root.is_dir() {
            checks.push(fail_check(
                "sources",
                format!(
                    "source `{}` input is not a directory: {}",
                    source.name, input_project_path
                ),
            ));
            issues.push(diagnostic(
                "invalid_source_dir",
                input_project_path,
                format!("source `{}` input is not a directory", source.name),
                "Point `input` at a directory, then run `devimg doctor` again.",
            ));
            continue;
        }

        checks.push(pass_check(
            "sources",
            format!(
                "source `{}` input exists: {}",
                source.name, input_project_path
            ),
        ));

        if output_root.starts_with(&input_root) {
            warnings.push(diagnostic(
                "output_inside_source",
                output_project_path,
                format!(
                    "source `{}` output directory is inside its input directory",
                    source.name
                ),
                "This is supported, but keep generated paths excluded from source globs.",
            ));
        } else if input_root.starts_with(&output_root) {
            warnings.push(diagnostic(
                "source_inside_output",
                input_project_path,
                format!(
                    "source `{}` input directory is inside its output directory",
                    source.name
                ),
                "Move input or output paths apart to avoid accidentally scanning generated files.",
            ));
        }
    }
    Ok(())
}

fn inspect_manifest_export(
    manifest_path: &Path,
    export_options: DoctorManifestExportOptions,
    checks: &mut Vec<DoctorCheck>,
    issues: &mut Vec<DoctorDiagnostic>,
) -> Result<()> {
    let manifest = match read_manifest(manifest_path) {
        Ok(manifest) => manifest,
        Err(error) => {
            issues.push(error_diagnostic(
                "manifest_export_unavailable",
                path_to_string(manifest_path),
                &error,
                "Run `devimg optimize --config <path>` before checking manifest export drift.",
            ));
            checks.push(fail_check(
                "manifest_export",
                "manifest export could not be rendered",
            ));
            return Ok(());
        }
    };
    let output = export_options.output;
    let output_label = path_to_string(&output);
    let options = ManifestExportOptions {
        strip_prefix: export_options.strip_prefix,
        url_prefix: export_options.url_prefix,
    };
    let rendered = match export_options.format {
        DoctorManifestExportFormat::Json => manifest_export_to_json(&manifest, &options),
        DoctorManifestExportFormat::Typescript => manifest_export_to_typescript_with_options(
            &manifest,
            &options,
            &ManifestTypescriptOptions {
                include_helpers: export_options.typescript_helpers,
            },
        ),
    };

    match fs::read(&output) {
        Ok(current) if current == rendered.as_bytes() => checks.push(pass_check(
            "manifest_export",
            format!("export is up to date: {output_label}"),
        )),
        Ok(_) => {
            checks.push(fail_check(
                "manifest_export",
                format!("export is stale: {output_label}"),
            ));
            issues.push(diagnostic(
                "manifest_export_stale",
                output_label,
                "manifest export differs from the current manifest",
                "Regenerate it with `devimg manifest export --manifest <manifest> --output <file>`.",
            ));
        }
        Err(source) if source.kind() == std::io::ErrorKind::NotFound => {
            checks.push(fail_check(
                "manifest_export",
                format!("export is missing: {output_label}"),
            ));
            issues.push(diagnostic(
                "manifest_export_missing",
                output_label,
                "manifest export file does not exist",
                "Generate it with `devimg manifest export --manifest <manifest> --output <file>`.",
            ));
        }
        Err(source) => return Err(DevimgError::io(output, source)),
    }
    Ok(())
}

fn unique_source_bytes(sources: &[crate::SourceImage]) -> u64 {
    let mut seen = Vec::<(&str, u64)>::new();
    for source in sources {
        if !seen.iter().any(|(path, _)| *path == source.project_path) {
            seen.push((&source.project_path, source.bytes));
        }
    }
    seen.iter().map(|(_, bytes)| *bytes).sum()
}

fn display_path(path: &Path) -> String {
    let rendered = path_to_string(path);
    if rendered.is_empty() {
        ".".to_string()
    } else {
        rendered
    }
}

fn check_issue_diagnostic(issue: &CheckIssue, config_path: &str) -> DoctorDiagnostic {
    let hint = match issue.kind.as_str() {
        "missing_manifest" | "missing" | "modified" | "stale" | "outdated_config"
        | "invalid_output" => format!(
            "Regenerate outputs with `{}`.",
            optimize_command(config_path)
        ),
        "oversized_file" | "oversized_total" => {
            "Reduce generated image bytes or update budgets, then run `devimg check` again."
                .to_string()
        }
        _ => "Review the issue, then run `devimg doctor` again.".to_string(),
    };
    diagnostic(
        issue.kind.as_str(),
        issue.path.as_str(),
        issue.message.as_str(),
        hint,
    )
}

fn warning_diagnostic(warning: &str, config_path: &str) -> DoctorDiagnostic {
    let info = warning_info(warning);
    let path = info
        .source
        .or(info.output)
        .unwrap_or_else(|| config_path.to_string());
    if info.code.starts_with("quality:") {
        return diagnostic(
            info.code,
            path,
            warning,
            "Tune preset quality, fit/crop, widths, or source assets in devimg.toml, then run `devimg doctor` again.",
        );
    }
    diagnostic(
        info.code,
        path,
        warning,
        "Review the preset/source config, then run `devimg doctor` again.",
    )
}

fn acknowledged_warning_diagnostic(warning: &str, config_path: &str) -> DoctorDiagnostic {
    let mut diagnostic = warning_diagnostic(warning, config_path);
    diagnostic.hint =
        "Acknowledged in devimg.toml. Keep the acknowledgement narrow and revisit it when the source image or preset changes."
            .to_string();
    diagnostic
}

fn framework_warning_diagnostic(warning: FrameworkWarning) -> DoctorDiagnostic {
    diagnostic(warning.code, warning.path, warning.message, warning.hint)
}

fn error_diagnostic(
    code: impl Into<String>,
    path: impl Into<String>,
    error: &DevimgError,
    hint: impl Into<String>,
) -> DoctorDiagnostic {
    diagnostic(code, path, error.to_string(), hint)
}

fn diagnostic(
    code: impl Into<String>,
    path: impl Into<String>,
    message: impl Into<String>,
    hint: impl Into<String>,
) -> DoctorDiagnostic {
    DoctorDiagnostic {
        code: code.into(),
        path: path.into(),
        message: message.into(),
        hint: hint.into(),
    }
}

fn pass_check(name: impl Into<String>, message: impl Into<String>) -> DoctorCheck {
    DoctorCheck {
        name: name.into(),
        status: "pass".to_string(),
        message: message.into(),
    }
}

fn fail_check(name: impl Into<String>, message: impl Into<String>) -> DoctorCheck {
    DoctorCheck {
        name: name.into(),
        status: "fail".to_string(),
        message: message.into(),
    }
}

fn next_command(
    config: &Config,
    issues: &[DoctorDiagnostic],
    warnings: &[DoctorDiagnostic],
) -> String {
    let config_path = path_to_string(&config.path);
    if issues.iter().any(|issue| {
        matches!(
            issue.code.as_str(),
            "missing_manifest"
                | "missing"
                | "modified"
                | "stale"
                | "outdated_config"
                | "invalid_output"
        )
    }) {
        return optimize_command(&config_path);
    }
    if issues
        .iter()
        .any(|issue| issue.code.starts_with("manifest_export_"))
    {
        return "devimg manifest export --manifest <manifest> --output <file>".to_string();
    }
    if issues.iter().any(|issue| {
        matches!(
            issue.code.as_str(),
            "oversized_file" | "oversized_total" | "empty_sources" | "missing_source_dir"
        )
    }) {
        return doctor_command(&config_path);
    }
    if warnings
        .iter()
        .any(|warning| warning.code == "missing_report")
    {
        return check_command(&config_path);
    }
    check_command(&config_path)
}

fn optimize_command(config_path: &str) -> String {
    format!(
        "devimg optimize{} --allow-overwrite",
        config_option(config_path)
    )
}

fn check_command(config_path: &str) -> String {
    format!("devimg check{}", config_option(config_path))
}

fn doctor_command(config_path: &str) -> String {
    format!("devimg doctor{}", config_option(config_path))
}

fn config_option(config_path: &str) -> String {
    if config_path == DEFAULT_CONFIG_PATH {
        String::new()
    } else {
        format!(" --config {}", shell_arg(config_path))
    }
}

fn shell_arg(value: &str) -> String {
    if !value.is_empty()
        && value
            .chars()
            .all(|ch| ch.is_ascii_alphanumeric() || matches!(ch, '/' | '.' | '_' | '-' | ':' | '@'))
    {
        value.to_string()
    } else {
        format!("'{}'", value.replace('\'', "'\\''"))
    }
}