]>
Commit | Line | Data |
---|---|---|
04ddd4d0 | 1 | use crate::core::source::MaybePackage; |
a645c4fe | 2 | use crate::core::{Dependency, Package, PackageId, QueryKind, Source, SourceId, Summary}; |
ebca5190 | 3 | use crate::util::errors::CargoResult; |
82093ad9 | 4 | use std::task::Poll; |
ebca5190 WL |
5 | |
6 | use anyhow::Context as _; | |
8214bb95 AC |
7 | |
8 | pub struct ReplacedSource<'cfg> { | |
9 | to_replace: SourceId, | |
10 | replace_with: SourceId, | |
b8b7faee | 11 | inner: Box<dyn Source + 'cfg>, |
8214bb95 AC |
12 | } |
13 | ||
14 | impl<'cfg> ReplacedSource<'cfg> { | |
1e682848 | 15 | pub fn new( |
e5a11190 E |
16 | to_replace: SourceId, |
17 | replace_with: SourceId, | |
b8b7faee | 18 | src: Box<dyn Source + 'cfg>, |
1e682848 | 19 | ) -> ReplacedSource<'cfg> { |
8214bb95 | 20 | ReplacedSource { |
e5a11190 E |
21 | to_replace, |
22 | replace_with, | |
8214bb95 AC |
23 | inner: src, |
24 | } | |
25 | } | |
f947326a DW |
26 | } |
27 | ||
28 | impl<'cfg> Source for ReplacedSource<'cfg> { | |
e5a11190 E |
29 | fn source_id(&self) -> SourceId { |
30 | self.to_replace | |
53bd095f DW |
31 | } |
32 | ||
e5a11190 E |
33 | fn replaced_source_id(&self) -> SourceId { |
34 | self.replace_with | |
84cc3d8b E |
35 | } |
36 | ||
6acedd8a DW |
37 | fn supports_checksums(&self) -> bool { |
38 | self.inner.supports_checksums() | |
39 | } | |
40 | ||
41 | fn requires_precise(&self) -> bool { | |
42 | self.inner.requires_precise() | |
43 | } | |
44 | ||
233ecba2 | 45 | fn query( |
82093ad9 AS |
46 | &mut self, |
47 | dep: &Dependency, | |
a645c4fe | 48 | kind: QueryKind, |
82093ad9 AS |
49 | f: &mut dyn FnMut(Summary), |
50 | ) -> Poll<CargoResult<()>> { | |
e5a11190 E |
51 | let (replace_with, to_replace) = (self.replace_with, self.to_replace); |
52 | let dep = dep.clone().map_source(to_replace, replace_with); | |
53 | ||
54 | self.inner | |
a645c4fe | 55 | .query(&dep, kind, &mut |summary| { |
e5a11190 E |
56 | f(summary.map_source(replace_with, to_replace)) |
57 | }) | |
82093ad9 AS |
58 | .map_err(|e| { |
59 | e.context(format!( | |
60 | "failed to query replaced source {}", | |
61 | self.to_replace | |
62 | )) | |
63 | }) | |
5e680f28 | 64 | } |
65 | ||
f12f0256 AS |
66 | fn invalidate_cache(&mut self) { |
67 | self.inner.invalidate_cache() | |
8214bb95 AC |
68 | } |
69 | ||
dae87a26 | 70 | fn download(&mut self, id: PackageId) -> CargoResult<MaybePackage> { |
e5a11190 E |
71 | let id = id.with_source_id(self.replace_with); |
72 | let pkg = self | |
73 | .inner | |
dae87a26 | 74 | .download(id) |
ebca5190 | 75 | .with_context(|| format!("failed to download replaced source {}", self.to_replace))?; |
c94804bd AC |
76 | Ok(match pkg { |
77 | MaybePackage::Ready(pkg) => { | |
e5a11190 | 78 | MaybePackage::Ready(pkg.map_source(self.replace_with, self.to_replace)) |
c94804bd AC |
79 | } |
80 | other @ MaybePackage::Download { .. } => other, | |
81 | }) | |
82 | } | |
83 | ||
dae87a26 | 84 | fn finish_download(&mut self, id: PackageId, data: Vec<u8>) -> CargoResult<Package> { |
e5a11190 E |
85 | let id = id.with_source_id(self.replace_with); |
86 | let pkg = self | |
87 | .inner | |
dae87a26 | 88 | .finish_download(id, data) |
ebca5190 | 89 | .with_context(|| format!("failed to download replaced source {}", self.to_replace))?; |
e5a11190 | 90 | Ok(pkg.map_source(self.replace_with, self.to_replace)) |
8214bb95 AC |
91 | } |
92 | ||
93 | fn fingerprint(&self, id: &Package) -> CargoResult<String> { | |
c5611a32 | 94 | self.inner.fingerprint(id) |
7fd5243c AC |
95 | } |
96 | ||
dae87a26 | 97 | fn verify(&self, id: PackageId) -> CargoResult<()> { |
e5a11190 | 98 | let id = id.with_source_id(self.replace_with); |
dae87a26 | 99 | self.inner.verify(id) |
8214bb95 | 100 | } |
20cfb41e AC |
101 | |
102 | fn describe(&self) -> String { | |
e5a11190 E |
103 | format!( |
104 | "{} (which is replacing {})", | |
105 | self.inner.describe(), | |
106 | self.to_replace | |
107 | ) | |
20cfb41e AC |
108 | } |
109 | ||
110 | fn is_replaced(&self) -> bool { | |
111 | true | |
112 | } | |
4a2f810d AC |
113 | |
114 | fn add_to_yanked_whitelist(&mut self, pkgs: &[PackageId]) { | |
f16efff1 AC |
115 | let pkgs = pkgs |
116 | .iter() | |
117 | .map(|id| id.with_source_id(self.replace_with)) | |
4a2f810d AC |
118 | .collect::<Vec<_>>(); |
119 | self.inner.add_to_yanked_whitelist(&pkgs); | |
120 | } | |
5f616eb1 | 121 | |
72ed97bf | 122 | fn is_yanked(&mut self, pkg: PackageId) -> Poll<CargoResult<bool>> { |
5f616eb1 EH |
123 | self.inner.is_yanked(pkg) |
124 | } | |
82093ad9 AS |
125 | |
126 | fn block_until_ready(&mut self) -> CargoResult<()> { | |
f12f0256 AS |
127 | self.inner |
128 | .block_until_ready() | |
129 | .with_context(|| format!("failed to update replaced source {}", self.to_replace)) | |
82093ad9 | 130 | } |
8214bb95 | 131 | } |