#[cfg(test)]
mod test_every_field_set;
#[cfg(test)]
mod test_from_schema;
use jiff::{SignedDuration, Timestamp, civil};
use rust_decimal::Decimal;
use super::{CpoId, Warning, v2x};
use crate::{
FromSchema as _, LowerSchema, Money, Price, Weekday, country, currency,
duration::Seconds,
energy::{Ampere, Kw, Kwh},
money::VatOrigin,
number::FromDecimal as _,
schema, string, warning,
};
#[derive(Debug)]
pub(crate) struct Tariff<'buf> {
pub party_id: Option<CpoId<'buf>>,
pub id: string::AsciiPrintable<'buf>,
pub currency: currency::Code,
pub min_price: Option<Price>,
pub max_price: Option<Price>,
pub start_date_time: Option<Timestamp>,
pub end_date_time: Option<Timestamp>,
pub elements: Vec<Element>,
}
#[derive(Debug)]
pub(crate) struct Element {
pub price_components: Vec<PriceComponent>,
pub restrictions: Option<Restrictions>,
}
#[derive(Debug)]
pub(crate) struct PriceComponent {
pub dimension_type: v2x::DimensionType,
pub vat: VatOrigin,
pub price: Money,
pub step_size: u64,
}
#[derive(Copy, Clone, Debug)]
pub(crate) enum ReservationRestrictionType {
Reservation,
ReservationExpires,
}
impl From<&schema::v221::ReservationRestrictionType> for ReservationRestrictionType {
fn from(source: &schema::v221::ReservationRestrictionType) -> Self {
use schema::v221::ReservationRestrictionType;
match source {
ReservationRestrictionType::Reservation => Self::Reservation,
ReservationRestrictionType::ReservationExpires => Self::ReservationExpires,
}
}
}
#[derive(Debug)]
pub(crate) struct Restrictions {
pub start_time: Option<civil::Time>,
pub end_time: Option<civil::Time>,
pub start_date: Option<civil::Date>,
pub end_date: Option<civil::Date>,
pub min_kwh: Option<Kwh>,
pub max_kwh: Option<Kwh>,
pub min_current: Option<Ampere>,
pub max_current: Option<Ampere>,
pub min_power: Option<Kw>,
pub max_power: Option<Kw>,
pub min_duration: Option<SignedDuration>,
pub max_duration: Option<SignedDuration>,
pub day_of_week: Option<Vec<Weekday>>,
pub reservation: Option<ReservationRestrictionType>,
}
impl<'buf> LowerSchema<'buf, schema::v221::Tariff<'buf>> for Tariff<'buf> {
type Warning = Warning;
fn lower_schema(
source: &schema::v221::Tariff<'buf>,
warnings: &mut dyn warning::Gather<Warning>,
) -> Result<Self, warning::Error<Warning>> {
let country_code = warnings.ok_or_reject(&source.country_code)?;
let country_code = country::CodeSet::from_schema(country_code, warnings)?;
let country_code = match country_code {
country::CodeSet::Alpha2(code) | country::CodeSet::Alpha3(code) => code,
};
let currency = warnings.ok_or_reject(&source.currency)?;
let currency = currency::Code::from_schema(currency, warnings)?;
let id = warnings.ok_or_reject(&source.id)?;
let id = string::AsciiPrintable::from_schema(id, warnings)?;
let party_id = warnings.ok_or_reject(&source.party_id)?;
let party_id = string::AsciiExactLen::<'_, 3>::from_schema(party_id, warnings)?;
let min_price = warnings.ok_or_reject(&source.min_price)?;
let min_price = min_price
.as_ref()
.map(|value| Price::from_schema(value, warnings))
.transpose()?;
let max_price = warnings.ok_or_reject(&source.max_price)?;
let max_price = max_price
.as_ref()
.map(|value| Price::from_schema(value, warnings))
.transpose()?;
let start_date_time = warnings.ok_or_reject(&source.start_date_time)?;
let start_date_time = start_date_time
.as_ref()
.map(|value| Timestamp::from_schema(value, warnings))
.transpose()?;
let end_date_time = warnings.ok_or_reject(&source.end_date_time)?;
let end_date_time = end_date_time
.as_ref()
.map(|value| Timestamp::from_schema(value, warnings))
.transpose()?;
let elements = warnings.ok_or_reject(&source.elements)?;
let mut lowered = Vec::with_capacity(elements.len());
for element in elements {
let element = warnings.ok_or_reject(element)?;
lowered.push(Element::from_schema(element, warnings)?);
}
let tariff = Tariff {
currency,
party_id: Some(CpoId {
country_code,
id: party_id,
}),
id,
min_price,
max_price,
start_date_time,
end_date_time,
elements: lowered,
};
Ok(tariff)
}
}
impl<'buf> LowerSchema<'buf, schema::v221::Element<'buf>> for Element {
type Warning = Warning;
fn lower_schema(
source: &schema::v221::Element<'buf>,
warnings: &mut dyn warning::Gather<Warning>,
) -> Result<Self, warning::Error<Warning>> {
let components = warnings.ok_or_reject(&source.price_components)?;
let mut price_components = Vec::with_capacity(components.len());
for component in components {
let component = warnings.ok_or_reject(component)?;
let component = Option::<PriceComponent>::from_schema(component, warnings)?;
if let Some(component) = component {
price_components.push(component);
}
}
let restrictions = warnings.ok_or_reject(&source.restrictions)?;
let restrictions = restrictions
.as_ref()
.map(|value| Restrictions::from_schema(value, warnings))
.transpose()?;
let elem = Element {
price_components,
restrictions,
};
Ok(elem)
}
}
impl<'buf> LowerSchema<'buf, schema::v221::PriceComponent<'buf>> for Option<PriceComponent> {
type Warning = Warning;
fn lower_schema(
source: &schema::v221::PriceComponent<'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 = v2x::DimensionType::from(&dimension_type.value());
let vat = source
.vat
.map_some(|value| VatOrigin::from_schema(value, warnings))
.transpose()?
.unwrap_or(VatOrigin::NotProvided);
let price = warnings.ok_or_reject(&source.price)?;
let price = Decimal::from_schema(price, warnings)?;
let step_size = warnings.ok_or_reject(&source.step_size)?;
let step_size = u64::from_schema(step_size, warnings)?;
let comp = PriceComponent {
dimension_type,
vat,
price: Money::from_decimal(price),
step_size,
};
Ok(Some(comp))
}
}
impl<'buf> LowerSchema<'buf, schema::v221::Restrictions<'buf>> for Restrictions {
type Warning = Warning;
fn lower_schema(
source: &schema::v221::Restrictions<'buf>,
warnings: &mut dyn warning::Gather<Warning>,
) -> Result<Self, warning::Error<Warning>> {
let start_time = warnings.ok_or_reject(&source.start_time)?;
let start_time = start_time
.as_ref()
.map(|value| civil::Time::from_schema(value, warnings))
.transpose()?;
let end_time = warnings.ok_or_reject(&source.end_time)?;
let end_time = end_time
.as_ref()
.map(|value| civil::Time::from_schema(value, warnings))
.transpose()?;
let end_time = end_time.filter(|end_time| *end_time != civil::Time::midnight());
let start_date = warnings.ok_or_reject(&source.start_date)?;
let start_date = start_date
.as_ref()
.map(|value| civil::Date::from_schema(value, warnings))
.transpose()?;
let end_date = warnings.ok_or_reject(&source.end_date)?;
let end_date = end_date
.as_ref()
.map(|value| civil::Date::from_schema(value, warnings))
.transpose()?;
let min_kwh = warnings.ok_or_reject(&source.min_kwh)?;
let min_kwh = min_kwh
.as_ref()
.map(|value| Kwh::from_schema(value, warnings))
.transpose()?;
let max_kwh = warnings.ok_or_reject(&source.max_kwh)?;
let max_kwh = max_kwh
.as_ref()
.map(|value| Kwh::from_schema(value, warnings))
.transpose()?;
let min_current = warnings.ok_or_reject(&source.min_current)?;
let min_current = min_current
.as_ref()
.map(|value| Ampere::from_schema(value, warnings))
.transpose()?;
let max_current = warnings.ok_or_reject(&source.max_current)?;
let max_current = max_current
.as_ref()
.map(|value| Ampere::from_schema(value, warnings))
.transpose()?;
let min_power = warnings.ok_or_reject(&source.min_power)?;
let min_power = min_power
.as_ref()
.map(|value| Kw::from_schema(value, warnings))
.transpose()?;
let max_power = warnings.ok_or_reject(&source.max_power)?;
let max_power = max_power
.as_ref()
.map(|value| Kw::from_schema(value, warnings))
.transpose()?;
let min_duration = warnings.ok_or_reject(&source.min_duration)?;
let min_duration = min_duration
.as_ref()
.map(|value| Seconds::from_schema(value, warnings))
.transpose()?
.map(SignedDuration::from);
let max_duration = warnings.ok_or_reject(&source.max_duration)?;
let max_duration = max_duration
.as_ref()
.map(|value| Seconds::from_schema(value, warnings))
.transpose()?
.map(SignedDuration::from);
let day_of_week = warnings.ok_or_reject(&source.day_of_week)?;
let day_of_week = match day_of_week {
Some(days) => {
let mut weekdays = Vec::with_capacity(days.len());
for day in days {
let day = warnings.ok_or_reject(day)?;
weekdays.push(Weekday::from(&day.value()));
}
Some(weekdays)
}
None => None,
};
let day_of_week = day_of_week.filter(|days| !days.is_empty());
let reservation = warnings.ok_or_reject(&source.reservation)?;
let reservation = reservation
.as_ref()
.map(|reservation| ReservationRestrictionType::from(&reservation.value()));
let res = Restrictions {
start_time,
end_time,
start_date,
end_date,
min_kwh,
max_kwh,
min_current,
max_current,
min_power,
max_power,
min_duration,
max_duration,
day_of_week,
reservation,
};
Ok(res)
}
}