trident_config/
lib.rs

1pub mod constants;
2pub mod coverage;
3pub mod fuzz;
4mod metrics;
5use constants::*;
6use coverage::*;
7use fuzz::*;
8pub mod utils;
9
10use serde::Deserialize;
11use std::fs;
12use std::io;
13use thiserror::Error;
14use utils::discover_root;
15mod regression;
16
17#[derive(Error, Debug)]
18pub enum Error {
19    #[error("invalid workspace")]
20    BadWorkspace,
21    #[error("{0:?}")]
22    Anyhow(#[from] anyhow::Error),
23    #[error("{0:?}")]
24    Io(#[from] io::Error),
25    #[error("{0:?}")]
26    Toml(#[from] toml::de::Error),
27}
28
29#[derive(Debug, Deserialize, Clone)]
30pub struct TridentConfig {
31    pub fuzz: Option<Fuzz>,
32}
33
34impl Default for TridentConfig {
35    fn default() -> Self {
36        Self::new()
37    }
38}
39
40impl TridentConfig {
41    pub fn new() -> Self {
42        let root = discover_root().expect("failed to find the root folder");
43        let s = fs::read_to_string(root.join(TRIDENT_TOML).as_path())
44            .expect("failed to read the Trident config file");
45        let _config: TridentConfig =
46            toml::from_str(&s).expect("failed to parse the Trident config file");
47        _config
48    }
49
50    // -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
51    // fuzz
52    pub fn get_metrics(&self) -> bool {
53        self.fuzz
54            .as_ref()
55            .map(|fuzz| fuzz.get_metrics())
56            .unwrap_or_default()
57    }
58
59    pub fn get_metrics_json(&self) -> bool {
60        self.fuzz
61            .as_ref()
62            .map(|fuzz| fuzz.get_metrics_json())
63            .unwrap_or_default()
64    }
65
66    pub fn get_metrics_dashboard(&self) -> bool {
67        self.fuzz
68            .as_ref()
69            .map(|fuzz| fuzz.get_metrics_dashboard())
70            .unwrap_or_default()
71    }
72
73    pub fn get_regression(&self) -> bool {
74        self.fuzz
75            .as_ref()
76            .map(|fuzz| fuzz.get_regression())
77            .unwrap_or_default()
78    }
79
80    pub fn get_coverage(&self) -> Coverage {
81        self.fuzz
82            .as_ref()
83            .map(|fuzz| fuzz.get_coverage())
84            .unwrap_or_default()
85    }
86
87    pub fn loopcount(&self) -> u64 {
88        self.get_coverage().get_loopcount()
89    }
90
91    pub fn coverage_server_port(&self) -> u16 {
92        self.get_coverage().get_server_port()
93    }
94
95    pub fn programs(&self) -> Vec<FuzzProgram> {
96        self.fuzz
97            .as_ref()
98            .map(|fuzz| {
99                if let Some(programs) = &fuzz.programs {
100                    programs.iter().map(FuzzProgram::from).collect()
101                } else {
102                    Vec::default()
103                }
104            })
105            .unwrap_or_default()
106    }
107
108    pub fn accounts(&self) -> Vec<FuzzAccount> {
109        self.fuzz
110            .as_ref()
111            .map(|fuzz| {
112                if let Some(accounts) = &fuzz.accounts {
113                    accounts.iter().map(FuzzAccount::from).collect()
114                } else {
115                    Vec::default()
116                }
117            })
118            .unwrap_or_default()
119    }
120}