use serde::{Deserialize, Serialize};
use crate::traits::{Bo4eMeta, Bo4eObject};
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OfferPosition {
#[serde(flatten)]
pub meta: Bo4eMeta,
#[serde(skip_serializing_if = "Option::is_none")]
pub position_description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub position_price_value: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub position_quantity_value: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub position_cost_value: Option<f64>,
}
impl Bo4eObject for OfferPosition {
fn type_name_german() -> &'static str {
"Angebotsposition"
}
fn type_name_english() -> &'static str {
"OfferPosition"
}
fn meta(&self) -> &Bo4eMeta {
&self.meta
}
fn meta_mut(&mut self) -> &mut Bo4eMeta {
&mut self.meta
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_offer_position_default() {
let pos = OfferPosition::default();
assert!(pos.position_description.is_none());
assert!(pos.position_price_value.is_none());
}
#[test]
fn test_offer_position_serialize() {
let pos = OfferPosition {
position_description: Some("Electricity delivery".to_string()),
position_price_value: Some(24.56),
position_quantity_value: Some(4000.0),
position_cost_value: Some(982.40),
..Default::default()
};
let json = serde_json::to_string(&pos).unwrap();
assert!(json.contains(r#""positionDescription":"Electricity delivery""#));
assert!(json.contains("24.56"));
assert!(json.contains("4000"));
}
#[test]
fn test_offer_position_roundtrip() {
let pos = OfferPosition {
meta: Bo4eMeta::with_type("Angebotsposition"),
position_description: Some("Gas supply".to_string()),
position_price_value: Some(0.08),
position_quantity_value: Some(10000.0),
position_cost_value: Some(800.0),
};
let json = serde_json::to_string(&pos).unwrap();
let parsed: OfferPosition = serde_json::from_str(&json).unwrap();
assert_eq!(pos, parsed);
}
#[test]
fn test_bo4e_object_impl() {
assert_eq!(OfferPosition::type_name_german(), "Angebotsposition");
assert_eq!(OfferPosition::type_name_english(), "OfferPosition");
}
}