mnemo-rs 1.6.21

Local-first shell history manager with CLI/TUI, fuzzy search, project/session context, and DevSecOps-focused release hardening.
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
//! Commande `mnemo doctor` : diagnostic de l'installation locale.
//!
//! En mode simple, `doctor` ne modifie **jamais** le système : il se contente
//! d'inspecter la configuration, la base, le `PATH` et le `.bashrc`. Le mode
//! `--fix` répare les éléments manquants de façon non destructive (création de
//! la config / base, ajout du bloc `.bashrc` avec sauvegarde) ; il ne supprime
//! jamais de données.

use anyhow::Result;
use serde::Serialize;
use std::path::{Path, PathBuf};

use crate::{backup, config, db, migrations, shell};

/// Niveau de chaque contrôle.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Status {
    Ok,
    Warn,
    Error,
    Info,
    /// Correction appliquée par `--fix` (non bloquante, distincte d'un simple OK).
    Fix,
}

impl Status {
    fn label(self) -> &'static str {
        match self {
            Status::Ok => "OK",
            Status::Warn => "WARN",
            Status::Error => "ERROR",
            Status::Info => "INFO",
            Status::Fix => "FIX",
        }
    }
}

/// Résultat d'un contrôle individuel.
#[derive(Debug, Clone, Serialize)]
pub struct Check {
    pub name: String,
    pub status: Status,
    pub message: String,
}

impl Check {
    fn new(name: &str, status: Status, message: impl Into<String>) -> Self {
        Self {
            name: name.to_string(),
            status,
            message: message.into(),
        }
    }
}

/// Rapport complet (liste de contrôles).
#[derive(Debug, Default)]
pub struct Report {
    pub checks: Vec<Check>,
}

impl Report {
    fn push(&mut self, name: &str, status: Status, message: impl Into<String>) {
        self.checks.push(Check::new(name, status, message));
    }

    fn count(&self, status: Status) -> usize {
        self.checks.iter().filter(|c| c.status == status).count()
    }

    /// Code de sortie : 1 si au moins une erreur bloquante, sinon 0.
    pub fn exit_code(&self) -> i32 {
        if self.count(Status::Error) > 0 {
            1
        } else {
            0
        }
    }
}

/// Point d'entrée de la commande. Retourne le code de sortie attendu.
pub fn run(fix: bool, json: bool) -> Result<i32> {
    let mut report = Report::default();

    if fix {
        apply_fixes(&mut report)?;
    }

    collect_checks(&mut report)?;

    if json {
        println!("{}", render_json(&report));
    } else {
        render_text(&report);
    }

    Ok(report.exit_code())
}

// ---------------------------------------------------------------------------
// Mode --fix (non destructif).
// ---------------------------------------------------------------------------

fn apply_fixes(report: &mut Report) -> Result<()> {
    let mut fixes = 0usize;

    // 0. Dossiers de données / configuration (créés en amont si absents).
    for (name, dir) in [
        (
            "fix.dir.config",
            config::config_path()?.parent().map(Path::to_path_buf),
        ),
        (
            "fix.dir.data",
            config::db_path()?.parent().map(Path::to_path_buf),
        ),
    ] {
        if let Some(dir) = dir {
            if dir.as_os_str().is_empty() || dir.exists() {
                continue;
            }
            std::fs::create_dir_all(&dir)?;
            config::harden_dir(&dir);
            fixes += 1;
            report.push(
                name,
                Status::Ok,
                format!("Dossier créé : {}", dir.display()),
            );
        }
    }

    // 1. Config.
    let cfg_path = config::config_path()?;
    if cfg_path.exists() {
        report.push("fix.config", Status::Info, "Configuration déjà présente");
    } else {
        config::Config::default().save(&cfg_path)?;
        fixes += 1;
        report.push(
            "fix.config",
            Status::Ok,
            format!("Configuration créée : {}", cfg_path.display()),
        );
    }

    // 2. Base de données (création du schéma si absente).
    let db_path = config::db_path()?;
    let db_existait = db_path.exists();
    // `db::open` durcit silencieusement la base à 600 ; on capture donc l'état
    // des permissions AVANT ouverture pour pouvoir rapporter explicitement la
    // correction dans le résumé.
    #[cfg(unix)]
    let db_trop_ouverte = db_existait && is_too_open(&db_path);
    db::open(&db_path)?;
    if db_existait {
        report.push("fix.db", Status::Info, "Base de données déjà présente");
    } else {
        fixes += 1;
        report.push(
            "fix.db",
            Status::Ok,
            format!("Base de données créée : {}", db_path.display()),
        );
    }

    // 3. Permissions trop permissives sur la config et la base (chmod 600).
    fixes += fix_permissions(report, "fix.config.perms", &cfg_path);
    // La base a déjà été resserrée par `db::open` : on rend la correction
    // explicite plutôt que silencieuse.
    #[cfg(unix)]
    if db_trop_ouverte {
        report.push(
            "fix.db.perms",
            Status::Fix,
            format!("Permissions corrigées : {} → 600", db_path.display()),
        );
        fixes += 1;
    }
    #[cfg(not(unix))]
    {
        fixes += fix_permissions(report, "fix.db.perms", &db_path);
    }

    // 4. Sauvegardes existantes trop ouvertes (chmod 600, résumé unique).
    fixes += fix_backups_permissions(report);

    // 5. Bloc .bashrc : ajout / déduplication / restauration du Ctrl+R.
    if let Some(bashrc) = bashrc_path() {
        match shell::repair_block(&bashrc) {
            Ok(shell::BlockRepair::Created) => {
                fixes += 1;
                report.push(
                    "fix.bashrc",
                    Status::Ok,
                    "Bloc mnemo ajouté au .bashrc (sauvegarde créée)",
                );
            }
            Ok(shell::BlockRepair::Deduplicated) => {
                fixes += 1;
                report.push(
                    "fix.bashrc",
                    Status::Ok,
                    "Bloc mnemo dupliqué supprimé, un seul conservé (sauvegarde créée)",
                );
            }
            Ok(shell::BlockRepair::CtrlRRestored) => {
                fixes += 1;
                report.push(
                    "fix.bashrc",
                    Status::Ok,
                    "Raccourci Ctrl+R restauré dans le bloc mnemo (sauvegarde créée)",
                );
            }
            Ok(shell::BlockRepair::Upgraded) => {
                fixes += 1;
                report.push(
                    "fix.bashrc",
                    Status::Ok,
                    "Bloc mnemo obsolète mis à niveau (sessions activées, sauvegarde créée)",
                );
            }
            Ok(shell::BlockRepair::AlreadyOk) => report.push(
                "fix.bashrc",
                Status::Info,
                "Bloc mnemo déjà présent et complet (aucune modification)",
            ),
            Err(e) => report.push(
                "fix.bashrc",
                Status::Warn,
                format!("Impossible de modifier le .bashrc : {e}"),
            ),
        }
    }

    // 6. PATH : message clair, jamais de modification automatique.
    if let Some(local_bin) = local_bin_dir() {
        if !path_contains(&local_bin) {
            report.push(
                "fix.path",
                Status::Warn,
                format!(
                    "{} n'est pas dans le PATH. Ajoutez à votre ~/.bashrc : export PATH=\"$HOME/.local/bin:$PATH\"",
                    local_bin.display()
                ),
            );
        }
    }

    // Résumé des corrections appliquées.
    if fixes > 0 {
        report.push(
            "fix.summary",
            Status::Ok,
            format!("Corrections appliquées : {fixes}"),
        );
    } else {
        report.push("fix.summary", Status::Info, "Aucune correction nécessaire");
    }

    Ok(())
}

/// Resserre les permissions d'un fichier à `0o600` s'il est accessible au
/// groupe ou aux autres. Retourne `1` si une correction a été appliquée.
fn fix_permissions(report: &mut Report, name: &str, path: &Path) -> usize {
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        if !path.exists() {
            return 0;
        }
        let Ok(meta) = std::fs::metadata(path) else {
            return 0;
        };
        let mode = meta.permissions().mode() & 0o777;
        if mode & 0o077 == 0 {
            return 0;
        }
        let mut perms = meta.permissions();
        perms.set_mode(0o600);
        if let Err(e) = std::fs::set_permissions(path, perms) {
            report.push(name, Status::Warn, format!("Permissions inchangées : {e}"));
            return 0;
        }
        report.push(
            name,
            Status::Fix,
            format!("Permissions corrigées : {} → 600", path.display()),
        );
        1
    }
    #[cfg(not(unix))]
    {
        let _ = (report, name, path);
        0
    }
}

/// Indique si un fichier est accessible au groupe ou aux autres (mode `0o077`).
#[cfg(unix)]
fn is_too_open(path: &Path) -> bool {
    use std::os::unix::fs::PermissionsExt;
    std::fs::metadata(path)
        .map(|m| m.permissions().mode() & 0o077 != 0)
        .unwrap_or(false)
}

/// Resserre à `600` toutes les archives de sauvegarde trop ouvertes.
///
/// Best-effort et non destructif : ne lit ni ne modifie le contenu des
/// archives, ne supprime rien, n'échoue pas si le dossier `backups` est absent.
/// Pousse un unique résumé `[FIX]` indiquant le nombre d'archives corrigées et
/// retourne `1` si au moins une correction a eu lieu (pour le compteur global).
fn fix_backups_permissions(report: &mut Report) -> usize {
    #[cfg(unix)]
    {
        let Ok(dir) = backup::backups_dir() else {
            return 0;
        };
        let archives = backup::list_archives(&dir);
        let mut corrected = 0usize;
        for archive in &archives {
            if is_too_open(archive) {
                config::harden_file(archive);
                corrected += 1;
            }
        }
        if corrected == 0 {
            return 0;
        }
        report.push(
            "fix.backups.perms",
            Status::Fix,
            format!("Permissions corrigées : {corrected} backup(s) → 600"),
        );
        1
    }
    #[cfg(not(unix))]
    {
        let _ = report;
        0
    }
}

// ---------------------------------------------------------------------------
// Contrôles de diagnostic (lecture seule).
// ---------------------------------------------------------------------------

fn collect_checks(report: &mut Report) -> Result<()> {
    check_binary(report);
    check_local_bin_path(report);
    check_config(report)?;
    check_database(report)?;
    check_backups(report);
    check_bashrc(report);
    check_shell(report);
    check_histtimeformat(report);
    Ok(())
}

fn check_binary(report: &mut Report) {
    report.push(
        "binary.version",
        Status::Info,
        format!("mnemo version {}", env!("CARGO_PKG_VERSION")),
    );

    match find_in_path("mnemo") {
        Some(p) => report.push(
            "binary.path",
            Status::Ok,
            format!("Binaire trouvé dans le PATH : {}", p.display()),
        ),
        None => report.push(
            "binary.path",
            Status::Warn,
            "Binaire mnemo introuvable dans le PATH (installez-le dans ~/.local/bin)",
        ),
    }
}

fn check_local_bin_path(report: &mut Report) {
    if let Some(local_bin) = local_bin_dir() {
        if path_contains(&local_bin) {
            report.push(
                "path.local_bin",
                Status::Ok,
                format!("{} est dans le PATH", local_bin.display()),
            );
        } else {
            report.push(
                "path.local_bin",
                Status::Warn,
                format!("{} n'est pas dans le PATH", local_bin.display()),
            );
        }
    }
}

fn check_config(report: &mut Report) -> Result<()> {
    let cfg_path = config::config_path()?;
    if cfg_path.exists() {
        report.push(
            "config.file",
            Status::Ok,
            format!("Configuration présente : {}", cfg_path.display()),
        );
        check_permissions(report, "config.perms", &cfg_path);
    } else {
        report.push(
            "config.file",
            Status::Warn,
            format!(
                "Configuration absente : {} (lancez `mnemo init` ou `mnemo doctor --fix`)",
                cfg_path.display()
            ),
        );
    }

    let cfg = config::Config::load()?;
    if cfg.stats.ignored_commands.is_empty() {
        report.push(
            "config.stats_ignore",
            Status::Info,
            "Aucune commande ignorée dans stats".to_string(),
        );
    } else {
        report.push(
            "config.stats_ignore",
            Status::Info,
            format!(
                "Commandes ignorées dans stats : {}",
                cfg.stats.ignored_commands.join(", ")
            ),
        );
    }
    Ok(())
}

fn check_database(report: &mut Report) -> Result<()> {
    let db_path = config::db_path()?;
    if !db_path.exists() {
        report.push(
            "db.file",
            Status::Warn,
            format!(
                "Base absente : {} (lancez `mnemo import` ou `mnemo doctor --fix`)",
                db_path.display()
            ),
        );
        return Ok(());
    }

    report.push(
        "db.file",
        Status::Ok,
        format!("Base présente : {}", db_path.display()),
    );
    check_permissions(report, "db.perms", &db_path);

    // Ouverture en lecture seule (ne modifie jamais la base).
    let conn = match db::open_readonly(&db_path) {
        Ok(c) => c,
        Err(e) => {
            report.push(
                "db.open",
                Status::Error,
                format!("Base illisible / corrompue : {e}"),
            );
            return Ok(());
        }
    };

    match db::table_exists(&conn, "commands") {
        Ok(true) => {
            report.push("db.table", Status::Ok, "Table `commands` présente");
            check_schema_version(report, &conn);
            match db::count(&conn) {
                Ok(n) => report.push(
                    "db.count",
                    Status::Info,
                    format!("{n} commande(s) enregistrée(s)"),
                ),
                Err(e) => report.push(
                    "db.count",
                    Status::Error,
                    format!("Lecture du nombre de commandes impossible : {e}"),
                ),
            }
        }
        Ok(false) => report.push(
            "db.table",
            Status::Error,
            "Table `commands` absente (base invalide)",
        ),
        Err(e) => report.push(
            "db.open",
            Status::Error,
            format!("Base illisible / corrompue : {e}"),
        ),
    }

    Ok(())
}

/// Contrôle (lecture seule) des permissions des archives de sauvegarde.
///
/// Non bruyant : n'affiche rien si aucune archive n'existe, un résumé `OK`
/// discret si toutes sont en `600`, et un unique résumé `WARN` agrégé (sans
/// lister chaque fichier) si certaines sont trop ouvertes.
fn check_backups(report: &mut Report) {
    #[cfg(unix)]
    {
        let Ok(dir) = backup::backups_dir() else {
            return;
        };
        let archives = backup::list_archives(&dir);
        if archives.is_empty() {
            return;
        }
        let open = archives.iter().filter(|p| is_too_open(p)).count();
        if open > 0 {
            report.push(
                "backups.perms",
                Status::Warn,
                format!("Backups trop ouverts : {open} fichier(s), attendu 600"),
            );
        } else {
            report.push(
                "backups.perms",
                Status::Ok,
                format!("Sauvegardes : {} archive(s) en 600", archives.len()),
            );
        }
    }
    #[cfg(not(unix))]
    {
        let _ = report;
    }
}

/// Vérifie la version du schéma SQLite (`PRAGMA user_version`) sans jamais
/// migrer : `doctor` reste en lecture seule. Signale si une migration est
/// nécessaire, ou si la base provient d'une version plus récente de mnemo.
fn check_schema_version(report: &mut Report, conn: &rusqlite::Connection) {
    let expected = migrations::SCHEMA_VERSION;
    match migrations::schema_version(conn) {
        Ok(current) => {
            report.push(
                "db.schema",
                Status::Info,
                format!("Schéma SQLite : v{current} (attendu v{expected})"),
            );
            if current < expected {
                report.push(
                    "db.schema.migration",
                    Status::Warn,
                    "Migration nécessaire : lancez `mnemo migrate` (ou toute commande mnemo l'applique automatiquement)",
                );
            } else if current > expected {
                report.push(
                    "db.schema.migration",
                    Status::Error,
                    format!(
                        "Base créée par une version plus récente (schéma v{current} > v{expected}) : mettez mnemo à jour"
                    ),
                );
            } else {
                report.push(
                    "db.schema.migration",
                    Status::Ok,
                    "Schéma à jour, aucune migration nécessaire",
                );
            }
        }
        Err(e) => report.push(
            "db.schema",
            Status::Error,
            format!("Lecture de la version de schéma impossible : {e}"),
        ),
    }
}

fn check_bashrc(report: &mut Report) {
    let Some(bashrc) = bashrc_path() else {
        return;
    };

    if !bashrc.exists() {
        report.push(
            "bashrc.file",
            Status::Warn,
            format!("{} introuvable", bashrc.display()),
        );
        return;
    }
    report.push(
        "bashrc.file",
        Status::Ok,
        format!("{} présent", bashrc.display()),
    );

    let content = std::fs::read_to_string(&bashrc).unwrap_or_default();

    if shell::has_block(&content) {
        report.push(
            "bashrc.block",
            Status::Ok,
            "Bloc d'intégration mnemo présent",
        );
        let n = shell::count_blocks(&content);
        if n > 1 {
            report.push(
                "bashrc.duplicate",
                Status::Warn,
                format!("Bloc mnemo dupliqué {n} fois (gardez-en un seul)"),
            );
        } else {
            report.push("bashrc.duplicate", Status::Ok, "Bloc mnemo unique");
        }

        if shell::has_ctrl_r_bind(&content) {
            report.push("bashrc.ctrl_r", Status::Ok, "Raccourci Ctrl+R configuré");
        } else {
            report.push(
                "bashrc.ctrl_r",
                Status::Warn,
                "Raccourci Ctrl+R absent du bloc mnemo",
            );
        }

        if shell::block_state(&content) == shell::BlockState::Legacy {
            report.push(
                "bashrc.version",
                Status::Warn,
                "Bloc d'intégration mnemo obsolète : il ne capture pas MNEMO_SESSION_ID, requis par `mnemo session`. Lancez `mnemo shell upgrade`.",
            );
        } else {
            report.push(
                "bashrc.version",
                Status::Ok,
                "Intégration Bash à jour (sessions activées)",
            );
        }
    } else {
        report.push(
            "bashrc.block",
            Status::Warn,
            "Bloc d'intégration mnemo absent (lancez `mnemo doctor --fix`)",
        );
    }
}

fn check_shell(report: &mut Report) {
    match std::env::var("SHELL") {
        Ok(sh) if sh.ends_with("bash") => {
            report.push("shell.current", Status::Ok, format!("Shell courant : {sh}"))
        }
        Ok(sh) => report.push(
            "shell.current",
            Status::Warn,
            format!("Shell courant : {sh} (mnemo ne supporte que Bash pour l'instant)"),
        ),
        Err(_) => report.push("shell.current", Status::Warn, "Variable $SHELL non définie"),
    }
}

fn check_histtimeformat(report: &mut Report) {
    match std::env::var("HISTTIMEFORMAT") {
        Ok(v) if !v.trim().is_empty() => {
            report.push("shell.histtime", Status::Ok, "HISTTIMEFORMAT est configuré")
        }
        _ => report.push(
            "shell.histtime",
            Status::Info,
            "HISTTIMEFORMAT non configuré : les horodatages d'import seront approximatifs",
        ),
    }
}

/// Vérifie que le fichier n'est pas modifiable par le groupe ou les autres.
fn check_permissions(report: &mut Report, name: &str, path: &Path) {
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        match std::fs::metadata(path) {
            Ok(meta) => {
                let mode = meta.permissions().mode() & 0o777;
                if mode & 0o077 != 0 {
                    report.push(
                        name,
                        Status::Warn,
                        format!(
                            "Permissions trop ouvertes : {} (actuel {:o}, attendu 600)",
                            path.display(),
                            mode
                        ),
                    );
                } else {
                    report.push(
                        name,
                        Status::Ok,
                        format!("Permissions correctes ({:o})", mode),
                    );
                }
            }
            Err(e) => report.push(name, Status::Warn, format!("Permissions illisibles : {e}")),
        }
    }
    #[cfg(not(unix))]
    {
        let _ = (path,);
        report.push(name, Status::Info, "Vérification des permissions ignorée");
    }
}

// ---------------------------------------------------------------------------
// Helpers PATH / chemins.
// ---------------------------------------------------------------------------

fn bashrc_path() -> Option<PathBuf> {
    dirs::home_dir().map(|h| h.join(".bashrc"))
}

fn local_bin_dir() -> Option<PathBuf> {
    dirs::home_dir().map(|h| h.join(".local").join("bin"))
}

fn path_contains(dir: &Path) -> bool {
    std::env::var_os("PATH")
        .map(|paths| std::env::split_paths(&paths).any(|p| p == dir))
        .unwrap_or(false)
}

fn find_in_path(exe: &str) -> Option<PathBuf> {
    let paths = std::env::var_os("PATH")?;
    for dir in std::env::split_paths(&paths) {
        let candidate = dir.join(exe);
        if is_executable(&candidate) {
            return Some(candidate);
        }
    }
    None
}

fn is_executable(path: &Path) -> bool {
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::metadata(path)
            .map(|m| m.is_file() && m.permissions().mode() & 0o111 != 0)
            .unwrap_or(false)
    }
    #[cfg(not(unix))]
    {
        path.is_file()
    }
}

// ---------------------------------------------------------------------------
// Rendu.
// ---------------------------------------------------------------------------

fn render_text(report: &Report) {
    println!("mnemo doctor - rapport de diagnostic");
    println!("------------------------------------");
    for c in &report.checks {
        println!("[{:^5}] {}", c.status.label(), c.message);
    }
    println!("------------------------------------");
    println!(
        "Résumé : {} OK, {} WARN, {} ERROR, {} INFO, {} FIX",
        report.count(Status::Ok),
        report.count(Status::Warn),
        report.count(Status::Error),
        report.count(Status::Info),
        report.count(Status::Fix),
    );
    if report.exit_code() == 0 {
        println!("État global : sain (code 0)");
    } else {
        println!("État global : erreurs détectées (code 1)");
    }
}

fn render_json(report: &Report) -> String {
    #[derive(Serialize)]
    struct Summary {
        ok: usize,
        warn: usize,
        error: usize,
        info: usize,
        fix: usize,
        exit_code: i32,
    }
    #[derive(Serialize)]
    struct Output<'a> {
        summary: Summary,
        checks: &'a [Check],
    }

    let output = Output {
        summary: Summary {
            ok: report.count(Status::Ok),
            warn: report.count(Status::Warn),
            error: report.count(Status::Error),
            info: report.count(Status::Info),
            fix: report.count(Status::Fix),
            exit_code: report.exit_code(),
        },
        checks: &report.checks,
    };

    // La sérialisation ne peut échouer pour ces types simples ; on retombe sur
    // un objet vide par prudence plutôt que de paniquer.
    serde_json::to_string_pretty(&output).unwrap_or_else(|_| "{}".to_string())
}

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

    #[test]
    fn exit_code_depend_des_erreurs() {
        let mut r = Report::default();
        r.push("a", Status::Ok, "ok");
        r.push("b", Status::Warn, "warn");
        assert_eq!(r.exit_code(), 0);
        r.push("c", Status::Error, "boom");
        assert_eq!(r.exit_code(), 1);
    }

    #[test]
    fn json_echappe_les_caracteres_speciaux() {
        let mut r = Report::default();
        r.push("x", Status::Ok, "a\"b\\c\nfin");
        let s = render_json(&r);
        // serde_json doit produire un JSON parseable et correctement échappé.
        let parsed: serde_json::Value = serde_json::from_str(&s).unwrap();
        assert_eq!(parsed["checks"][0]["message"], "a\"b\\c\nfin");
    }

    #[test]
    fn json_est_bien_forme() {
        let mut r = Report::default();
        r.push("x", Status::Ok, "tout va bien");
        let s = render_json(&r);
        let parsed: serde_json::Value = serde_json::from_str(&s).unwrap();
        assert_eq!(parsed["summary"]["ok"], 1);
        assert_eq!(parsed["summary"]["exit_code"], 0);
        assert_eq!(parsed["checks"][0]["status"], "ok");
        assert_eq!(parsed["checks"][0]["name"], "x");
    }
}