lcov_parser/
record.rs

1// Copyright (c) 2015-2016 lcov-parser developers
2//
3// Licensed under the Apache License, Version 2.0
4// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
5// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. All files in the project carrying such notice may not be copied,
7// modified, or distributed except according to those terms.
8
9//! Module of LCOV Record.
10
11use std::convert:: { From };
12use std::option:: { Option };
13use std::io:: { Result };
14use std::io::prelude::*;
15use parser:: { parse_record };
16
17#[derive(Debug, PartialEq, Clone)]
18pub enum LCOVRecord
19{
20    TestName(Option<String>),         // TN:<test name>
21    SourceFile(String),               // SF:<absolute path to the source file>
22    Data(LineData),                   // DA:<line number>,<execution count>[,<checksum>]
23    FunctionName(FunctionName),       // FN:<line number of function start>,<function name> for each function
24    FunctionData(FunctionData),       // FNDA:<execution count>,<function name>
25    FunctionsFound(u32),              // FNF:<number of functions found>
26    FunctionsHit(u32),                // FNH:<number of function hit>
27    LinesHit(u32),                    // LH:<number of lines with an execution count> greater than 0
28    LinesFound(u32),                  // LF:<number of instrumented lines>
29    BranchData(BranchData),           // BRDA:<line number>,<block number>,<branch number>,<taken>
30    BranchesFound(u32),               // BRF:<number of branches found>
31    BranchesHit(u32),                 // BRH:<number of branches hit>
32    EndOfRecord                       // end_of_record
33}
34
35#[derive(Debug, PartialEq, Clone)]
36pub struct LineData {
37    pub line: u32,
38    pub count: u32,
39    pub checksum: Option<String> // MD5
40}
41
42#[derive(Debug, PartialEq, Clone)]
43pub struct FunctionName {
44    pub name: String,
45    pub line: u32
46}
47
48#[derive(Debug, PartialEq, Clone)]
49pub struct FunctionData {
50    pub name: String,
51    pub count: u32
52}
53
54#[derive(Debug, PartialEq, Clone)]
55pub struct BranchData {
56    pub line: u32,
57    pub block: u32,
58    pub branch: u32,
59    pub taken: u32
60}
61
62/// Parse the record from &str.
63///
64/// # Examples
65///
66/// ```
67/// use lcov_parser:: { LCOVRecord };
68///
69/// let actual = LCOVRecord::from("TN:product_test\n");
70/// let expected = LCOVRecord::TestName(Some("product_test".to_string()));
71///
72/// assert_eq!(actual, expected);
73/// ```
74impl<'a> From<&'a str> for LCOVRecord {
75    fn from(input: &'a str) -> Self {
76        parse_record(input).unwrap()
77    }
78}
79
80impl From<LineData> for LCOVRecord {
81    fn from(input: LineData) -> Self {
82        LCOVRecord::Data(input)
83    }
84}
85
86impl From<FunctionName> for LCOVRecord {
87    fn from(input: FunctionName) -> Self {
88        LCOVRecord::FunctionName(input)
89    }
90}
91
92impl From<FunctionData> for LCOVRecord {
93    fn from(input: FunctionData) -> Self {
94        LCOVRecord::FunctionData(input)
95    }
96}
97
98impl From<BranchData> for LCOVRecord {
99    fn from(input: BranchData) -> Self {
100        LCOVRecord::BranchData(input)
101    }
102}
103
104pub trait RecordWrite {
105    fn write_records<T: Write>(&self, output: &mut T) -> Result<()>;
106}