Skip to main content

cargo/core/source/
mod.rs

1use std::collections::hash_map::HashMap;
2use std::fmt;
3
4use crate::core::{Dependency, Package, PackageId, Summary};
5use crate::util::CargoResult;
6
7mod source_id;
8
9pub use self::source_id::{GitReference, SourceId};
10
11/// A Source finds and downloads remote packages based on names and
12/// versions.
13pub trait Source {
14    /// Returns the `SourceId` corresponding to this source
15    fn source_id(&self) -> SourceId;
16
17    /// Returns the replaced `SourceId` corresponding to this source
18    fn replaced_source_id(&self) -> SourceId {
19        self.source_id()
20    }
21
22    /// Returns whether or not this source will return summaries with
23    /// checksums listed.
24    fn supports_checksums(&self) -> bool;
25
26    /// Returns whether or not this source will return summaries with
27    /// the `precise` field in the source id listed.
28    fn requires_precise(&self) -> bool;
29
30    /// Attempt to find the packages that match a dependency request.
31    fn query(&mut self, dep: &Dependency, f: &mut dyn FnMut(Summary)) -> CargoResult<()>;
32
33    /// Attempt to find the packages that are close to a dependency request.
34    /// Each source gets to define what `close` means for it.
35    /// path/git sources may return all dependencies that are at that uri.
36    /// where as an Index source may return dependencies that have the same canonicalization.
37    fn fuzzy_query(&mut self, dep: &Dependency, f: &mut dyn FnMut(Summary)) -> CargoResult<()>;
38
39    fn query_vec(&mut self, dep: &Dependency) -> CargoResult<Vec<Summary>> {
40        let mut ret = Vec::new();
41        self.query(dep, &mut |s| ret.push(s))?;
42        Ok(ret)
43    }
44
45    /// The update method performs any network operations required to
46    /// get the entire list of all names, versions and dependencies of
47    /// packages managed by the Source.
48    fn update(&mut self) -> CargoResult<()>;
49
50    /// The download method fetches the full package for each name and
51    /// version specified.
52    fn download(&mut self, package: PackageId) -> CargoResult<MaybePackage>;
53
54    fn finish_download(&mut self, package: PackageId, contents: Vec<u8>) -> CargoResult<Package>;
55
56    /// Generates a unique string which represents the fingerprint of the
57    /// current state of the source.
58    ///
59    /// This fingerprint is used to determine the "fresheness" of the source
60    /// later on. It must be guaranteed that the fingerprint of a source is
61    /// constant if and only if the output product will remain constant.
62    ///
63    /// The `pkg` argument is the package which this fingerprint should only be
64    /// interested in for when this source may contain multiple packages.
65    fn fingerprint(&self, pkg: &Package) -> CargoResult<String>;
66
67    /// If this source supports it, verifies the source of the package
68    /// specified.
69    ///
70    /// Note that the source may also have performed other checksum-based
71    /// verification during the `download` step, but this is intended to be run
72    /// just before a crate is compiled so it may perform more expensive checks
73    /// which may not be cacheable.
74    fn verify(&self, _pkg: PackageId) -> CargoResult<()> {
75        Ok(())
76    }
77
78    /// Describes this source in a human readable fashion, used for display in
79    /// resolver error messages currently.
80    fn describe(&self) -> String;
81
82    /// Returns whether a source is being replaced by another here
83    fn is_replaced(&self) -> bool {
84        false
85    }
86}
87
88pub enum MaybePackage {
89    Ready(Package),
90    Download { url: String, descriptor: String },
91}
92
93impl<'a, T: Source + ?Sized + 'a> Source for Box<T> {
94    /// Forwards to `Source::source_id`
95    fn source_id(&self) -> SourceId {
96        (**self).source_id()
97    }
98
99    /// Forwards to `Source::replaced_source_id`
100    fn replaced_source_id(&self) -> SourceId {
101        (**self).replaced_source_id()
102    }
103
104    /// Forwards to `Source::supports_checksums`
105    fn supports_checksums(&self) -> bool {
106        (**self).supports_checksums()
107    }
108
109    /// Forwards to `Source::requires_precise`
110    fn requires_precise(&self) -> bool {
111        (**self).requires_precise()
112    }
113
114    /// Forwards to `Source::query`
115    fn query(&mut self, dep: &Dependency, f: &mut dyn FnMut(Summary)) -> CargoResult<()> {
116        (**self).query(dep, f)
117    }
118
119    /// Forwards to `Source::query`
120    fn fuzzy_query(&mut self, dep: &Dependency, f: &mut dyn FnMut(Summary)) -> CargoResult<()> {
121        (**self).fuzzy_query(dep, f)
122    }
123
124    /// Forwards to `Source::update`
125    fn update(&mut self) -> CargoResult<()> {
126        (**self).update()
127    }
128
129    /// Forwards to `Source::download`
130    fn download(&mut self, id: PackageId) -> CargoResult<MaybePackage> {
131        (**self).download(id)
132    }
133
134    fn finish_download(&mut self, id: PackageId, data: Vec<u8>) -> CargoResult<Package> {
135        (**self).finish_download(id, data)
136    }
137
138    /// Forwards to `Source::fingerprint`
139    fn fingerprint(&self, pkg: &Package) -> CargoResult<String> {
140        (**self).fingerprint(pkg)
141    }
142
143    /// Forwards to `Source::verify`
144    fn verify(&self, pkg: PackageId) -> CargoResult<()> {
145        (**self).verify(pkg)
146    }
147
148    fn describe(&self) -> String {
149        (**self).describe()
150    }
151
152    fn is_replaced(&self) -> bool {
153        (**self).is_replaced()
154    }
155}
156
157impl<'a, T: Source + ?Sized + 'a> Source for &'a mut T {
158    fn source_id(&self) -> SourceId {
159        (**self).source_id()
160    }
161
162    fn replaced_source_id(&self) -> SourceId {
163        (**self).replaced_source_id()
164    }
165
166    fn supports_checksums(&self) -> bool {
167        (**self).supports_checksums()
168    }
169
170    fn requires_precise(&self) -> bool {
171        (**self).requires_precise()
172    }
173
174    fn query(&mut self, dep: &Dependency, f: &mut dyn FnMut(Summary)) -> CargoResult<()> {
175        (**self).query(dep, f)
176    }
177
178    fn fuzzy_query(&mut self, dep: &Dependency, f: &mut dyn FnMut(Summary)) -> CargoResult<()> {
179        (**self).fuzzy_query(dep, f)
180    }
181
182    fn update(&mut self) -> CargoResult<()> {
183        (**self).update()
184    }
185
186    fn download(&mut self, id: PackageId) -> CargoResult<MaybePackage> {
187        (**self).download(id)
188    }
189
190    fn finish_download(&mut self, id: PackageId, data: Vec<u8>) -> CargoResult<Package> {
191        (**self).finish_download(id, data)
192    }
193
194    fn fingerprint(&self, pkg: &Package) -> CargoResult<String> {
195        (**self).fingerprint(pkg)
196    }
197
198    fn verify(&self, pkg: PackageId) -> CargoResult<()> {
199        (**self).verify(pkg)
200    }
201
202    fn describe(&self) -> String {
203        (**self).describe()
204    }
205
206    fn is_replaced(&self) -> bool {
207        (**self).is_replaced()
208    }
209}
210
211/// A `HashMap` of `SourceId` -> `Box<Source>`
212#[derive(Default)]
213pub struct SourceMap<'src> {
214    map: HashMap<SourceId, Box<dyn Source + 'src>>,
215}
216
217// impl debug on source requires specialization, if even desirable at all
218impl<'src> fmt::Debug for SourceMap<'src> {
219    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
220        write!(f, "SourceMap ")?;
221        f.debug_set().entries(self.map.keys()).finish()
222    }
223}
224
225impl<'src> SourceMap<'src> {
226    /// Create an empty map
227    pub fn new() -> SourceMap<'src> {
228        SourceMap {
229            map: HashMap::new(),
230        }
231    }
232
233    /// Like `HashMap::contains_key`
234    pub fn contains(&self, id: SourceId) -> bool {
235        self.map.contains_key(&id)
236    }
237
238    /// Like `HashMap::get`
239    pub fn get(&self, id: SourceId) -> Option<&(dyn Source + 'src)> {
240        let source = self.map.get(&id);
241
242        source.map(|s| {
243            let s: &(dyn Source + 'src) = &**s;
244            s
245        })
246    }
247
248    /// Like `HashMap::get_mut`
249    pub fn get_mut(&mut self, id: SourceId) -> Option<&mut (dyn Source + 'src)> {
250        self.map.get_mut(&id).map(|s| {
251            let s: &mut (dyn Source + 'src) = &mut **s;
252            s
253        })
254    }
255
256    /// Like `HashMap::get`, but first calculates the `SourceId` from a
257    /// `PackageId`
258    pub fn get_by_package_id(&self, pkg_id: PackageId) -> Option<&(dyn Source + 'src)> {
259        self.get(pkg_id.source_id())
260    }
261
262    /// Like `HashMap::insert`, but derives the SourceId key from the Source
263    pub fn insert(&mut self, source: Box<dyn Source + 'src>) {
264        let id = source.source_id();
265        self.map.insert(id, source);
266    }
267
268    /// Like `HashMap::is_empty`
269    pub fn is_empty(&self) -> bool {
270        self.map.is_empty()
271    }
272
273    /// Like `HashMap::len`
274    pub fn len(&self) -> usize {
275        self.map.len()
276    }
277
278    /// Like `HashMap::values`
279    pub fn sources<'a>(&'a self) -> impl Iterator<Item = &'a Box<dyn Source + 'src>> {
280        self.map.values()
281    }
282
283    /// Like `HashMap::iter_mut`
284    pub fn sources_mut<'a>(
285        &'a mut self,
286    ) -> impl Iterator<Item = (&'a SourceId, &'a mut (dyn Source + 'src))> {
287        self.map.iter_mut().map(|(a, b)| (a, &mut **b))
288    }
289}