wasmer_wasix/runtime/resolver/
multi_source.rs1use std::sync::Arc;
2
3use wasmer_config::package::PackageSource;
4
5use crate::runtime::resolver::{PackageSummary, QueryError, Source};
6
7#[derive(Debug, Clone)]
22pub struct MultiSource {
23 sources: Vec<Arc<dyn Source + Send + Sync>>,
24 strategy: MultiSourceStrategy,
25}
26
27impl Default for MultiSource {
28 fn default() -> Self {
29 Self::new()
30 }
31}
32
33impl MultiSource {
34 pub fn new() -> Self {
35 MultiSource {
36 sources: Vec::new(),
37 strategy: MultiSourceStrategy::default(),
38 }
39 }
40
41 pub fn add_source(&mut self, source: impl Source + Send + 'static) -> &mut Self {
42 self.add_shared_source(Arc::new(source))
43 }
44
45 pub fn add_shared_source(&mut self, source: Arc<dyn Source + Send + Sync>) -> &mut Self {
46 self.sources.push(source);
47 self
48 }
49
50 pub fn with_strategy(self, strategy: MultiSourceStrategy) -> Self {
52 MultiSource { strategy, ..self }
53 }
54}
55
56#[async_trait::async_trait]
57impl Source for MultiSource {
58 #[tracing::instrument(level = "debug", skip_all, fields(%package))]
59 async fn query(&self, package: &PackageSource) -> Result<Vec<PackageSummary>, QueryError> {
60 let mut output = Vec::<PackageSummary>::new();
61
62 for source in &self.sources {
63 match source.query(package).await {
64 Ok(mut summaries) => {
65 if self.strategy.merge_results {
66 summaries.retain(|new| {
68 !output.iter().any(|existing| new.pkg.id == existing.pkg.id)
69 });
70 output.extend(summaries);
71 } else {
72 return Ok(summaries);
73 }
74 }
75 Err(QueryError::Unsupported { .. })
76 if self.strategy.continue_if_unsupported || self.strategy.merge_results =>
77 {
78 continue;
79 }
80 Err(QueryError::NotFound { .. })
81 if self.strategy.continue_if_not_found || self.strategy.merge_results =>
82 {
83 continue;
84 }
85 Err(QueryError::NoMatches { .. })
86 if self.strategy.continue_if_no_matches || self.strategy.merge_results =>
87 {
88 continue;
89 }
90 Err(e) => return Err(e),
94 }
95 }
96
97 if !output.is_empty() {
98 output.sort_by(|a, b| a.pkg.id.cmp(&b.pkg.id));
99
100 Ok(output)
101 } else {
102 Err(QueryError::NotFound {
103 query: package.clone(),
104 })
105 }
106 }
107}
108
109#[derive(Debug, Clone, PartialEq, Eq)]
110#[non_exhaustive]
111pub struct MultiSourceStrategy {
112 pub continue_if_unsupported: bool,
117 pub continue_if_not_found: bool,
123 pub continue_if_no_matches: bool,
128
129 pub merge_results: bool,
133}
134
135impl Default for MultiSourceStrategy {
136 fn default() -> Self {
137 MultiSourceStrategy {
138 continue_if_unsupported: true,
139 continue_if_not_found: true,
140 continue_if_no_matches: true,
141 merge_results: true,
142 }
143 }
144}
145
146#[cfg(test)]
147mod tests {
148 use wasmer_config::package::PackageId;
149
150 use super::super::{DistributionInfo, InMemorySource, PackageInfo, WebcHash};
151 use super::*;
152
153 #[tokio::test]
155 async fn test_multi_source_merge() {
156 let id1 = PackageId::new_named("ns/pkg", "0.0.1".parse().unwrap());
157 let pkg1 = PackageSummary {
158 pkg: PackageInfo {
159 id: id1.clone(),
160 commands: Vec::new(),
161 entrypoint: None,
162 dependencies: Vec::new(),
163 filesystem: Vec::new(),
164 },
165 dist: DistributionInfo {
166 webc: "https://example.com/ns/pkg/0.0.1".parse().unwrap(),
167 webc_sha256: WebcHash([0u8; 32]),
168 },
169 };
170
171 let id2 = PackageId::new_named("ns/pkg", "0.0.2".parse().unwrap());
172 let mut pkg2 = pkg1.clone();
173 pkg2.pkg.id = id2.clone();
174
175 let mut mem1 = InMemorySource::new();
176 mem1.add(pkg1);
177
178 let mut mem2 = InMemorySource::new();
179 mem2.add(pkg2);
180
181 let mut multi = MultiSource::new();
182 multi.add_source(mem1);
183 multi.add_source(mem2);
184
185 let summaries = multi.query(&"ns/pkg".parse().unwrap()).await.unwrap();
186 assert_eq!(summaries.len(), 2);
187 assert_eq!(summaries[0].pkg.id, id1);
188 assert_eq!(summaries[1].pkg.id, id2);
189 }
190}