Skip to main content

cargo_plot/core/path_matcher/
stats.rs

1use crate::core::path_view::{PathGrid, PathList, PathTree, ViewMode};
2
3/// [POL]: Podzbiór wyników zawierający surowe ścieżki i wygenerowane widoki.
4#[derive(Default)]
5pub struct ResultSet {
6    pub paths: Vec<String>,
7    pub tree: Option<PathTree>,
8    pub list: Option<PathList>,
9    pub grid: Option<PathGrid>,
10}
11
12// [ENG]: Simple stats object to avoid manual counting in the Engine.
13// [POL]: Prosty obiekt statystyk, aby uniknąć ręcznego liczenia w Engine.
14#[derive(Default)]
15pub struct MatchStats {
16    pub m_size_matched: usize,
17    pub x_size_mismatched: usize,
18    pub total: usize,
19    pub m_matched: ResultSet,
20    pub x_mismatched: ResultSet,
21}
22
23#[derive(Debug, Clone, Copy, PartialEq)]
24pub enum ShowMode {
25    Include,
26    Exclude,
27    Context,
28}
29
30impl MatchStats {
31    /// : Hermetyzacja renderowania po stronie rdzenia.
32    /// Zwraca gotowy, złożony ciąg znaków, gotowy do wrzucenia w konsolę lub plik.
33    #[must_use]
34    pub fn render_output(
35        &self,
36        view_mode: ViewMode,
37        show_mode: ShowMode,
38        print_info: bool,
39        use_color: bool,
40    ) -> String {
41        let mut out = String::new();
42        let do_include = show_mode == ShowMode::Include || show_mode == ShowMode::Context;
43        let do_exclude = show_mode == ShowMode::Exclude || show_mode == ShowMode::Context;
44
45        match view_mode {
46            ViewMode::Grid => {
47                if do_include && let Some(grid) = &self.m_matched.grid {
48                    if print_info {
49                        out.push_str("✅\n");
50                    }
51                    if use_color {
52                        out.push_str(&grid.render_cli());
53                    } else {
54                        out.push_str(&grid.render_txt());
55                    }
56                }
57                if do_exclude && let Some(grid) = &self.x_mismatched.grid {
58                    if print_info {
59                        out.push_str("❌\n");
60                    }
61                    if use_color {
62                        out.push_str(&grid.render_cli());
63                    } else {
64                        out.push_str(&grid.render_txt());
65                    }
66                }
67            }
68            ViewMode::Tree => {
69                if do_include && let Some(tree) = &self.m_matched.tree {
70                    if print_info {
71                        out.push_str("✅\n");
72                    }
73                    if use_color {
74                        out.push_str(&tree.render_cli());
75                    } else {
76                        out.push_str(&tree.render_txt());
77                    }
78                }
79                if do_exclude && let Some(tree) = &self.x_mismatched.tree {
80                    if print_info {
81                        out.push_str("❌\n");
82                    }
83                    if use_color {
84                        out.push_str(&tree.render_cli());
85                    } else {
86                        out.push_str(&tree.render_txt());
87                    }
88                }
89            }
90            ViewMode::List => {
91                if do_include && let Some(list) = &self.m_matched.list {
92                    if print_info {
93                        out.push_str("✅\n");
94                    }
95                    if use_color {
96                        out.push_str(&list.render_cli(true));
97                    } else {
98                        out.push_str(&list.render_txt());
99                    }
100                }
101                if do_exclude && let Some(list) = &self.x_mismatched.list {
102                    if print_info {
103                        out.push_str("❌\n");
104                    }
105                    if use_color {
106                        out.push_str(&list.render_cli(false));
107                    } else {
108                        out.push_str(&list.render_txt());
109                    }
110                }
111            }
112        }
113
114        out
115    }
116}