trident_config/
coverage.rs

1use serde::Deserialize;
2
3use crate::constants::DEFAULT_COVERAGE_FORMAT;
4use crate::constants::DEFAULT_COVERAGE_SERVER_PORT;
5use crate::constants::DEFAULT_LOOPCOUNT;
6
7#[derive(Debug, Deserialize, Clone)]
8pub struct Coverage {
9    pub enable: Option<bool>,
10    pub server_port: Option<u16>,
11    pub loopcount: Option<u64>,
12    pub format: Option<String>,
13    pub attach_extension: Option<bool>,
14}
15
16impl Default for Coverage {
17    fn default() -> Self {
18        Self {
19            enable: Some(false),
20            server_port: Some(DEFAULT_COVERAGE_SERVER_PORT),
21            loopcount: Some(DEFAULT_LOOPCOUNT),
22            format: Some(DEFAULT_COVERAGE_FORMAT.to_string()),
23            attach_extension: Some(false),
24        }
25    }
26}
27
28impl Coverage {
29    pub fn get_enable(&self) -> bool {
30        self.enable.unwrap_or(false)
31    }
32
33    pub fn get_server_port(&self) -> u16 {
34        self.server_port.unwrap_or(DEFAULT_COVERAGE_SERVER_PORT)
35    }
36
37    pub fn get_loopcount(&self) -> u64 {
38        self.loopcount.unwrap_or(DEFAULT_LOOPCOUNT)
39    }
40
41    pub fn get_format(&self) -> String {
42        self.format
43            .clone()
44            .unwrap_or_else(|| DEFAULT_COVERAGE_FORMAT.to_string())
45    }
46
47    pub fn get_attach_extension(&self) -> bool {
48        self.attach_extension.unwrap_or(false)
49    }
50
51    pub fn validate(&self) -> Result<(), String> {
52        if self.get_attach_extension() && !self.get_enable() {
53            return Err("Cannot attach extension without enabling coverage!".to_string());
54        }
55
56        if self.get_attach_extension() && self.get_format() != "json" {
57            return Err("Cannot attach extension with format other than json!".to_string());
58        }
59
60        Ok(())
61    }
62}