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 std::path::PathBuf;

use anyhow::Result;
use cargo::Config;
use itertools::Itertools;

use crate::scout::blockchain::BlockChain;

use self::{
    builder::DetectorBuilder,
    configuration::{DetectorConfiguration, DetectorsConfigurationList},
};
mod builder;
mod configuration;
mod library;
mod source;

use cargo_metadata::Metadata;
pub use configuration::{get_local_detectors_configuration, get_remote_detectors_configuration};

#[derive(Debug)]
pub struct Detectors<'a> {
    cargo_config: Config,
    detectors_configs: DetectorsConfigurationList,
    metadata: &'a Metadata,
    verbose: bool,
}

impl<'a> Detectors<'a> {
    /// Creates a new instance of `Detectors`
    pub fn new(
        cargo_config: Config,
        detectors_configs: DetectorsConfigurationList,
        metadata: &'a Metadata,
        verbose: bool,
    ) -> Self {
        Self {
            cargo_config,
            detectors_configs,
            metadata,
            verbose,
        }
    }

    /// Builds detectors and returns the paths to the built libraries
    pub fn build(self, bc: BlockChain, used_detectors: &[String]) -> Result<Vec<PathBuf>> {
        self.detectors_configs
            .iter()
            .map(|detectors_config| self.build_detectors(detectors_config, bc, used_detectors))
            .flatten_ok()
            .collect::<Result<Vec<_>>>()
    }

    pub fn get_detector_names(&self) -> Result<Vec<String>> {
        self.detectors_configs
            .iter()
            .map(|detectors_config| {
                let builder = DetectorBuilder::new(
                    &self.cargo_config,
                    detectors_config,
                    self.metadata,
                    self.verbose,
                );
                builder.get_detector_names()
            })
            .flatten_ok()
            .collect::<Result<Vec<_>>>()
    }

    fn build_detectors(
        &self,
        detectors_config: &DetectorConfiguration,
        bc: BlockChain,
        used_detectors: &[String],
    ) -> Result<Vec<PathBuf>> {
        let builder = DetectorBuilder::new(
            &self.cargo_config,
            detectors_config,
            self.metadata,
            self.verbose,
        );
        builder.build(bc, used_detectors.to_vec())
    }
}