1use nom::bytes::complete::{tag, take_until1};
2use nom::IResult;
3
4pub fn source_file_path(input: &str) -> IResult<&str, &str> {
5 let (file_path, _) = tag("SF:")(input)?;
6 Ok(("", file_path))
7}
8
9pub fn function_name(input: &str) -> IResult<&str, (usize, &str)> {
10 let (input, _) = tag("FN:")(input)?;
11 let (input, line_number) = take_until1(",")(input)?;
12 let (name, _) = tag(",")(input)?;
13
14 Ok(("", (line_number.parse::<usize>().unwrap(), name)))
15}
16
17pub fn function_hit_count(input: &str) -> IResult<&str, (usize, &str)> {
18 let (input, _) = tag("FNDA:")(input)?;
19 let (input, line_number) = take_until1(",")(input)?;
20 let (name, _) = tag(",")(input)?;
21
22 Ok(("", (line_number.parse::<usize>().unwrap(), name)))
23}
24
25fn tag_number<'i>(input: &'i str, t: &'_ str) -> IResult<&'i str, usize> {
26 let (found, _) = tag(t)(input)?;
27 Ok(("", found.parse::<usize>().unwrap()))
28}
29
30pub fn functions_found(input: &str) -> IResult<&str, usize> {
31 tag_number(input, "FNF:")
32}
33
34pub fn functions_hit(input: &str) -> IResult<&str, usize> {
35 tag_number(input, "FNH:")
36}
37
38pub fn lines_found(input: &str) -> IResult<&str, usize> {
53 tag_number(input, "LF:")
54}
55
56pub fn lines_hit(input: &str) -> IResult<&str, usize> {
57 tag_number(input, "LH:")
58}
59
60pub fn branches_found(input: &str) -> IResult<&str, usize> {
61 tag_number(input, "BRF:")
62}
63
64pub fn branches_hit(input: &str) -> IResult<&str, usize> {
65 tag_number(input, "BRH:")
66}
67
68#[cfg(test)]
69mod tests {
70 use super::*;
71
72 #[test]
73 fn test_source_file_path() {
74 let input = "SF:/home/thvdveld/source/vub/smoltcp/src/iface/fragmentation.rs";
75
76 let (_, file_name) = source_file_path(input).unwrap();
77
78 assert_eq!(
79 file_name,
80 "/home/thvdveld/source/vub/smoltcp/src/iface/fragmentation.rs"
81 );
82 }
83
84 #[test]
85 fn test_function_name() {
86 let input = "FN:110,_RINvMs2_NtNtCshpVWEOJQZRA_7smoltcp5iface13fragmentationINtB6_15PacketAssemblerpE8add_withpEBa_";
87
88 let (_, (line_number, name)) = function_name(input).unwrap();
89
90 assert_eq!(line_number, 110);
91 assert_eq!(
92 name,
93 "_RINvMs2_NtNtCshpVWEOJQZRA_7smoltcp5iface13fragmentationINtB6_15PacketAssemblerpE8add_withpEBa_"
94 );
95 }
96
97 #[test]
98 fn test_function_hit_count() {
99 let input = "FNDA:0,_RINvMs2_NtNtCshpVWEOJQZRA_7smoltcp5iface13fragmentationINtB6_15PacketAssemblerpE8add_withpEBa_";
100
101 let (_, (hits, name)) = function_hit_count(input).unwrap();
102
103 assert_eq!(hits, 0);
104 assert_eq!(
105 name,
106 "_RINvMs2_NtNtCshpVWEOJQZRA_7smoltcp5iface13fragmentationINtB6_15PacketAssemblerpE8add_withpEBa_"
107 );
108 }
109
110 #[test]
111 fn test_functions_found() {
112 let input = "FNF:38";
113 let (_, found) = functions_found(input).unwrap();
114 assert_eq!(found, 38);
115 }
116
117 #[test]
118 fn test_functions_hit() {
119 let input = "FNH:23";
120 let (_, found) = functions_hit(input).unwrap();
121 assert_eq!(found, 23);
122 }
123
124 #[test]
125 fn test_line_number_hit_count() {
126 let input = "DA:17,0";
127 let (_, (line, hit)) = line_number_hit_count(input).unwrap();
128 assert_eq!(line, 17);
129 assert_eq!(hit, 0);
130 }
131
132 #[test]
133 fn test_branches_found() {
134 let input = "BRF:0";
135 let (_, branches) = branches_found(input).unwrap();
136 assert_eq!(branches, 0);
137 }
138
139 #[test]
140 fn test_branches_hit() {
141 let input = "BRH:0";
142 let (_, branches) = branches_hit(input).unwrap();
143 assert_eq!(branches, 0);
144 }
145}