Skip to content

Commit

Permalink
feat: added error handling to ilcd parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ocni-dtu committed Jan 23, 2024
1 parent 9e2f0f7 commit bc9beb9
Show file tree
Hide file tree
Showing 6 changed files with 1,742 additions and 16 deletions.
13 changes: 4 additions & 9 deletions src/epd.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use chrono::{DateTime, Utc};
use chrono::prelude::*;
use schemars::JsonSchema;
use serde::{Deserialize, Deserializer, Serialize};
use schemars::{JsonSchema};

use crate::ilcd::{ILCD, ModuleAnie};

#[derive(Debug, Serialize, JsonSchema)]
Expand Down Expand Up @@ -200,7 +201,7 @@ impl<'de> Deserialize<'de> for EPD {
let mut adpe = None;
let mut adpf = None;

for lcia_result in helper.lcia_results.lci_result.iter() {
for lcia_result in helper.lcia_results.lcia_result.iter() {
if lcia_result.reference_to_lcia_method_dataset.short_description.iter().find(|&description| description.value == "Global warming potential (GWP)").is_some() {
gwp = Some(ImpactCategory::from(&lcia_result.other.anies))
} else if lcia_result.reference_to_lcia_method_dataset.short_description.iter().find(|&description| description.value == "Depletion potential of the stratospheric ozone layer (ODP)").is_some() {
Expand Down Expand Up @@ -331,10 +332,4 @@ impl<'de> Deserialize<'de> for EPD {
eet,
})
}
}

//
// fn main() {
// let schema = schema_for!(EPD);
// println!("{}", serde_json::to_string_pretty(&schema).unwrap());
// }
}
2 changes: 1 addition & 1 deletion src/ilcd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub struct ReferenceToDescription {
#[serde(rename_all = "camelCase")]
pub struct LCIAResults {
#[serde(alias = "LCIAResult")]
pub lci_result: Vec<LCIAResult>,
pub lcia_result: Vec<LCIAResult>,
}

#[derive(Deserialize, Debug)]
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ mod python;
#[cfg(feature = "default")]
pub fn convert_ilcd(json: String) -> String {
let epd = parse::parse_ilcd(json);
serde_json::to_string(&epd).unwrap()
match epd {
Ok(epd) => serde_json::to_string(&epd).unwrap(),
Err(_) => String::from("")
}
}
10 changes: 7 additions & 3 deletions src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use serde_json::Error;
use crate::epd::EPD;

/// Parse a ILCD formatted EPD in an EPDx struct
Expand All @@ -7,7 +8,10 @@ use crate::epd::EPD;
/// * `json`: JSON formatted string containing the "full" EPD on ILCD format
///
/// returns: EPD
pub fn parse_ilcd(json: String) -> EPD {
let epd: EPD = serde_json::from_str(&json).unwrap();
epd
pub fn parse_ilcd(json: String) -> Result<EPD, Error> {
let epd = serde_json::from_str(&json);
match epd {
Ok(epd) => Ok(epd),
Err(err) => Err(err)
}
}

0 comments on commit bc9beb9

Please sign in to comment.