#[cfg(test)]
pub(crate) mod test;
#[cfg(test)]
mod test_build;
#[cfg(test)]
mod test_real_world;
pub(crate) mod v211;
pub(crate) mod v221;
pub(crate) mod v2x;
use std::{borrow::Cow, fmt};
use crate::{
FromSchema as _, Verdict, country, currency, datetime, duration, explain, from_warning_all,
json, lint, money, number, schema, string, version,
warning::{self, Caveat},
};
#[derive(Debug)]
pub enum Warning {
Country(country::Warning),
Currency(currency::Warning),
DateTime(datetime::Warning),
Decode(json::decode::Warning),
Duration(duration::Warning),
FieldInvalidValue {
value: String,
message: Cow<'static, str>,
},
Money(money::Warning),
NoElements,
Number(number::Warning),
String(string::Warning),
Rejected,
}
impl Warning {
fn field_invalid_value(
value: impl Into<String>,
message: impl Into<Cow<'static, str>>,
) -> Self {
Warning::FieldInvalidValue {
value: value.into(),
message: message.into(),
}
}
}
impl fmt::Display for Warning {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::String(warning_kind) => write!(f, "{warning_kind}"),
Self::Country(warning_kind) => write!(f, "{warning_kind}"),
Self::Currency(warning_kind) => write!(f, "{warning_kind}"),
Self::DateTime(warning_kind) => write!(f, "{warning_kind}"),
Self::Decode(warning_kind) => write!(f, "{warning_kind}"),
Self::Duration(warning_kind) => write!(f, "{warning_kind}"),
Self::FieldInvalidValue { value, message } => {
write!(f, "Field has invalid value `{value}`: {message}")
}
Self::Money(warning_kind) => write!(f, "{warning_kind}"),
Self::NoElements => f.write_str("The tariff has no `elements`"),
Self::Number(warning_kind) => write!(f, "{warning_kind}"),
Self::Rejected => f.write_str(
"The schema IR for a tariff object was rejected; see the schema \
validation warnings.",
),
}
}
}
impl crate::Warning for Warning {
fn id(&self) -> warning::Id {
match self {
Self::String(warning) => warning.id(),
Self::Country(warning) => warning.id(),
Self::Currency(warning) => warning.id(),
Self::DateTime(warning) => warning.id(),
Self::Decode(warning) => warning.id(),
Self::Duration(warning) => warning.id(),
Self::FieldInvalidValue { value, message: _ } => {
warning::Id::from_string(format!("field_invalid_value({value})"))
}
Self::Money(warning) => warning.id(),
Self::NoElements => warning::Id::from_static("no_elements"),
Self::Number(warning) => warning.id(),
Self::Rejected => warning::Id::from_static("rejected"),
}
}
fn is_rejected(&self) -> bool {
matches!(self, Self::Rejected)
}
}
impl From<warning::Rejected> for Warning {
fn from(_: warning::Rejected) -> Self {
Self::Rejected
}
}
from_warning_all!(
country::Warning => Warning::Country,
currency::Warning => Warning::Currency,
datetime::Warning => Warning::DateTime,
duration::Warning => Warning::Duration,
json::decode::Warning => Warning::Decode,
money::Warning => Warning::Money,
number::Warning => Warning::Number,
string::Warning => Warning::String
);
#[derive(Clone, Debug)]
pub(crate) struct CpoId<'buf> {
pub country_code: country::Code,
pub id: string::AsciiExactLen<'buf, 3>,
}
pub fn infer_version(json: json::Document<'_>) -> VersionGuess<'_> {
version::tariff_version(json)
}
pub type VersionGuess<'buf> = version::Guess<VersionedJson<'buf>, Unversioned<'buf>>;
pub fn check(
json: json::Document<'_>,
version: crate::Version,
) -> Caveat<Checked<'_>, schema::Warning> {
let mut warnings = warning::Gatherer::new();
let version = match version {
crate::Version::V221 => Version::V221(schema::v221::build_tariff(&json, &mut warnings)),
crate::Version::V211 => Version::V211(schema::v211::build_tariff(&json, &mut warnings)),
};
let checked = Checked { doc: json, version };
Caveat::new(checked, warnings.into_set())
}
pub fn build(tariff: Checked<'_>) -> Verdict<Tariff<'_>, Warning> {
let mut warnings = warning::Gatherer::new();
let v221 = tariff.lower_to_v221(&mut warnings);
let element = tariff.into_doc().into_root();
let tariff = v221.map(|tariff| Tariff { tariff, element });
warnings.into_verdict(tariff)
}
pub struct Tariff<'buf> {
tariff: v221::Tariff<'buf>,
element: json::Element<'buf>,
}
impl fmt::Debug for Tariff<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.tariff, f)
}
}
impl<'buf> Tariff<'buf> {
pub(crate) fn new(tariff: v221::Tariff<'buf>, element: json::Element<'buf>) -> Self {
Self { tariff, element }
}
pub(crate) fn as_v221(&self) -> &v221::Tariff<'buf> {
&self.tariff
}
pub(crate) fn as_element(&self) -> &json::Element<'buf> {
&self.element
}
}
#[derive(Clone)]
pub struct VersionedJson<'buf> {
doc: json::Document<'buf>,
version: crate::Version,
}
#[derive(Clone)]
pub struct Checked<'buf> {
doc: json::Document<'buf>,
version: Version<'buf>,
}
impl fmt::Debug for Checked<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if f.alternate() {
match &self.version {
Version::V211(tariff) => fmt::Debug::fmt(&tariff, f),
Version::V221(tariff) => fmt::Debug::fmt(&tariff, f),
}
} else {
match &self.version {
Version::V211(_) => f.write_str("V211"),
Version::V221(_) => f.write_str("V221"),
}
}
}
}
impl version::Versioned for Checked<'_> {
fn version(&self) -> crate::Version {
match self.version {
Version::V211(_) => crate::Version::V211,
Version::V221(_) => crate::Version::V221,
}
}
}
impl<'buf> Checked<'buf> {
pub(crate) fn lower_to_v221(
&self,
warnings: &mut dyn warning::Gather<Warning>,
) -> Result<v221::Tariff<'buf>, warning::Error<Warning>> {
let tariff = match &self.version {
Version::V211(tariff) => {
let tariff = v211::Tariff::from_schema(tariff, warnings)?;
v221::Tariff::from(tariff)
}
Version::V221(tariff) => v221::Tariff::from_schema(tariff, warnings)?,
};
if tariff.elements.is_empty() {
return Err(warning::Error::new(self.as_element(), Warning::NoElements));
}
Ok(tariff)
}
pub(crate) fn schema(&self) -> &Version<'buf> {
&self.version
}
pub fn into_doc(self) -> json::Document<'buf> {
self.doc
}
pub(crate) fn as_element(&self) -> &json::Element<'buf> {
self.doc.root()
}
pub fn as_doc(&self) -> &json::Document<'buf> {
&self.doc
}
pub fn as_json_str(&self) -> &'buf str {
self.doc.source()
}
}
#[expect(
clippy::large_enum_variant,
reason = "the v2.1.1 and v2.2.1 tariff IRs differ in size; this short-lived versioned \
value is not worth boxing"
)]
#[derive(Clone)]
pub(crate) enum Version<'buf> {
V211(schema::v211::Tariff<'buf>),
V221(schema::v221::Tariff<'buf>),
}
impl fmt::Debug for VersionedJson<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if f.alternate() {
fmt::Debug::fmt(&self.doc, f)
} else {
match self.version {
crate::Version::V211 => f.write_str("V211"),
crate::Version::V221 => f.write_str("V221"),
}
}
}
}
impl version::Versioned for VersionedJson<'_> {
fn version(&self) -> crate::Version {
self.version
}
}
impl<'buf> VersionedJson<'buf> {
pub(crate) fn new(doc: json::Document<'buf>, version: crate::Version) -> Self {
Self { doc, version }
}
pub fn into_doc(self) -> json::Document<'buf> {
self.doc
}
pub fn as_doc(&self) -> &json::Document<'buf> {
&self.doc
}
pub fn as_json_str(&self) -> &'buf str {
self.doc.source()
}
}
#[derive(Debug)]
pub struct Unversioned<'buf> {
doc: json::Document<'buf>,
}
impl<'buf> Unversioned<'buf> {
pub(crate) fn new(elem: json::Document<'buf>) -> Self {
Self { doc: elem }
}
pub fn into_doc(self) -> json::Document<'buf> {
self.doc
}
}
impl<'buf> version::Unversioned for Unversioned<'buf> {
type Versioned = VersionedJson<'buf>;
fn force_into_versioned(self, version: crate::Version) -> VersionedJson<'buf> {
let Self { doc } = self;
VersionedJson { doc, version }
}
}
pub fn lint(tariff: &Checked<'_>) -> lint::tariff::Report {
lint::tariff(tariff)
}
pub fn fix(
tariff: &Checked<'_>,
options: lint::fix::Options,
) -> Result<lint::fix::Fixed, crate::fix::Error> {
lint::fix::tariff(tariff, options)
}
pub fn explain(tariff: &Tariff<'_>, language: crate::Language) -> String {
explain::tariff(tariff.as_v221(), language)
}