lcax_models/
generic_impact_data.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3
4#[cfg(feature = "jsbindings")]
5use tsify_next::Tsify;
6
7#[cfg(feature = "pybindings")]
8use pyo3::prelude::*;
9
10use crate::life_cycle_base::Impacts;
11use crate::shared::{Conversion, MetaData, Reference, Source, Unit};
12use lcax_core::utils::get_version;
13
14#[derive(Deserialize, Serialize, JsonSchema, Clone, PartialEq)]
15#[serde(rename_all = "camelCase")]
16#[cfg_attr(
17    feature = "jsbindings",
18    derive(Tsify),
19    tsify(into_wasm_abi, from_wasm_abi)
20)]
21#[cfg_attr(feature = "pybindings", pyclass(get_all, set_all))]
22pub struct GenericData {
23    pub id: String,
24    pub name: String,
25    pub declared_unit: Unit,
26    pub format_version: String,
27    pub source: Option<Source>,
28    pub comment: Option<String>,
29    pub conversions: Option<Vec<Conversion>>,
30    pub impacts: Impacts,
31    pub meta_data: Option<MetaData>,
32}
33
34impl Default for GenericData {
35    fn default() -> Self {
36        Self {
37            id: uuid::Uuid::new_v4().to_string(),
38            name: "".to_string(),
39            declared_unit: Unit::UNKNOWN,
40            format_version: get_version(),
41            source: None,
42            comment: None,
43            conversions: None,
44            impacts: Impacts::new(),
45            meta_data: None,
46        }
47    }
48}
49
50#[cfg_attr(feature = "pybindings", pymethods)]
51impl GenericData {
52    #[cfg(feature = "pybindings")]
53    #[new]
54    #[pyo3(signature=(name, declared_unit, impacts, id=None, format_version=None, source=None, comment=None, conversions=None, meta_data=None ))]
55    pub fn new_py(
56        name: String,
57        declared_unit: Unit,
58        impacts: Impacts,
59        id: Option<String>,
60        format_version: Option<String>,
61        source: Option<Source>,
62        comment: Option<String>,
63        conversions: Option<Vec<Conversion>>,
64        meta_data: Option<MetaData>,
65    ) -> GenericData {
66        GenericData::new(
67            id,
68            name,
69            declared_unit,
70            format_version,
71            source,
72            comment,
73            conversions,
74            impacts,
75            meta_data,
76        )
77    }
78}
79
80impl GenericData {
81    pub fn new(
82        id: Option<String>,
83        name: String,
84        declared_unit: Unit,
85        format_version: Option<String>,
86        source: Option<Source>,
87        comment: Option<String>,
88        conversions: Option<Vec<Conversion>>,
89        impacts: Impacts,
90        meta_data: Option<MetaData>,
91    ) -> Self {
92        let _id = id.unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
93        let _format_version = format_version.unwrap_or_else(|| get_version().to_string());
94        Self {
95            id: _id,
96            name,
97            declared_unit,
98            format_version: _format_version,
99            source,
100            comment,
101            conversions,
102            impacts,
103            meta_data,
104        }
105    }
106}
107#[derive(Deserialize, Serialize, JsonSchema, Clone, PartialEq)]
108#[serde(rename_all = "camelCase")]
109#[serde(tag = "type")]
110#[cfg_attr(feature = "jsbindings", derive(Tsify))]
111#[cfg_attr(feature = "pybindings", derive(FromPyObject, IntoPyObject))]
112pub enum GenericDataReference {
113    #[serde(rename = "EPD")]
114    GenericData(GenericData),
115    Reference(Reference),
116}
117
118impl GenericDataReference {
119    pub fn resolve(&self) -> Result<GenericData, String> {
120        match self {
121            GenericDataReference::GenericData(data) => Ok(data.clone()),
122            _ => Err("Handling of references not implemented yet!".to_string()),
123        }
124    }
125
126    pub fn new(
127        _type: &str,
128        id: Option<String>,
129        name: String,
130        declared_unit: Unit,
131        format_version: Option<String>,
132        source: Option<Source>,
133        comment: Option<String>,
134        conversions: Option<Vec<Conversion>>,
135        impacts: Impacts,
136        meta_data: Option<MetaData>,
137    ) -> Self {
138        match _type {
139            "generic_data" => GenericDataReference::GenericData(GenericData::new(
140                id,
141                name,
142                declared_unit,
143                format_version,
144                source,
145                comment,
146                conversions,
147                impacts,
148                meta_data,
149            )),
150            // "reference" => GenericDataReference::Reference(Reference::new()),
151            &_ => panic!("Unknown impact type!"),
152        }
153    }
154}
155
156impl Default for GenericDataReference {
157    fn default() -> GenericDataReference {
158        GenericDataReference::GenericData(Default::default())
159    }
160}