#[cfg(test)]
mod test_from_schema;
use jiff::{SignedDuration, Timestamp};
use rust_decimal::Decimal;
use crate::{
FromSchema as _, Kwh, LowerSchema, Money, Price, ToDuration, cdr::Warning,
number::FromDecimal as _, price::v221, schema, warning,
};
#[derive(Clone, Debug)]
pub(crate) struct Cdr {
start_date_time: Timestamp,
stop_date_time: Timestamp,
charging_periods: Vec<ChargingPeriod>,
total_cost: Decimal,
total_energy: Decimal,
duration_total: SignedDuration,
duration_idle: Option<SignedDuration>,
}
#[derive(Debug, Clone)]
struct Dimension {
dimension_type: DimensionType,
volume: Decimal,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) enum DimensionType {
Energy,
Flat,
MaxCurrent,
MinCurrent,
ParkingTime,
Time,
}
impl From<&schema::v211::CdrDimensionType> for DimensionType {
fn from(source: &schema::v211::CdrDimensionType) -> Self {
use schema::v211::CdrDimensionType;
match source {
CdrDimensionType::Energy => Self::Energy,
CdrDimensionType::Flat => Self::Flat,
CdrDimensionType::MaxCurrent => Self::MaxCurrent,
CdrDimensionType::MinCurrent => Self::MinCurrent,
CdrDimensionType::ParkingTime => Self::ParkingTime,
CdrDimensionType::Time => Self::Time,
}
}
}
#[derive(Clone, Debug)]
struct ChargingPeriod {
start_date_time: Timestamp,
dimensions: Vec<Dimension>,
}
impl From<Cdr> for v221::Cdr {
fn from(cdr: Cdr) -> Self {
let Cdr {
start_date_time,
stop_date_time,
charging_periods,
total_cost,
total_energy,
duration_total,
duration_idle,
} = cdr;
Self {
end_date_time: stop_date_time,
start_date_time,
charging_periods: charging_periods
.into_iter()
.map(ChargingPeriod::into)
.collect(),
kind: v221::cdr::Kind::Normal,
totals: v221::cdr::Totals {
cost: Price {
excl_vat: Money::from_decimal(total_cost),
incl_vat: None,
},
energy: Kwh::from_decimal(total_energy),
energy_cost: None,
duration_total,
duration_charging_cost: None,
fixed_cost: None,
duration_idle,
duration_idle_cost: None,
},
}
}
}
impl From<ChargingPeriod> for v221::cdr::ChargingPeriod {
fn from(period: ChargingPeriod) -> Self {
let ChargingPeriod {
start_date_time,
dimensions,
} = period;
let dimensions = dimensions
.into_iter()
.filter_map(|d| {
let Dimension {
dimension_type,
volume,
} = d;
if let DimensionType::Flat = dimension_type {
return None;
}
let dimension_type = match dimension_type {
DimensionType::Energy => v221::cdr::DimensionType::Energy,
DimensionType::MaxCurrent => v221::cdr::DimensionType::MaxCurrent,
DimensionType::MinCurrent => v221::cdr::DimensionType::MinCurrent,
DimensionType::ParkingTime => v221::cdr::DimensionType::ParkingTime,
DimensionType::Time => v221::cdr::DimensionType::Time,
DimensionType::Flat => return None,
};
Some(v221::cdr::Dimension {
dimension_type,
volume,
})
})
.collect();
Self {
start_date_time,
dimensions,
}
}
}
impl<'buf> LowerSchema<'buf, schema::v211::Cdr<'buf>> for Cdr {
type Warning = Warning;
fn lower_schema(
source: &schema::v211::Cdr<'buf>,
warnings: &mut dyn warning::Gather<Warning>,
) -> Result<Self, warning::Error<Warning>> {
let start_date_time = warnings.ok_or_reject(&source.start_date_time)?;
let start_date_time = Timestamp::from_schema(start_date_time, warnings)?;
let stop_date_time = warnings.ok_or_reject(&source.stop_date_time)?;
let stop_date_time = Timestamp::from_schema(stop_date_time, warnings)?;
let periods = warnings.ok_or_reject(&source.charging_periods)?;
let mut charging_periods = Vec::with_capacity(periods.len());
for period in periods {
let period = warnings.ok_or_reject(period)?;
let period = ChargingPeriod::from_schema(period, warnings)?;
charging_periods.push(period);
}
charging_periods.sort_unstable_by_key(|period| period.start_date_time);
let total_cost = warnings.ok_or_reject(&source.total_cost)?;
let total_cost = Decimal::from_schema(total_cost, warnings)?;
let total_energy = warnings.ok_or_reject(&source.total_energy)?;
let total_energy = Decimal::from_schema(total_energy, warnings)?;
let total_time = warnings.ok_or_reject(&source.total_time)?;
let total_time = Decimal::from_schema(total_time, warnings)?;
let total_parking_time = warnings.ok_or_reject(&source.total_parking_time)?;
let total_parking_time = total_parking_time
.as_ref()
.map(|time| Decimal::from_schema(time, warnings))
.transpose()?;
let cdr = Cdr {
start_date_time,
stop_date_time,
charging_periods,
total_cost,
total_energy,
duration_total: total_time.to_duration(),
duration_idle: total_parking_time.as_ref().map(ToDuration::to_duration),
};
Ok(cdr)
}
}
impl<'buf> LowerSchema<'buf, schema::v211::ChargingPeriod<'buf>> for ChargingPeriod {
type Warning = Warning;
fn lower_schema(
source: &schema::v211::ChargingPeriod<'buf>,
warnings: &mut dyn warning::Gather<Warning>,
) -> Result<Self, warning::Error<Warning>> {
let start_date_time = warnings.ok_or_reject(&source.start_date_time)?;
let start_date_time = Timestamp::from_schema(start_date_time, warnings)?;
let dimensions = warnings.ok_or_reject(&source.dimensions)?;
let mut lowered = Vec::with_capacity(dimensions.len());
for dimension in dimensions {
let dimension = warnings.ok_or_reject(dimension)?;
let dimension = Option::<Dimension>::from_schema(dimension, warnings)?;
if let Some(dimension) = dimension {
lowered.push(dimension);
}
}
let period = ChargingPeriod {
start_date_time,
dimensions: lowered,
};
Ok(period)
}
}
impl<'buf> LowerSchema<'buf, schema::v211::Dimension<'buf>> for Option<Dimension> {
type Warning = Warning;
fn lower_schema(
source: &schema::v211::Dimension<'buf>,
warnings: &mut dyn warning::Gather<Warning>,
) -> Result<Self, warning::Error<Warning>> {
if let schema::Integrity::Err(_) = &source.dimension_type {
return Ok(None);
}
let dimension_type = warnings.ok_or_reject(&source.dimension_type)?;
let dimension_type = DimensionType::from(&dimension_type.value());
let volume = warnings.ok_or_reject(&source.volume)?;
let volume = Decimal::from_schema(volume, warnings)?;
let dimension = Dimension {
dimension_type,
volume,
};
Ok(Some(dimension))
}
}