istanbul_oxide/
types.rs

1use indexmap::IndexMap;
2use serde::{Deserialize, Serialize};
3
4use crate::{coverage::Coverage, Range};
5
6#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
7pub struct Function {
8    pub name: String,
9    pub decl: Range,
10    pub loc: Range,
11    pub line: u32,
12}
13
14#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
15#[serde(rename_all = "kebab-case")]
16pub enum BranchType {
17    BinaryExpr,
18    DefaultArg,
19    If,
20    Switch,
21    CondExpr,
22}
23
24impl ToString for BranchType {
25    fn to_string(&self) -> String {
26        match self {
27            BranchType::BinaryExpr => "binary-expr".to_string(),
28            BranchType::DefaultArg => "default-arg".to_string(),
29            BranchType::If => "if".to_string(),
30            BranchType::Switch => "switch".to_string(),
31            BranchType::CondExpr => "cond-expr".to_string(),
32        }
33    }
34}
35
36#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
37pub struct Branch {
38    pub loc: Option<Range>,
39    #[serde(rename = "type")]
40    pub branch_type: BranchType,
41    pub locations: Vec<Range>,
42    pub line: Option<u32>,
43}
44
45impl Branch {
46    pub fn from_line(branch_type: BranchType, line: u32, locations: Vec<Range>) -> Branch {
47        Branch {
48            loc: None,
49            branch_type,
50            locations,
51            line: Some(line),
52        }
53    }
54    pub fn from_loc(branch_type: BranchType, loc: Range, locations: Vec<Range>) -> Branch {
55        Branch {
56            loc: Some(loc),
57            branch_type,
58            locations,
59            line: None,
60        }
61    }
62}
63
64/// Map to line number to hit count.
65pub type LineHitMap = IndexMap<u32, u32>;
66pub type StatementMap = IndexMap<u32, Range>;
67pub type FunctionMap = IndexMap<u32, Function>;
68pub type BranchMap = IndexMap<u32, Branch>;
69pub type BranchHitMap = IndexMap<u32, Vec<u32>>;
70pub type BranchCoverageMap = IndexMap<u32, Coverage>;
71
72#[cfg(test)]
73mod tests {
74    use crate::BranchType;
75
76    #[test]
77    fn branch_type_should_return_kebab_string() {
78        assert_eq!(&BranchType::BinaryExpr.to_string(), "binary-expr");
79        assert_eq!(&BranchType::DefaultArg.to_string(), "default-arg");
80        assert_eq!(&BranchType::If.to_string(), "if");
81        assert_eq!(&BranchType::Switch.to_string(), "switch");
82        assert_eq!(&BranchType::CondExpr.to_string(), "cond-expr");
83    }
84}