Skip to content

Commit c08d675

Browse files
refactor: improvements based on @zeitgeist's review
1 parent 3dab116 commit c08d675

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

gen/src/arbitrary_impls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fn formatted_arbitrary_string(fixed: &str, g: &mut quickcheck::Gen) -> String {
5151
impl Arbitrary for ShipmentFootprint {
5252
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
5353
ShipmentFootprint {
54-
// Using u16 to avoid unreadably large
54+
// Using u16 to avoid unreadably large numbers.
5555
mass: format!("{}", u16::arbitrary(g)),
5656
shipment_id: formatted_arbitrary_string("shipment-", g),
5757
tces: NonEmptyVec::<Tce>::arbitrary(g),

gen/src/lib.rs

+21-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
//! iLEAP Data Model Extension data model
2-
use std::error::Error;
3-
42
use chrono::{DateTime, Utc};
53
use pact_data_model::{
64
CarbonFootprint, CharacterizationFactors, CompanyIdSet, CrossSectoralStandard,
@@ -631,7 +629,7 @@ pub fn to_pcf(
631629
product_or_sector_specific_rules: None, // TODO: get clarity on whether GLEC should be specified
632630
biogenic_accounting_methodology: None,
633631
boundary_processes_description: "".to_string(),
634-
reference_period_start: Utc::now(),
632+
reference_period_start: Utc::now(), // TODO: turn into parameter.
635633
reference_period_end: (Utc::now() + chrono::Duration::days(364)),
636634
geographic_scope: None,
637635
secondary_emission_factor_sources: None,
@@ -658,8 +656,8 @@ pub fn to_pcf(
658656
}
659657
}
660658

661-
pub fn gen_rnd_demo_data() -> Result<Vec<ProductFootprint>, Box<dyn Error>> {
662-
let mut og = Gen::new(10);
659+
pub fn gen_rnd_demo_data(size: usize) -> Vec<ProductFootprint> {
660+
let mut og = Gen::new(size);
663661

664662
let mut shipment_footprints = vec![];
665663
let mut tocs = vec![];
@@ -674,6 +672,7 @@ pub fn gen_rnd_demo_data() -> Result<Vec<ProductFootprint>, Box<dyn Error>> {
674672

675673
let mut i = 0;
676674
let limit = u8::arbitrary(&mut og) % 5 + 1;
675+
// TODO: improve code through pair programming with Martin.
677676
loop {
678677
let mut tce = Tce::arbitrary(&mut og);
679678

@@ -708,12 +707,10 @@ pub fn gen_rnd_demo_data() -> Result<Vec<ProductFootprint>, Box<dyn Error>> {
708707
tce.distance = GlecDistance::Actual(Decimal::from(0).into());
709708
tce.transport_activity = Decimal::from(0).into();
710709

711-
tce.co2e_wtw = WrappedDecimal::from(
712-
((hoc.co2e_intensity_wtw.0 * tce.mass.0)).round_dp(2),
713-
);
714-
tce.co2e_ttw = WrappedDecimal::from(
715-
((hoc.co2e_intensity_ttw.0 * tce.mass.0)).round_dp(2),
716-
);
710+
tce.co2e_wtw =
711+
WrappedDecimal::from((hoc.co2e_intensity_wtw.0 * tce.mass.0).round_dp(2));
712+
tce.co2e_ttw =
713+
WrappedDecimal::from((hoc.co2e_intensity_ttw.0 * tce.mass.0).round_dp(2));
717714

718715
let hoc = to_pcf(
719716
ILeapType::Hoc(hoc),
@@ -773,14 +770,20 @@ pub fn gen_rnd_demo_data() -> Result<Vec<ProductFootprint>, Box<dyn Error>> {
773770
shipment_footprints.push(ship_foot);
774771
}
775772

776-
println!("{shipment_footprints:#?}");
777-
println!("{tocs:#?}");
778-
println!("{hocs:#?}");
779-
780-
let footprints: Vec<ProductFootprint> = vec![shipment_footprints, tocs, hocs]
773+
vec![shipment_footprints, tocs, hocs]
781774
.into_iter()
782775
.flatten()
783-
.collect();
776+
.collect()
777+
}
778+
779+
#[cfg(test)]
780+
mod tests {
781+
use super::*;
784782

785-
Ok(footprints)
783+
#[test]
784+
fn test_gen_rnd_demo_data() {
785+
let footprints = gen_rnd_demo_data(10);
786+
787+
println!("{footprints:#?}");
788+
}
786789
}

0 commit comments

Comments
 (0)