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
pub mod language_type;
pub mod languages;
mod syntax;
use std::{collections::BTreeMap, mem, ops::AddAssign};
pub use self::{language_type::*, languages::Languages};
use crate::{
sort::Sort::{self, *},
stats::Report,
};
#[derive(Clone, Debug, Deserialize, Default, PartialEq, Serialize)]
pub struct Language {
pub blanks: usize,
pub code: usize,
pub comments: usize,
pub reports: Vec<Report>,
pub children: BTreeMap<LanguageType, Vec<Report>>,
pub inaccurate: bool,
}
impl Language {
pub fn new() -> Self {
Self::default()
}
#[inline]
pub fn lines(&self) -> usize {
self.blanks + self.code + self.comments
}
pub fn add_report(&mut self, report: Report) {
for (lang, stats) in &report.stats.blobs {
let mut new_report = Report::new(report.name.clone());
new_report.stats = stats.clone();
self.children.entry(*lang).or_default().push(new_report);
}
self.reports.push(report);
}
#[inline]
pub fn mark_inaccurate(&mut self) {
self.inaccurate = true;
}
pub fn summarise(&self) -> Language {
let mut summary = self.clone();
for reports in self.children.values() {
for stats in reports.iter().map(|r| r.stats.summarise()) {
summary.comments += stats.comments;
summary.code += stats.code;
summary.blanks += stats.blanks;
}
}
summary
}
pub fn total(&mut self) {
let mut blanks = 0;
let mut code = 0;
let mut comments = 0;
for report in &self.reports {
blanks += report.stats.blanks;
code += report.stats.code;
comments += report.stats.comments;
}
self.blanks = blanks;
self.code = code;
self.comments = comments;
}
pub fn is_empty(&self) -> bool {
self.code == 0 && self.comments == 0 && self.blanks == 0 && self.children.is_empty()
}
pub fn sort_by(&mut self, category: Sort) {
match category {
Blanks => self
.reports
.sort_by(|a, b| b.stats.blanks.cmp(&a.stats.blanks)),
Comments => self
.reports
.sort_by(|a, b| b.stats.comments.cmp(&a.stats.comments)),
Code => self.reports.sort_by(|a, b| b.stats.code.cmp(&a.stats.code)),
Files => self.reports.sort_by(|a, b| a.name.cmp(&b.name)),
Lines => self
.reports
.sort_by(|a, b| b.stats.lines().cmp(&a.stats.lines())),
}
}
}
impl AddAssign for Language {
fn add_assign(&mut self, mut rhs: Self) {
self.comments += rhs.comments;
self.blanks += rhs.blanks;
self.code += rhs.code;
self.reports
.extend(mem::replace(&mut rhs.reports, Vec::new()));
self.children
.extend(mem::replace(&mut rhs.children, BTreeMap::new()));
self.inaccurate |= rhs.inaccurate
}
}