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
//! Database scopes

use serde::{Deserialize, Serialize};

/// Registries where packages are located
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum Registry {
    /// Public package published to <https://crates.io>
    #[serde(rename = "public")]
    Public,

    /// Package is local
    #[serde(rename = "local")]
    Local,

    /// Package is located in a private registry
    #[serde(rename = "private")]
    Private {
        /// URI of the private registry
        uri: String,
    },

    /// All sources should be considered
    #[serde(rename = "all")]
    All,
}

impl Default for Registry {
    fn default() -> Self {
        Registry::Public
    }
}

/// Scopes for packages to be queried (i.e. their sources)
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Package {
    /// Source of a package
    pub source: Vec<Registry>,
}

impl Package {
    /// Is the scope only for remote crates?
    pub fn is_remote(&self) -> bool {
        self.source.iter().any(|source| match source {
            Registry::Public | Registry::Private { .. } => true,
            _ => false,
        })
    }
}

impl Default for Package {
    fn default() -> Self {
        Registry::default().into()
    }
}

impl Package {
    /// Creates a new [[`Package`]] scope from a specific registry URI
    pub fn from_registry(source: &str) -> Self {
        Registry::Private {
            uri: source.to_string(),
        }
        .into()
    }
}

impl From<Registry> for Package {
    fn from(registry: Registry) -> Self {
        Self {
            source: vec![registry],
        }
    }
}