Skip to main content

uv_configuration/
sources.rs

1use uv_normalize::PackageName;
2
3#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
4#[serde(rename_all = "kebab-case", deny_unknown_fields)]
5pub enum NoSources {
6    /// Use `tool.uv.sources` when resolving dependencies.
7    #[default]
8    None,
9
10    /// Ignore `tool.uv.sources` when resolving dependencies for all packages.
11    All,
12
13    /// Ignore `tool.uv.sources` when resolving dependencies for specific packages.
14    Packages(Vec<PackageName>),
15}
16
17impl NoSources {
18    /// Determine the no sources strategy to use for the given arguments.
19    pub fn from_args(no_sources: Option<bool>, no_sources_package: Vec<PackageName>) -> Self {
20        match no_sources {
21            Some(true) => Self::All,
22            Some(false) => Self::None,
23            None => {
24                if no_sources_package.is_empty() {
25                    Self::None
26                } else {
27                    Self::Packages(no_sources_package)
28                }
29            }
30        }
31    }
32
33    /// Returns `true` if all sources should be ignored.
34    pub fn all(&self) -> bool {
35        matches!(self, Self::All)
36    }
37
38    /// Returns `true` if sources should be ignored for the given package.
39    pub fn for_package(&self, package_name: &PackageName) -> bool {
40        match self {
41            Self::None => false,
42            Self::All => true,
43            Self::Packages(packages) => packages.contains(package_name),
44        }
45    }
46
47    /// Combine a set of [`NoSources`] values.
48    #[must_use]
49    pub fn combine(self, other: Self) -> Self {
50        match (self, other) {
51            (Self::None, Self::None) => Self::None,
52            (Self::All, _) | (_, Self::All) => Self::All,
53            (Self::Packages(a), Self::None) => Self::Packages(a),
54            (Self::None, Self::Packages(b)) => Self::Packages(b),
55            (Self::Packages(mut a), Self::Packages(b)) => {
56                a.extend(b);
57                Self::Packages(a)
58            }
59        }
60    }
61
62    /// Extend a [`NoSources`] value with another.
63    pub fn extend(&mut self, other: Self) {
64        match (&mut *self, other) {
65            (Self::All, _) | (_, Self::All) => *self = Self::All,
66            (Self::None, Self::None) => {}
67            (Self::Packages(_), Self::None) => {}
68            (Self::None, Self::Packages(b)) => {
69                *self = Self::Packages(b);
70            }
71            (Self::Packages(a), Self::Packages(b)) => {
72                a.extend(b);
73            }
74        }
75    }
76}
77
78impl NoSources {
79    /// Returns `true` if all sources are allowed.
80    pub fn is_none(&self) -> bool {
81        matches!(self, Self::None)
82    }
83}