@@ -4,6 +4,12 @@ use anyhow::anyhow;
44use spin_common:: ui:: quoted_path;
55
66pub ( crate ) struct ComponentToValidate < ' a > {
7+ id : & ' a str ,
8+ source_description : String ,
9+ wasm : Vec < u8 > ,
10+ }
11+
12+ struct ComponentSource < ' a > {
713 id : & ' a str ,
814 source : & ' a spin_manifest:: schema:: v2:: ComponentSource ,
915 dependencies : WrappedComponentDependencies ,
@@ -14,22 +20,30 @@ impl<'a> ComponentToValidate<'a> {
1420 self . id
1521 }
1622
17- pub fn source_description ( & self ) -> String {
18- match self . source {
19- spin_manifest:: schema:: v2:: ComponentSource :: Local ( path) => {
20- format ! ( "file {}" , quoted_path( path) )
21- }
22- spin_manifest:: schema:: v2:: ComponentSource :: Remote { url, .. } => format ! ( "URL {url}" ) ,
23- spin_manifest:: schema:: v2:: ComponentSource :: Registry { package, .. } => {
24- format ! ( "package {package}" )
25- }
26- }
23+ pub fn source_description ( & self ) -> & str {
24+ & self . source_description
25+ }
26+
27+ pub fn wasm_bytes ( & self ) -> & [ u8 ] {
28+ & self . wasm
2729 }
2830}
2931
30- pub fn component_source < ' a > (
32+ pub async fn load_and_resolve_all < ' a > (
33+ app : & ' a spin_manifest:: schema:: v2:: AppManifest ,
34+ triggers : & ' a [ spin_manifest:: schema:: v2:: Trigger ] ,
35+ resolution_context : & ' a ResolutionContext ,
36+ ) -> anyhow:: Result < Vec < ComponentToValidate < ' a > > > {
37+ let component_futures = triggers
38+ . iter ( )
39+ . map ( |t| load_and_resolve_one ( app, t, resolution_context) ) ;
40+ crate :: join_all_result ( component_futures) . await
41+ }
42+
43+ async fn load_and_resolve_one < ' a > (
3144 app : & ' a spin_manifest:: schema:: v2:: AppManifest ,
3245 trigger : & ' a spin_manifest:: schema:: v2:: Trigger ,
46+ resolution_context : & ' a ResolutionContext ,
3347) -> anyhow:: Result < ComponentToValidate < ' a > > {
3448 let component_spec = trigger
3549 . component
@@ -50,10 +64,21 @@ pub fn component_source<'a>(
5064 ( id, & component. source , & component. dependencies )
5165 }
5266 } ;
53- Ok ( ComponentToValidate {
67+
68+ let component = ComponentSource {
5469 id,
5570 source,
5671 dependencies : WrappedComponentDependencies :: new ( dependencies) ,
72+ } ;
73+
74+ let loader = ComponentSourceLoader :: new ( resolution_context. wasm_loader ( ) ) ;
75+
76+ let wasm = spin_compose:: compose ( & loader, & component) . await ?;
77+
78+ Ok ( ComponentToValidate {
79+ id,
80+ source_description : source_description ( component. source ) ,
81+ wasm,
5782 } )
5883}
5984
@@ -68,33 +93,29 @@ impl ResolutionContext {
6893 Ok ( Self { wasm_loader } )
6994 }
7095
71- pub ( crate ) fn wasm_loader ( & self ) -> & spin_loader:: WasmLoader {
96+ fn wasm_loader ( & self ) -> & spin_loader:: WasmLoader {
7297 & self . wasm_loader
7398 }
7499}
75100
76- pub ( crate ) struct ComponentSourceLoader < ' a > {
101+ struct ComponentSourceLoader < ' a > {
77102 wasm_loader : & ' a spin_loader:: WasmLoader ,
78- _phantom : std:: marker:: PhantomData < & ' a usize > ,
79103}
80104
81105impl < ' a > ComponentSourceLoader < ' a > {
82106 pub fn new ( wasm_loader : & ' a spin_loader:: WasmLoader ) -> Self {
83- Self {
84- wasm_loader,
85- _phantom : std:: marker:: PhantomData ,
86- }
107+ Self { wasm_loader }
87108 }
88109}
89110
90111#[ async_trait:: async_trait]
91112impl < ' a > spin_compose:: ComponentSourceLoader for ComponentSourceLoader < ' a > {
92- type Component = ComponentToValidate < ' a > ;
113+ type Component = ComponentSource < ' a > ;
93114 type Dependency = WrappedComponentDependency ;
94115 async fn load_component_source ( & self , source : & Self :: Component ) -> anyhow:: Result < Vec < u8 > > {
95116 let path = self
96117 . wasm_loader
97- . load_component_source ( source. id ( ) , source. source )
118+ . load_component_source ( source. id , source. source )
98119 . await ?;
99120 let bytes = tokio:: fs:: read ( & path) . await ?;
100121 let component = spin_componentize:: componentize_if_necessary ( & bytes) ?;
@@ -144,7 +165,7 @@ impl WrappedComponentDependencies {
144165}
145166
146167#[ async_trait:: async_trait]
147- impl < ' a > spin_compose:: ComponentLike for ComponentToValidate < ' a > {
168+ impl < ' a > spin_compose:: ComponentLike for ComponentSource < ' a > {
148169 type Dependency = WrappedComponentDependency ;
149170
150171 fn dependencies (
@@ -176,3 +197,15 @@ impl spin_compose::DependencyLike for WrappedComponentDependency {
176197 }
177198 }
178199}
200+
201+ fn source_description ( source : & spin_manifest:: schema:: v2:: ComponentSource ) -> String {
202+ match source {
203+ spin_manifest:: schema:: v2:: ComponentSource :: Local ( path) => {
204+ format ! ( "file {}" , quoted_path( path) )
205+ }
206+ spin_manifest:: schema:: v2:: ComponentSource :: Remote { url, .. } => format ! ( "URL {url}" ) ,
207+ spin_manifest:: schema:: v2:: ComponentSource :: Registry { package, .. } => {
208+ format ! ( "package {package}" )
209+ }
210+ }
211+ }
0 commit comments