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
// Copyright (c) 2015-2016 lcov-parser developers
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

//! Module of LCOV Record.

use std::convert:: { From };
use std::option:: { Option };
use std::io:: { Result };
use std::io::prelude::*;
use parser:: { parse_record };

#[derive(Debug, PartialEq, Clone)]
pub enum LCOVRecord
{
    TestName(Option<String>),         // TN:<test name>
    SourceFile(String),               // SF:<absolute path to the source file>
    Data(LineData),                   // DA:<line number>,<execution count>[,<checksum>]
    FunctionName(FunctionName),       // FN:<line number of function start>,<function name> for each function
    FunctionData(FunctionData),       // FNDA:<execution count>,<function name>
    FunctionsFound(u32),              // FNF:<number of functions found>
    FunctionsHit(u32),                // FNH:<number of function hit>
    LinesHit(u32),                    // LH:<number of lines with an execution count> greater than 0
    LinesFound(u32),                  // LF:<number of instrumented lines>
    BranchData(BranchData),           // BRDA:<line number>,<block number>,<branch number>,<taken>
    BranchesFound(u32),               // BRF:<number of branches found>
    BranchesHit(u32),                 // BRH:<number of branches hit>
    EndOfRecord                       // end_of_record
}

#[derive(Debug, PartialEq, Clone)]
pub struct LineData {
    pub line: u32,
    pub count: u32,
    pub checksum: Option<String> // MD5
}

#[derive(Debug, PartialEq, Clone)]
pub struct FunctionName {
    pub name: String,
    pub line: u32
}

#[derive(Debug, PartialEq, Clone)]
pub struct FunctionData {
    pub name: String,
    pub count: u32
}

#[derive(Debug, PartialEq, Clone)]
pub struct BranchData {
    pub line: u32,
    pub block: u32,
    pub branch: u32,
    pub taken: u32
}

/// Parse the record from &str.
///
/// # Examples
///
/// ```
/// use lcov_parser:: { LCOVRecord };
///
/// let actual = LCOVRecord::from("TN:product_test\n");
/// let expected = LCOVRecord::TestName(Some("product_test".to_string()));
///
/// assert_eq!(actual, expected);
/// ```
impl<'a> From<&'a str> for LCOVRecord {
    fn from(input: &'a str) -> Self {
        parse_record(input).unwrap()
    }
}

impl From<LineData> for LCOVRecord {
    fn from(input: LineData) -> Self {
        LCOVRecord::Data(input)
    }
}

impl From<FunctionName> for LCOVRecord {
    fn from(input: FunctionName) -> Self {
        LCOVRecord::FunctionName(input)
    }
}

impl From<FunctionData> for LCOVRecord {
    fn from(input: FunctionData) -> Self {
        LCOVRecord::FunctionData(input)
    }
}

impl From<BranchData> for LCOVRecord {
    fn from(input: BranchData) -> Self {
        LCOVRecord::BranchData(input)
    }
}

pub trait RecordWrite {
    fn write_records<T: Write>(&self, output: &mut T) -> Result<()>;
}