@@ -5,15 +5,14 @@ mod loader;
55
66use environment_definition:: { TargetEnvironment , TargetWorld , TriggerType } ;
77pub use loader:: ResolutionContext ;
8- use loader:: { component_source , ComponentSourceLoader , ComponentToValidate } ;
8+ use loader:: { load_and_resolve_all , ComponentToValidate } ;
99
1010pub async fn validate_application_against_environment_ids (
1111 env_ids : impl Iterator < Item = & str > ,
1212 app : & spin_manifest:: schema:: v2:: AppManifest ,
1313 resolution_context : & ResolutionContext ,
1414) -> anyhow:: Result < ( ) > {
15- let envs = futures:: future:: join_all ( env_ids. map ( resolve_environment_id) ) . await ;
16- let envs: Vec < _ > = envs. into_iter ( ) . collect :: < Result < _ , _ > > ( ) ?;
15+ let envs = join_all_result ( env_ids. map ( resolve_environment_id) ) . await ?;
1716 validate_application_against_environments ( & envs, app, resolution_context) . await
1817}
1918
@@ -54,6 +53,8 @@ pub async fn validate_application_against_environments(
5453 app : & spin_manifest:: schema:: v2:: AppManifest ,
5554 resolution_context : & ResolutionContext ,
5655) -> anyhow:: Result < ( ) > {
56+ use futures:: FutureExt ;
57+
5758 for trigger_type in app. triggers . keys ( ) {
5859 if let Some ( env) = envs
5960 . iter ( )
@@ -66,26 +67,15 @@ pub async fn validate_application_against_environments(
6667 }
6768 }
6869
69- let components_by_trigger_type = app
70- . triggers
71- . iter ( )
72- . map ( |( ty, ts) | {
73- ts. iter ( )
74- . map ( |t| component_source ( app, t) )
75- . collect :: < Result < Vec < _ > , _ > > ( )
76- . map ( |css| ( ty, css) )
77- } )
78- . collect :: < Result < Vec < _ > , _ > > ( ) ?;
70+ let components_by_trigger_type_futs = app. triggers . iter ( ) . map ( |( ty, ts) | {
71+ load_and_resolve_all ( app, ts, resolution_context)
72+ . map ( |css| css. map ( |css| ( ty. to_owned ( ) , css) ) )
73+ } ) ;
74+ let components_by_trigger_type = join_all_result ( components_by_trigger_type_futs) . await ?;
7975
8076 for ( trigger_type, component) in components_by_trigger_type {
8177 for component in & component {
82- validate_component_against_environments (
83- envs,
84- trigger_type,
85- component,
86- resolution_context,
87- )
88- . await ?;
78+ validate_component_against_environments ( envs, & trigger_type, component) . await ?;
8979 }
9080 }
9181
@@ -96,7 +86,6 @@ async fn validate_component_against_environments(
9686 envs : & [ TargetEnvironment ] ,
9787 trigger_type : & TriggerType ,
9888 component : & ComponentToValidate < ' _ > ,
99- resolution_context : & ResolutionContext ,
10089) -> anyhow:: Result < ( ) > {
10190 let worlds = envs
10291 . iter ( )
@@ -110,21 +99,16 @@ async fn validate_component_against_environments(
11099 . map ( |w| ( e. name . as_str ( ) , w) )
111100 } )
112101 . collect :: < Result < std:: collections:: HashSet < _ > , _ > > ( ) ?;
113- validate_component_against_worlds ( worlds. into_iter ( ) , component, resolution_context ) . await ?;
102+ validate_component_against_worlds ( worlds. into_iter ( ) , component) . await ?;
114103 Ok ( ( ) )
115104}
116105
117106async fn validate_component_against_worlds (
118107 target_worlds : impl Iterator < Item = ( & str , & TargetWorld ) > ,
119108 component : & ComponentToValidate < ' _ > ,
120- resolution_context : & ResolutionContext ,
121109) -> anyhow:: Result < ( ) > {
122- let loader = ComponentSourceLoader :: new ( resolution_context. wasm_loader ( ) ) ;
123- let wasm_bytes = spin_compose:: compose ( & loader, component) . await ?;
124-
125110 for ( env_name, target_world) in target_worlds {
126- validate_wasm_against_any_world ( env_name, target_world, component, wasm_bytes. as_ref ( ) )
127- . await ?;
111+ validate_wasm_against_any_world ( env_name, target_world, component) . await ?;
128112 }
129113
130114 tracing:: info!(
@@ -139,7 +123,6 @@ async fn validate_wasm_against_any_world(
139123 env_name : & str ,
140124 target_world : & TargetWorld ,
141125 component : & ComponentToValidate < ' _ > ,
142- wasm : & [ u8 ] ,
143126) -> anyhow:: Result < ( ) > {
144127 let mut result = Ok ( ( ) ) ;
145128 for target_str in target_world. versioned_names ( ) {
@@ -148,7 +131,7 @@ async fn validate_wasm_against_any_world(
148131 component. id( ) ,
149132 component. source_description( ) ,
150133 ) ;
151- match validate_wasm_against_world ( env_name, & target_str, component, wasm ) . await {
134+ match validate_wasm_against_world ( env_name, & target_str, component) . await {
152135 Ok ( ( ) ) => {
153136 tracing:: info!(
154137 "Validated component {} {} against target world {target_str}" ,
@@ -175,7 +158,6 @@ async fn validate_wasm_against_world(
175158 env_name : & str ,
176159 target_str : & str ,
177160 component : & ComponentToValidate < ' _ > ,
178- wasm : & [ u8 ] ,
179161) -> anyhow:: Result < ( ) > {
180162 let comp_name = "root:component" ;
181163
@@ -200,7 +182,7 @@ async fn validate_wasm_against_world(
200182 . await
201183 . context ( "reg_resolver.resolve failed" ) ?;
202184
203- packages. insert ( compkey, wasm . to_vec ( ) ) ;
185+ packages. insert ( compkey, component . wasm_bytes ( ) . to_vec ( ) ) ;
204186
205187 match doc. resolve ( packages) {
206188 Ok ( _) => Ok ( ( ) ) ,
@@ -223,3 +205,15 @@ async fn validate_wasm_against_world(
223205 } ,
224206 }
225207}
208+
209+ /// Equivalent to futures::future::join_all, but specialised for iterators of
210+ /// fallible futures. It returns a Result<Vec<...>> instead of a Vec<Result<...>> -
211+ /// this just moves the transposition boilerplate out of the main flow.
212+ async fn join_all_result < T , I > ( iter : I ) -> anyhow:: Result < Vec < T > >
213+ where
214+ I : IntoIterator ,
215+ I :: Item : std:: future:: Future < Output = anyhow:: Result < T > > ,
216+ {
217+ let vec_result = futures:: future:: join_all ( iter) . await ;
218+ vec_result. into_iter ( ) . collect ( )
219+ }
0 commit comments