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
//! A coverage information about a source file.
//!
//! Some coverage information is stored in a [`Sections`] as `BTreeMap` .
//!
//! [`Sections`]: ./type.Sections.html
use self::branch::Branches;
use self::function::Functions;
use self::line::Lines;
use super::{Merge, MergeError, ParseError, Parser, ReadError, Record};
use std::collections::BTreeMap;
use std::iter;
use std::path::PathBuf;

pub mod branch;
pub mod function;
pub mod line;

/// A map of coverage information about source files.
pub type Sections = BTreeMap<Key, Value>;

/// A key of a coverage information about a source file.
///
/// This struct is used as a key of [`Sections`].
///
/// [`Sections`]: ./type.Sections.html
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct Key {
    /// Name of the test.
    pub test_name: String,
    /// Path of the source file.
    pub source_file: PathBuf,
}

/// A value of a coverage information about a source file.
///
/// This struct is  used as a value of [`Sections`].
///
/// [`Sections`]: ./type.Sections.html
#[derive(Debug, Clone, Default, Eq, PartialEq)]
pub struct Value {
    /// Function coverage information in the section.
    pub functions: Functions,
    /// Branch coverage information in the section.
    pub branches: Branches,
    /// Line coverage information in the section.
    pub lines: Lines,
}

impl Value {
    /// Returns `true` if `self` has no coverage information.
    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() {
        // Sometimes, lcov emits TN: records multiple times, so skip the first TN: record.
        let mut test_name = None;
        while let Some(tn) = eat_if_matches!(parser, Record::TestName { name } => name) {
            test_name = Some(tn);
        }
        // Sometimes, lcov emit extra TN: records at the end of the tracefile.
        if parser.peek().map_err(ParseError::Read)?.is_none() {
            break;
        }

        let key = Key {
            test_name: test_name.unwrap_or_else(String::new),
            source_file: eat!(parser, Record::SourceFile { path } => path),
        };
        let value = Value {
            functions: function::parse(parser)?,
            branches: branch::parse(parser)?,
            lines: line::parse(parser)?,
        };
        // If the new section contains no data, ignore it.
        // LCOV merge (`lcov -c -a XXX`) behaves the same way.
        if !value.is_empty() {
            let _ = sections.insert(key, value);
        }
        eat!(parser, Record::EndOfRecord);
    }

    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)
}