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
use self::branch::Branches;
use self::function::Functions;
use self::line::Lines;
use super::{Merge, MergeError, ParseError, Parser, ReadError, Record, RecordKind};
use std::collections::BTreeMap;
use std::iter;
use std::path::PathBuf;
pub mod branch;
pub mod function;
pub mod line;
pub type Sections = BTreeMap<Key, Value>;
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct Key {
pub test_name: String,
pub source_file: PathBuf,
}
#[derive(Debug, Clone, Default, Eq, PartialEq)]
pub struct Value {
pub functions: Functions,
pub branches: Branches,
pub lines: Lines,
}
impl Value {
pub fn is_empty(&self) -> bool {
self.functions.is_empty() && self.branches.is_empty() && self.lines.is_empty()
}
}
impl Merge for Value {
fn merge(&mut self, other: Self) -> Result<(), MergeError> {
self.functions.merge(other.functions)?;
self.branches.merge(other.branches)?;
self.lines.merge(other.lines)?;
Ok(())
}
fn merge_lossy(&mut self, other: Self) {
self.functions.merge_lossy(other.functions);
self.branches.merge_lossy(other.branches);
self.lines.merge_lossy(other.lines);
}
}
pub(crate) fn parse<I>(parser: &mut Parser<I, Record>) -> Result<Sections, ParseError>
where
I: Iterator<Item = Result<Record, ReadError>>,
{
let mut sections = Sections::new();
while parser.peek().map_err(ParseError::Read)?.is_some() {
let mut test_name = None;
while let Some(tn) = eat_if_matches!(parser, Record::TestName { name } => name) {
test_name = Some(tn);
}
if parser.peek().map_err(ParseError::Read)?.is_none() {
break;
}
let mut source_file = None;
let mut functions = Functions::default();
let mut branches = Branches::default();
let mut lines = Lines::default();
loop {
match parser.pop()?.ok_or(ParseError::UnexpectedEof)? {
rec @ Record::TestName { .. } => {
return Err(ParseError::UnexpectedRecord {
expected: RecordKind::EndOfRecord,
found: rec.kind(),
})
}
Record::SourceFile { path } => source_file = Some(path),
Record::FunctionName { name, start_line } => {
let _ = functions.insert(
function::Key { name },
function::Value {
start_line: Some(start_line),
count: 0,
},
);
}
Record::FunctionData { name, count } => {
let data = functions.entry(function::Key { name }).or_default();
data.count += count;
}
Record::FunctionsFound { .. } => {}
Record::FunctionsHit { .. } => {}
Record::BranchData {
line,
block,
branch,
taken,
} => {
let _ = branches.insert(
branch::Key {
line,
block,
branch,
},
branch::Value { taken },
);
}
Record::BranchesFound { .. } => {}
Record::BranchesHit { .. } => {}
Record::LineData {
line,
count,
checksum,
} => {
let _ = lines.insert(line::Key { line }, line::Value { count, checksum });
}
Record::LinesFound { .. } => {}
Record::LinesHit { .. } => {}
Record::EndOfRecord => break,
}
}
let key = Key {
test_name: test_name.unwrap_or_default(),
source_file: source_file.unwrap_or_default(),
};
let value = Value {
functions,
branches,
lines,
};
if !value.is_empty() {
let _ = sections.insert(key, value);
}
}
Ok(sections)
}
pub(crate) fn into_records(sections: Sections) -> Box<dyn Iterator<Item = Record>> {
let iter = sections.into_iter().flat_map(|(key, value)| {
let test_name = Record::TestName {
name: key.test_name,
};
let source_file = Record::SourceFile {
path: key.source_file,
};
iter::once(test_name)
.chain(iter::once(source_file))
.chain(function::into_records(value.functions))
.chain(branch::into_records(value.branches))
.chain(line::into_records(value.lines))
.chain(iter::once(Record::EndOfRecord))
});
Box::new(iter)
}