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
#[derive(Clone, Debug)]
pub(crate) struct BinRegistry {
    bins: std::collections::BTreeMap<String, crate::schema::Bin>,
    fallback: bool,
}

impl BinRegistry {
    pub(crate) fn new() -> Self {
        Self {
            bins: Default::default(),
            fallback: true,
        }
    }

    pub(crate) fn register_bin(&mut self, name: String, bin: crate::schema::Bin) {
        self.bins.insert(name, bin);
    }

    pub(crate) fn register_bins(
        &mut self,
        bins: impl Iterator<Item = (String, crate::schema::Bin)>,
    ) {
        self.bins.extend(bins);
    }

    pub(crate) fn resolve_bin(
        &self,
        bin: crate::schema::Bin,
    ) -> Result<crate::schema::Bin, crate::Error> {
        match bin {
            crate::schema::Bin::Path(path) => {
                let bin = crate::schema::Bin::Path(path);
                Ok(bin)
            }
            crate::schema::Bin::Name(name) => {
                let bin = self.resolve_name(&name);
                Ok(bin)
            }
            crate::schema::Bin::Ignore => Ok(crate::schema::Bin::Ignore),
            crate::schema::Bin::Error(err) => Err(err),
        }
    }

    pub(crate) fn resolve_name(&self, name: &str) -> crate::schema::Bin {
        if let Some(path) = self.bins.get(name) {
            return path.clone();
        }

        if self.fallback {
            let path = crate::cargo::cargo_bin(name);
            if path.exists() {
                return crate::schema::Bin::Path(path);
            }
        }

        crate::schema::Bin::Name(name.to_owned())
    }
}

impl Default for BinRegistry {
    fn default() -> Self {
        Self::new()
    }
}