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

use anyhow::{Context, Result};
use cargo::{
    core::{Dependency, GitReference, SourceId},
    util::IntoUrl,
};

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

#[derive(Debug, Clone)]
pub struct DetectorConfiguration {
    pub dependency: Dependency,
    pub path: Option<String>,
}

pub type DetectorsConfigurationList = Vec<DetectorConfiguration>;

/// Returns list of detectors.
pub fn get_remote_detectors_configuration(
    blockchain: BlockChain,
) -> Result<DetectorsConfigurationList> {
    let dependency = Dependency::parse(
        "library",
        None,
        SourceId::for_git(
            &blockchain
                .get_detectors_url()
                .into_url()
                .with_context(|| format!("Failed to get URL for {} blockchain", blockchain))?,
            GitReference::DefaultBranch,
        )?,
    )?;

    let detectors = vec![DetectorConfiguration {
        dependency,
        path: Some("detectors".to_string()),
    }];

    Ok(detectors)
}

/// Returns local detectors configuration from custom path.
pub fn get_local_detectors_configuration(path: &Path) -> Result<DetectorsConfigurationList> {
    let detectors = vec![DetectorConfiguration {
        dependency: Dependency::parse(
            "library",
            None,
            SourceId::for_path(path)
                .with_context(|| format!("Failed to create SourceId for path: {:?}", path))?,
        )
        .with_context(|| "Failed to parse local detector dependency")?,
        path: None,
    }];
    Ok(detectors)
}