-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathdefs.rs
More file actions
288 lines (252 loc) · 8.52 KB
/
Copy pathdefs.rs
File metadata and controls
288 lines (252 loc) · 8.52 KB
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
use crossbeam_channel::{Receiver, Sender};
use rustc_hash::FxHashMap;
use serde::ser::{Serialize, Serializer};
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::fmt::{Display, Formatter};
use std::path::PathBuf;
use std::rc::Rc;
use std::sync::Mutex;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Function {
pub start: u32,
pub executed: bool,
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct CovResult {
pub lines: BTreeMap<u32, u64>,
pub branches: BTreeMap<u32, Vec<bool>>,
pub functions: FunctionMap,
}
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum ItemFormat {
Gcno,
Profraw,
Profdata,
Info,
JacocoXml,
Gocov,
}
#[derive(Debug)]
pub struct GcnoBuffers {
pub stem: String,
pub gcno_buf: Vec<u8>,
pub gcda_buf: Vec<Vec<u8>>,
}
#[derive(Debug)]
pub enum ItemType {
Path((String, PathBuf)),
Paths(Vec<PathBuf>),
Content(Vec<u8>),
Buffers(GcnoBuffers),
}
#[derive(Debug)]
pub struct WorkItem {
pub format: ItemFormat,
pub item: ItemType,
pub name: String,
}
pub type FunctionMap = FxHashMap<String, Function>;
pub type JobReceiver = Receiver<Option<WorkItem>>;
pub type JobSender = Sender<Option<WorkItem>>;
pub type CovResultMap = FxHashMap<String, CovResult>;
pub type SyncCovResultMap = Mutex<CovResultMap>;
pub type ResultTuple = (PathBuf, PathBuf, CovResult);
#[derive(Debug, Default)]
pub struct CDStats {
pub total: usize,
pub covered: usize,
pub missed: usize,
pub percent: f64,
}
#[derive(Debug)]
pub struct CDFileStats {
pub name: String,
pub stats: CDStats,
pub coverage: Vec<i64>,
}
#[derive(Debug, Default)]
pub struct CDDirStats {
pub name: String,
pub files: Vec<CDFileStats>,
pub dirs: Vec<Rc<RefCell<CDDirStats>>>,
pub stats: CDStats,
}
#[derive(Debug)]
pub struct HtmlItem {
pub abs_path: PathBuf,
pub rel_path: PathBuf,
pub result: CovResult,
}
#[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct HtmlStats {
pub total_lines: usize,
pub covered_lines: usize,
pub total_funs: usize,
pub covered_funs: usize,
pub total_branches: usize,
pub covered_branches: usize,
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct HtmlFileStats {
pub stats: HtmlStats,
pub abs_prefix: Option<PathBuf>,
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct HtmlDirStats {
pub files: BTreeMap<String, HtmlFileStats>,
pub stats: HtmlStats,
pub abs_prefix: Option<PathBuf>,
}
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct HtmlGlobalStats {
pub dirs: BTreeMap<String, HtmlDirStats>,
pub stats: HtmlStats,
pub abs_prefix: Option<PathBuf>,
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
pub enum HtmlItemStats {
Directory(HtmlDirStats),
File(HtmlFileStats),
}
impl HtmlGlobalStats {
pub fn list(&self, dir: &str) -> BTreeMap<String, HtmlItemStats> {
let mut result = BTreeMap::new();
// Add files from the specified directory
if let Some(dir_stats) = self.dirs.get(dir) {
for (file_name, file_stats) in &dir_stats.files {
result.insert(file_name.clone(), HtmlItemStats::File(file_stats.clone()));
}
}
// Add subdirectories as entries
if dir.is_empty() {
// For root directory, add top-level directories
for (dir_path, dir_stats) in &self.dirs {
if !dir_path.is_empty() && !dir_path.contains('/') {
result.insert(
dir_path.clone(),
HtmlItemStats::Directory(dir_stats.clone()),
);
}
}
} else {
// For specific directory, add immediate subdirectories
let prefix = if dir.ends_with('/') {
dir.to_string()
} else {
format!("{}/", dir)
};
for (dir_path, dir_stats) in &self.dirs {
if dir_path.starts_with(&prefix) {
let suffix = &dir_path[prefix.len()..];
if !suffix.is_empty() && !suffix.contains('/') {
result.insert(
suffix.to_string(),
HtmlItemStats::Directory(dir_stats.clone()),
);
}
}
}
}
result
}
}
pub type HtmlJobReceiver = Receiver<Option<HtmlItem>>;
pub type HtmlJobSender = Sender<Option<HtmlItem>>;
pub enum StringOrRef<'a> {
S(String),
R(&'a String),
}
impl Display for StringOrRef<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
StringOrRef::S(s) => write!(f, "{s}"),
StringOrRef::R(s) => write!(f, "{s}"),
}
}
}
impl Serialize for StringOrRef<'_> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
StringOrRef::S(s) => serializer.serialize_str(s),
StringOrRef::R(s) => serializer.serialize_str(s),
}
}
}
pub struct JacocoReport {
pub lines: BTreeMap<u32, u64>,
pub branches: BTreeMap<u32, Vec<bool>>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_html_global_stats_list() {
let global_json = serde_json::json!({
"dirs": {
"": {
"files": {
"build.rs": {
"stats": {"total_lines": 30, "covered_lines": 25, "total_funs": 3, "covered_funs": 2, "total_branches": 6, "covered_branches": 5},
"abs_prefix": null
}
},
"stats": {"total_lines": 30, "covered_lines": 25, "total_funs": 3, "covered_funs": 2, "total_branches": 6, "covered_branches": 5},
"abs_prefix": null
},
"src": {
"files": {
"lib.rs": {
"stats": {"total_lines": 50, "covered_lines": 40, "total_funs": 5, "covered_funs": 4, "total_branches": 10, "covered_branches": 8},
"abs_prefix": null
}
},
"stats": {"total_lines": 100, "covered_lines": 80, "total_funs": 10, "covered_funs": 8, "total_branches": 20, "covered_branches": 16},
"abs_prefix": null
},
"src/utils": {
"files": {
"mod.rs": {
"stats": {"total_lines": 50, "covered_lines": 40, "total_funs": 5, "covered_funs": 4, "total_branches": 10, "covered_branches": 8},
"abs_prefix": null
}
},
"stats": {"total_lines": 50, "covered_lines": 40, "total_funs": 5, "covered_funs": 4, "total_branches": 10, "covered_branches": 8},
"abs_prefix": null
}
},
"stats": {"total_lines": 130, "covered_lines": 105, "total_funs": 13, "covered_funs": 10, "total_branches": 26, "covered_branches": 21},
"abs_prefix": null
});
let global: HtmlGlobalStats = serde_json::from_value(global_json).unwrap();
let root_items = global.list("");
assert_eq!(root_items.len(), 2);
assert!(root_items.contains_key("build.rs"));
assert!(root_items.contains_key("src"));
// Check that build.rs is a file and src is a directory
match root_items.get("build.rs").unwrap() {
HtmlItemStats::File(_) => {}
HtmlItemStats::Directory(_) => panic!("build.rs should be a file"),
}
match root_items.get("src").unwrap() {
HtmlItemStats::Directory(_) => {}
HtmlItemStats::File(_) => panic!("src should be a directory"),
}
let src_items = global.list("src");
assert_eq!(src_items.len(), 2);
assert!(src_items.contains_key("lib.rs"));
assert!(src_items.contains_key("utils"));
// Check that utils is a directory
match src_items.get("utils").unwrap() {
HtmlItemStats::Directory(_) => {}
HtmlItemStats::File(_) => panic!("utils should be a directory"),
}
let utils_items = global.list("src/utils");
assert_eq!(utils_items.len(), 1);
assert!(utils_items.contains_key("mod.rs"));
}
}