@@ -244,9 +244,8 @@ pub struct Tad {
244
244
pub packaging_or_tr_eq_type : Option < PackagingOrTrEqType > ,
245
245
#[ serde( skip_serializing_if = "Option::is_none" ) ]
246
246
pub packaging_or_tr_eq_amount : Option < usize > ,
247
- // TODO: verify whether the absence of this property is intended.
248
- // #[serde(skip_serializing_if = "Option::is_none")]
249
- // pub energy_carrier: EnergyCarrier,
247
+ // TODO: verify whether the absence of this property is intended. #[serde(skip_serializing_if =
248
+ // "Option::is_none")] pub energy_carrier: EnergyCarrier,
250
249
#[ serde( skip_serializing_if = "Option::is_none" ) ]
251
250
pub feedstocks : Option < Vec < Feedstock > > ,
252
251
}
@@ -656,50 +655,48 @@ pub fn to_pcf(
656
655
}
657
656
}
658
657
659
- pub fn gen_rnd_demo_data ( size : usize ) -> Vec < ProductFootprint > {
660
- let mut og = Gen :: new ( size) ;
658
+ // TODO: invert logic to generate a list of HOCs and TOCs and only then generate TCEs, improving
659
+ // readability and demo data quality, as suggested by Martin.
660
+ pub fn gen_rnd_demo_data ( size : u8 ) -> Vec < ProductFootprint > {
661
+ let mut og = Gen :: new ( size as usize ) ;
661
662
662
663
let mut shipment_footprints = vec ! [ ] ;
663
664
let mut tocs = vec ! [ ] ;
664
665
let mut hocs = vec ! [ ] ;
665
666
666
- let num_of_shipments = u8:: arbitrary ( & mut og) % 5 + 1 ;
667
+ let num_of_shipments = u8:: arbitrary ( & mut og) % size + 1 ;
667
668
for _ in 0 ..num_of_shipments {
668
669
let mut ship_foot = ShipmentFootprint :: arbitrary ( & mut og) ;
669
670
670
671
let mut tces: Vec < Tce > = vec ! [ ] ;
671
672
let mut prev_tces: Vec < String > = vec ! [ ] ;
672
673
673
674
let mut i = 0 ;
674
- let limit = u8:: arbitrary ( & mut og) % 5 + 1 ;
675
+ let limit = u8:: arbitrary ( & mut og) % size + 1 ;
675
676
// TODO: improve code through pair programming with Martin.
676
677
loop {
677
678
let mut tce = Tce :: arbitrary ( & mut og) ;
678
679
679
- if ( tce. toc_id . is_none ( ) && tce. hoc_id . is_none ( ) )
680
- || tce. toc_id . is_some ( ) && tce. hoc_id . is_some ( )
681
- {
682
- panic ! ( "Either Toc or Hoc, but not both, must be provided" ) ;
683
- }
684
-
685
680
if let Some ( prev_tce) = tces. last ( ) {
686
681
// Updates prevTceIds for the current TCE
687
682
prev_tces. push ( prev_tce. tce_id . clone ( ) ) ;
688
683
tce. prev_tce_ids = Some ( prev_tces. clone ( ) ) ;
689
684
690
685
// Avoids having two HOCs follow one another
691
686
if prev_tce. hoc_id . is_some ( ) && tce. hoc_id . is_some ( ) {
692
- continue ;
687
+ tce = Tce :: arbitrary ( & mut og ) ;
693
688
}
694
689
} ;
695
690
691
+ if i == 0 || i == limit - 1 && tce. hoc_id . is_some ( ) {
692
+ tce = Tce :: arbitrary ( & mut og) ;
693
+ }
694
+
696
695
if tce. hoc_id . is_some ( ) {
697
696
// Avoids having an HOC as the first or the last TCE
698
- if i == 0 || i == limit - 1 {
699
- continue ;
700
- }
701
697
702
698
let mut hoc = Hoc :: arbitrary ( & mut og) ;
699
+
703
700
hoc. hoc_id = tce. hoc_id . clone ( ) . unwrap ( ) ;
704
701
705
702
tce. hoc_id = Some ( hoc. hoc_id . clone ( ) ) ;
@@ -732,6 +729,7 @@ pub fn gen_rnd_demo_data(size: usize) -> Vec<ProductFootprint> {
732
729
. into ( ) ;
733
730
734
731
tce. toc_id = Some ( toc. toc_id . clone ( ) ) ;
732
+
735
733
tce. co2e_wtw = WrappedDecimal :: from (
736
734
( toc. co2e_intensity_wtw . 0 * tce. transport_activity . 0 ) . round_dp ( 2 ) ,
737
735
) ;
@@ -784,6 +782,24 @@ mod tests {
784
782
fn test_gen_rnd_demo_data ( ) {
785
783
let footprints = gen_rnd_demo_data ( 10 ) ;
786
784
785
+ for footprint in footprints. iter ( ) {
786
+ if let Some ( extensions) = & footprint. extensions {
787
+ for extension in extensions. iter ( ) {
788
+ if let Some ( ship_foot) = extension. data . get ( "ShipmentFootprint" ) {
789
+ let ship_foot =
790
+ serde_json:: from_value :: < ShipmentFootprint > ( ship_foot. to_owned ( ) )
791
+ . unwrap ( ) ;
792
+ for tce in ship_foot. tces . 0 . iter ( ) {
793
+ assert ! (
794
+ tce. toc_id. is_some( ) ^ tce. hoc_id. is_some( ) ,
795
+ "Either tocId or hocId, but not both, must be provided."
796
+ ) ;
797
+ }
798
+ }
799
+ }
800
+ }
801
+ }
802
+
787
803
println ! ( "{footprints:#?}" ) ;
788
804
}
789
805
}
0 commit comments