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
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};

use crate::{coverage::Coverage, Range};

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Function {
    pub name: String,
    pub decl: Range,
    pub loc: Range,
    pub line: u32,
}

#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum BranchType {
    BinaryExpr,
    DefaultArg,
    If,
    Switch,
    CondExpr,
}

impl ToString for BranchType {
    fn to_string(&self) -> String {
        match self {
            BranchType::BinaryExpr => "binary-expr".to_string(),
            BranchType::DefaultArg => "default-arg".to_string(),
            BranchType::If => "if".to_string(),
            BranchType::Switch => "switch".to_string(),
            BranchType::CondExpr => "cond-expr".to_string(),
        }
    }
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Branch {
    pub loc: Option<Range>,
    #[serde(rename = "type")]
    pub branch_type: BranchType,
    pub locations: Vec<Range>,
    pub line: Option<u32>,
}

impl Branch {
    pub fn from_line(branch_type: BranchType, line: u32, locations: Vec<Range>) -> Branch {
        Branch {
            loc: None,
            branch_type,
            locations,
            line: Some(line),
        }
    }
    pub fn from_loc(branch_type: BranchType, loc: Range, locations: Vec<Range>) -> Branch {
        Branch {
            loc: Some(loc),
            branch_type,
            locations,
            line: None,
        }
    }
}

/// Map to line number to hit count.
pub type LineHitMap = IndexMap<u32, u32>;
pub type StatementMap = IndexMap<u32, Range>;
pub type FunctionMap = IndexMap<u32, Function>;
pub type BranchMap = IndexMap<u32, Branch>;
pub type BranchHitMap = IndexMap<u32, Vec<u32>>;
pub type BranchCoverageMap = IndexMap<u32, Coverage>;

#[cfg(test)]
mod tests {
    use crate::BranchType;

    #[test]
    fn branch_type_should_return_kebab_string() {
        assert_eq!(&BranchType::BinaryExpr.to_string(), "binary-expr");
        assert_eq!(&BranchType::DefaultArg.to_string(), "default-arg");
        assert_eq!(&BranchType::If.to_string(), "if");
        assert_eq!(&BranchType::Switch.to_string(), "switch");
        assert_eq!(&BranchType::CondExpr.to_string(), "cond-expr");
    }
}