1+ use std:: collections:: BTreeMap ;
12use std:: convert:: TryFrom ;
3+ use std:: iter:: FromIterator ;
24use std:: path:: { Path , PathBuf } ;
35
46use bindle:: { BindleSpec , Condition , Group , Invoice , Label , Parcel } ;
@@ -128,6 +130,7 @@ fn expand_handler_modules_to_parcels(
128130 convert_one_match_to_parcel (
129131 PathBuf :: from ( expansion_context. to_absolute ( & handler. name ) ) ,
130132 expansion_context,
133+ vec ! [ ( "route" , & handler. route) , ( "file" , "false" ) ] ,
131134 None ,
132135 Some ( & group_name ( handler) ) ,
133136 )
@@ -180,13 +183,17 @@ fn try_convert_one_match_to_parcel(
180183) -> anyhow:: Result < Parcel > {
181184 match path {
182185 Err ( e) => Err ( anyhow:: Error :: new ( e) ) ,
183- Ok ( path) => convert_one_match_to_parcel ( path, expansion_context, Some ( member_of) , None ) ,
186+ Ok ( path) => {
187+ let features = vec ! [ ( "file" , "true" ) ] ;
188+ convert_one_match_to_parcel ( path, expansion_context, features, Some ( member_of) , None )
189+ }
184190 }
185191}
186192
187193fn convert_one_match_to_parcel (
188194 path : PathBuf ,
189195 expansion_context : & ExpansionContext ,
196+ wagi_features : Vec < ( & str , & str ) > ,
190197 member_of : Option < & str > ,
191198 requires : Option < & str > ,
192199) -> anyhow:: Result < Parcel > {
@@ -204,12 +211,16 @@ fn convert_one_match_to_parcel(
204211 . first_or_octet_stream ( )
205212 . to_string ( ) ;
206213
214+ // let features = vec![("route", route)];
215+ let feature = Some ( wagi_feature_of ( wagi_features) ) ;
216+
207217 Ok ( Parcel {
208218 label : Label {
209219 name,
210220 sha256 : digest_string,
211221 media_type,
212222 size,
223+ feature,
213224 ..Label :: default ( )
214225 } ,
215226 conditions : Some ( Condition {
@@ -303,6 +314,17 @@ fn vector_of(option: Option<&str>) -> Option<Vec<String>> {
303314 option. map ( |val| vec ! [ val. to_owned( ) ] )
304315}
305316
317+ fn wagi_feature_of ( values : Vec < ( & str , & str ) > ) -> BTreeMap < String , BTreeMap < String , String > > {
318+ BTreeMap :: from_iter ( vec ! [ ( "wagi" . to_owned( ) , feature_map_of( values) ) ] )
319+ }
320+
321+ fn feature_map_of ( values : Vec < ( & str , & str ) > ) -> BTreeMap < String , String > {
322+ values
323+ . into_iter ( )
324+ . map ( |( k, v) | ( k. to_owned ( ) , v. to_owned ( ) ) )
325+ . collect ( )
326+ }
327+
306328#[ cfg( test) ]
307329mod test {
308330 use std:: str:: FromStr ;
@@ -332,6 +354,25 @@ mod test {
332354 . unwrap ( )
333355 }
334356
357+ fn parcel_feature_value < ' a > (
358+ invoice : & ' a Invoice ,
359+ parcel_name : & str ,
360+ feature_name : & str ,
361+ item_name : & str ,
362+ ) -> & ' a str {
363+ parcel_named ( invoice, parcel_name)
364+ . label
365+ . feature
366+ . as_ref ( )
367+ . unwrap ( )
368+ . get ( feature_name)
369+ . as_ref ( )
370+ . unwrap ( )
371+ . get ( item_name)
372+ . as_ref ( )
373+ . unwrap ( )
374+ }
375+
335376 fn parcel_conditions < ' a > ( invoice : & ' a Invoice , parcel_name : & str ) -> & ' a Condition {
336377 parcel_named ( invoice, parcel_name)
337378 . conditions
@@ -356,6 +397,24 @@ mod test {
356397 assert_eq ! ( "weather" , invoice. bindle. id. name( ) ) ;
357398 }
358399
400+ #[ test]
401+ fn test_route_is_mapped ( ) {
402+ let invoice = expand_test_invoice ( "app1" ) . unwrap ( ) ;
403+ assert_eq ! (
404+ "/fake" ,
405+ parcel_feature_value( & invoice, "out/fake.wasm" , "wagi" , "route" )
406+ ) ;
407+ }
408+
409+ #[ test]
410+ fn test_handler_parcel_is_not_asset ( ) {
411+ let invoice = expand_test_invoice ( "app1" ) . unwrap ( ) ;
412+ assert_eq ! (
413+ "false" ,
414+ parcel_feature_value( & invoice, "out/fake.wasm" , "wagi" , "file" )
415+ ) ;
416+ }
417+
359418 #[ test]
360419 fn test_group_is_created_per_handler ( ) {
361420 let invoice = expand_test_invoice ( "app1" ) . unwrap ( ) ;
@@ -386,6 +445,15 @@ mod test {
386445 ) ;
387446 }
388447
448+ #[ test]
449+ fn test_assets_parcels_are_marked_as_assets ( ) {
450+ let invoice = expand_test_invoice ( "app1" ) . unwrap ( ) ;
451+ assert_eq ! (
452+ "true" ,
453+ parcel_feature_value( & invoice, "scripts/real.js" , "wagi" , "file" )
454+ ) ;
455+ }
456+
389457 #[ test]
390458 fn test_handler_parcels_are_not_members_of_groups ( ) {
391459 let invoice = expand_test_invoice ( "app1" ) . unwrap ( ) ;
0 commit comments