@@ -4,6 +4,12 @@ use anyhow::anyhow;
4
4
use spin_common:: ui:: quoted_path;
5
5
6
6
pub ( crate ) struct ComponentToValidate < ' a > {
7
+ id : & ' a str ,
8
+ source_description : String ,
9
+ wasm : Vec < u8 > ,
10
+ }
11
+
12
+ struct ComponentSource < ' a > {
7
13
id : & ' a str ,
8
14
source : & ' a spin_manifest:: schema:: v2:: ComponentSource ,
9
15
dependencies : WrappedComponentDependencies ,
@@ -14,22 +20,30 @@ impl<'a> ComponentToValidate<'a> {
14
20
self . id
15
21
}
16
22
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
27
29
}
28
30
}
29
31
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 > (
31
44
app : & ' a spin_manifest:: schema:: v2:: AppManifest ,
32
45
trigger : & ' a spin_manifest:: schema:: v2:: Trigger ,
46
+ resolution_context : & ' a ResolutionContext ,
33
47
) -> anyhow:: Result < ComponentToValidate < ' a > > {
34
48
let component_spec = trigger
35
49
. component
@@ -50,10 +64,21 @@ pub fn component_source<'a>(
50
64
( id, & component. source , & component. dependencies )
51
65
}
52
66
} ;
53
- Ok ( ComponentToValidate {
67
+
68
+ let component = ComponentSource {
54
69
id,
55
70
source,
56
71
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,
57
82
} )
58
83
}
59
84
@@ -68,33 +93,29 @@ impl ResolutionContext {
68
93
Ok ( Self { wasm_loader } )
69
94
}
70
95
71
- pub ( crate ) fn wasm_loader ( & self ) -> & spin_loader:: WasmLoader {
96
+ fn wasm_loader ( & self ) -> & spin_loader:: WasmLoader {
72
97
& self . wasm_loader
73
98
}
74
99
}
75
100
76
- pub ( crate ) struct ComponentSourceLoader < ' a > {
101
+ struct ComponentSourceLoader < ' a > {
77
102
wasm_loader : & ' a spin_loader:: WasmLoader ,
78
- _phantom : std:: marker:: PhantomData < & ' a usize > ,
79
103
}
80
104
81
105
impl < ' a > ComponentSourceLoader < ' a > {
82
106
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 }
87
108
}
88
109
}
89
110
90
111
#[ async_trait:: async_trait]
91
112
impl < ' a > spin_compose:: ComponentSourceLoader for ComponentSourceLoader < ' a > {
92
- type Component = ComponentToValidate < ' a > ;
113
+ type Component = ComponentSource < ' a > ;
93
114
type Dependency = WrappedComponentDependency ;
94
115
async fn load_component_source ( & self , source : & Self :: Component ) -> anyhow:: Result < Vec < u8 > > {
95
116
let path = self
96
117
. wasm_loader
97
- . load_component_source ( source. id ( ) , source. source )
118
+ . load_component_source ( source. id , source. source )
98
119
. await ?;
99
120
let bytes = tokio:: fs:: read ( & path) . await ?;
100
121
let component = spin_componentize:: componentize_if_necessary ( & bytes) ?;
@@ -144,7 +165,7 @@ impl WrappedComponentDependencies {
144
165
}
145
166
146
167
#[ async_trait:: async_trait]
147
- impl < ' a > spin_compose:: ComponentLike for ComponentToValidate < ' a > {
168
+ impl < ' a > spin_compose:: ComponentLike for ComponentSource < ' a > {
148
169
type Dependency = WrappedComponentDependency ;
149
170
150
171
fn dependencies (
@@ -176,3 +197,15 @@ impl spin_compose::DependencyLike for WrappedComponentDependency {
176
197
}
177
198
}
178
199
}
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