Skip to content

Commit

Permalink
Merge pull request #18 from wbcsd/open-api-fix
Browse files Browse the repository at this point in the history
fix: change exemptedEmissionsPercent data type to match tech specs
  • Loading branch information
zeitgeist authored Jan 23, 2024
2 parents 75691b5 + 48af946 commit e9b7dcf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
35 changes: 34 additions & 1 deletion endpoint/src/datamodel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ pub struct CarbonFootprint {
#[serde(skip_serializing_if = "Option::is_none")]
pub secondary_emission_factor_sources: Option<EmissionFactorDSSet>,

pub exempted_emissions_percent: PositiveDecimal,
pub exempted_emissions_percent: ExemptedEmissionsPercent,

pub exempted_emissions_description: String,

pub packaging_emissions_included: bool,
Expand Down Expand Up @@ -180,6 +181,11 @@ pub enum CharacterizationFactors {
#[serde(crate = "rocket::serde", rename_all = "camelCase")]
pub struct PositiveDecimal(Decimal);

/// a f64 in the 0..5 range
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(crate = "rocket::serde", rename_all = "camelCase")]
pub struct ExemptedEmissionsPercent(pub f64);

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(crate = "rocket::serde", rename_all = "camelCase")]
pub struct WrappedDecimal(Decimal);
Expand Down Expand Up @@ -435,6 +441,12 @@ impl From<Decimal> for StrictlyPositiveDecimal {
}
}

impl From<f64> for ExemptedEmissionsPercent {
fn from(f: f64) -> ExemptedEmissionsPercent {
ExemptedEmissionsPercent(f)
}
}

impl From<f64> for Percent {
fn from(f: f64) -> Percent {
Percent(f)
Expand Down Expand Up @@ -604,6 +616,27 @@ impl JsonSchema for StrictlyPositiveDecimal {
}
}

impl JsonSchema for ExemptedEmissionsPercent {
fn schema_name() -> String {
"ExemptedEmissionsPercent".into()
}

fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
let mut s = match f64::json_schema(gen) {
Schema::Object(s) => s,
Schema::Bool(_) => panic!("Unexpected base schema"),
};

s.number = Some(Box::new(NumberValidation {
minimum: Some(0.00),
maximum: Some(5.0),
..(NumberValidation::default())
}));

Schema::Object(s)
}
}

impl JsonSchema for Percent {
fn schema_name() -> String {
"Percent".into()
Expand Down
2 changes: 1 addition & 1 deletion endpoint/src/sample_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ lazy_static! {
biogenic_carbon_withdrawal: None,
characterization_factors: CharacterizationFactors::Ar5,
dqi: None,
exempted_emissions_percent: dec!(0.0).into(),
exempted_emissions_percent: ExemptedEmissionsPercent(0.0),
exempted_emissions_description: "".to_string(),
fossil_carbon_content: dec!(0.0).into(),
packaging_emissions_included: false,
Expand Down

0 comments on commit e9b7dcf

Please sign in to comment.