#[cfg(test)]
mod tests;

#[cfg(test)]
mod tests_ir;

#[cfg(test)]
mod tests_cdr_ir;

use super::{
    build, BuilderKind, Enum, Field, Integrity, Number, Object, Scalar, Schema, Str, Warning,
};

use crate::{json, Caveat};

// Maximum character lengths for OCPI string fields, as declared in the spec
// via the `string(n)` / `CiString(n)` notation.

// -- Identifiers ---------------------------------------------------------
const MAX_LEN_IDENTIFIER: usize = 36;
const MAX_LEN_LONG_IDENTIFIER: usize = 39;
const MAX_LEN_EVSE_ID: usize = 48;

// -- Codes ---------------------------------------------------------------
const MAX_LEN_CURRENCY: usize = 3;
const MAX_LEN_PARTY_ID: usize = 3;
const MAX_LEN_COUNTRY: usize = 3;
const MAX_LEN_COUNTRY_CODE: usize = 2;
const MAX_LEN_LANGUAGE: usize = 2;

// -- Geographic and address ----------------------------------------------
const MAX_LEN_LATITUDE: usize = 10;
const MAX_LEN_LONGITUDE: usize = 11;
const MAX_LEN_POSTAL_CODE: usize = 10;
const MAX_LEN_ADDRESS: usize = 45;
const MAX_LEN_CITY: usize = 45;
const MAX_LEN_STATE: usize = 20;

// -- Free text and names -------------------------------------------------
const MAX_LEN_NAME: usize = 255;
const MAX_LEN_URL: usize = 255;
const MAX_LEN_METER_ID: usize = 255;
const MAX_LEN_REMARK: usize = 255;
const MAX_LEN_ENERGY_MIX_NAME: usize = 64;
const MAX_LEN_DISPLAY_TEXT: usize = 512;

// -- Signed data ---------------------------------------------------------
const MAX_LEN_NATURE: usize = 32;
const MAX_LEN_PLAIN_DATA: usize = 512;
const MAX_LEN_SIGNED_DATA: usize = 5000;
const MAX_LEN_PUBLIC_KEY: usize = 512;
const MAX_LEN_SIGNED_DATA_URL: usize = 512;

// -- Enum value sets ---------------------------------------------------------
// Permitted spellings for each OCPI 2.2.1 enum that appears in the CDR/Tariff
// graph, taken verbatim from the spec. They are matched case-insensitively.

// `TokenType` (mod_tokens).
const TOKEN_TYPE_VALUES: &[&str] = &["AD_HOC_USER", "APP_USER", "OTHER", "RFID"];

// `ConnectorFormat` (mod_locations).
const CONNECTOR_FORMAT_VALUES: &[&str] = &["SOCKET", "CABLE"];

// `PowerType` (mod_locations).
const POWER_TYPE_VALUES: &[&str] = &[
    "AC_1_PHASE",
    "AC_2_PHASE",
    "AC_2_PHASE_SPLIT",
    "AC_3_PHASE",
    "DC",
];

// `ConnectorType` (mod_locations).
const CONNECTOR_TYPE_VALUES: &[&str] = &[
    "CHADEMO",
    "CHAOJI",
    "DOMESTIC_A",
    "DOMESTIC_B",
    "DOMESTIC_C",
    "DOMESTIC_D",
    "DOMESTIC_E",
    "DOMESTIC_F",
    "DOMESTIC_G",
    "DOMESTIC_H",
    "DOMESTIC_I",
    "DOMESTIC_J",
    "DOMESTIC_K",
    "DOMESTIC_L",
    "DOMESTIC_M",
    "DOMESTIC_N",
    "DOMESTIC_O",
    "GBT_AC",
    "GBT_DC",
    "IEC_60309_2_single_16",
    "IEC_60309_2_three_16",
    "IEC_60309_2_three_32",
    "IEC_60309_2_three_64",
    "IEC_62196_T1",
    "IEC_62196_T1_COMBO",
    "IEC_62196_T2",
    "IEC_62196_T2_COMBO",
    "IEC_62196_T3A",
    "IEC_62196_T3C",
    "NEMA_5_20",
    "NEMA_6_30",
    "NEMA_6_50",
    "NEMA_10_30",
    "NEMA_10_50",
    "NEMA_14_30",
    "NEMA_14_50",
    "PANTOGRAPH_BOTTOM_UP",
    "PANTOGRAPH_TOP_DOWN",
    "TESLA_R",
    "TESLA_S",
];

// `AuthMethod` (`mod_cdrs`).
const AUTH_METHOD_VALUES: &[&str] = &["AUTH_REQUEST", "COMMAND", "WHITELIST"];

// `CdrDimensionType` (`mod_cdrs`).
const CDR_DIMENSION_TYPE_VALUES: &[&str] = &[
    "CURRENT",
    "ENERGY",
    "ENERGY_EXPORT",
    "ENERGY_IMPORT",
    "MAX_CURRENT",
    "MIN_CURRENT",
    "MAX_POWER",
    "MIN_POWER",
    "PARKING_TIME",
    "POWER",
    "RESERVATION_TIME",
    "STATE_OF_CHARGE",
    "TIME",
];

// `TariffDimensionType` (mod_tariffs).
const TARIFF_DIMENSION_TYPE_VALUES: &[&str] = &["ENERGY", "FLAT", "PARKING_TIME", "TIME"];

// `DayOfWeek` (mod_tariffs).
const DAY_OF_WEEK_VALUES: &[&str] = &[
    "MONDAY",
    "TUESDAY",
    "WEDNESDAY",
    "THURSDAY",
    "FRIDAY",
    "SATURDAY",
    "SUNDAY",
];

// `ReservationRestrictionType` (mod_tariffs).
const RESERVATION_RESTRICTION_TYPE_VALUES: &[&str] = &["RESERVATION", "RESERVATION_EXPIRES"];

// `TariffType` (mod_tariffs).
const TARIFF_TYPE_VALUES: &[&str] = &[
    "AD_HOC_PAYMENT",
    "PROFILE_CHEAP",
    "PROFILE_FAST",
    "PROFILE_GREEN",
    "REGULAR",
];

// `EnergySourceCategory` (mod_locations).
const ENERGY_SOURCE_CATEGORY_VALUES: &[&str] = &[
    "NUCLEAR",
    "GENERAL_FOSSIL",
    "COAL",
    "GAS",
    "GENERAL_GREEN",
    "SOLAR",
    "WIND",
    "WATER",
];

// `EnvironmentalImpactCategory` (mod_locations).
const ENVIRONMENTAL_IMPACT_CATEGORY_VALUES: &[&str] = &["NUCLEAR_WASTE", "CARBON_DIOXIDE"];

// Item schema for arrays of enum values, which serialize as JSON strings.
static DAY_OF_WEEK: Schema = Schema::Scalar(Scalar::Enum(DAY_OF_WEEK_VALUES));

static PRICE_OBJ: Object = Object {
    fields: &[
        Field::required("excl_vat", Scalar::Number),
        Field::optional("incl_vat", Scalar::Number),
    ],
    kind: BuilderKind::V221Price,
};

static GEO_LOCATION_OBJ: Object = Object {
    // Latitude/longitude are decimal-degree strings in OCPI, not numbers.
    fields: &[
        Field::required("latitude", Scalar::StringMax(MAX_LEN_LATITUDE)),
        Field::required("longitude", Scalar::StringMax(MAX_LEN_LONGITUDE)),
    ],
    kind: BuilderKind::Ignore,
};

static CDR_TOKEN_OBJ: Object = Object {
    fields: &[
        Field::required("contract_id", Scalar::StringMax(MAX_LEN_IDENTIFIER)),
        Field::required("country_code", Scalar::StringMax(MAX_LEN_COUNTRY_CODE)),
        Field::required("party_id", Scalar::StringMax(MAX_LEN_PARTY_ID)),
        Field::required("type", Scalar::Enum(TOKEN_TYPE_VALUES)),
        Field::required("uid", Scalar::StringMax(MAX_LEN_IDENTIFIER)),
    ],
    kind: BuilderKind::Ignore,
};

static CDR_LOCATION_OBJ: Object = Object {
    fields: &[
        Field::required("address", Scalar::StringMax(MAX_LEN_ADDRESS)),
        Field::required("city", Scalar::StringMax(MAX_LEN_CITY)),
        Field::required("connector_format", Scalar::Enum(CONNECTOR_FORMAT_VALUES)),
        Field::required("connector_id", Scalar::StringMax(MAX_LEN_IDENTIFIER)),
        Field::required("connector_power_type", Scalar::Enum(POWER_TYPE_VALUES)),
        Field::required("connector_standard", Scalar::Enum(CONNECTOR_TYPE_VALUES)),
        Field::required_object("coordinates", &GEO_LOCATION_OBJ),
        Field::required("country", Scalar::StringMax(MAX_LEN_COUNTRY)),
        Field::required("evse_id", Scalar::StringMax(MAX_LEN_EVSE_ID)),
        Field::required("evse_uid", Scalar::StringMax(MAX_LEN_IDENTIFIER)),
        Field::required("id", Scalar::StringMax(MAX_LEN_IDENTIFIER)),
        Field::optional("name", Scalar::StringMax(MAX_LEN_NAME)),
        Field::optional("postal_code", Scalar::StringMax(MAX_LEN_POSTAL_CODE)),
        Field::optional("state", Scalar::StringMax(MAX_LEN_STATE)),
    ],
    kind: BuilderKind::Ignore,
};

static CDR_DIMENSION_OBJ: Object = Object {
    fields: &[
        Field::required("type", Scalar::Enum(CDR_DIMENSION_TYPE_VALUES)),
        Field::required("volume", Scalar::Number),
    ],
    kind: BuilderKind::V221CdrDimension,
};
static CDR_DIMENSION: Schema = Schema::Object(&CDR_DIMENSION_OBJ);

static CHARGING_PERIOD_OBJ: Object = Object {
    fields: &[
        Field::required_array("dimensions", &CDR_DIMENSION),
        Field::required("start_date_time", Scalar::String),
        Field::optional("tariff_id", Scalar::StringMax(MAX_LEN_IDENTIFIER)),
    ],
    kind: BuilderKind::V221ChargingPeriod,
};
static CHARGING_PERIOD: Schema = Schema::Object(&CHARGING_PERIOD_OBJ);

static SIGNED_VALUE_OBJ: Object = Object {
    fields: &[
        Field::required("nature", Scalar::StringMax(MAX_LEN_NATURE)),
        Field::required("plain_data", Scalar::StringMax(MAX_LEN_PLAIN_DATA)),
        Field::required("signed_data", Scalar::StringMax(MAX_LEN_SIGNED_DATA)),
    ],
    kind: BuilderKind::Ignore,
};
static SIGNED_VALUE: Schema = Schema::Object(&SIGNED_VALUE_OBJ);

// NOTE: encoding_method_version` is typed `int` in the 2.2.1 spec but modeled
// here as `Scalar::String`. That is a preexisting type discrepancy, not a
// length one, so it is left unchanged (and thus length-unconstrained).
static SIGNED_DATA_OBJ: Object = Object {
    fields: &[
        Field::required("encoding_method", Scalar::StringMax(MAX_LEN_IDENTIFIER)),
        Field::optional("encoding_method_version", Scalar::String),
        Field::optional("public_key", Scalar::StringMax(MAX_LEN_PUBLIC_KEY)),
        Field::required_array("signed_values", &SIGNED_VALUE),
        Field::optional("url", Scalar::StringMax(MAX_LEN_SIGNED_DATA_URL)),
    ],
    kind: BuilderKind::Ignore,
};

static DISPLAY_TEXT_OBJ: Object = Object {
    fields: &[
        Field::required("language", Scalar::StringMax(MAX_LEN_LANGUAGE)),
        Field::required("text", Scalar::StringMax(MAX_LEN_DISPLAY_TEXT)),
    ],
    kind: BuilderKind::Ignore,
};
static DISPLAY_TEXT: Schema = Schema::Object(&DISPLAY_TEXT_OBJ);

static ENERGY_SOURCE_OBJ: Object = Object {
    fields: &[
        Field::required("percentage", Scalar::Number),
        Field::required("source", Scalar::Enum(ENERGY_SOURCE_CATEGORY_VALUES)),
    ],
    kind: BuilderKind::Ignore,
};
static ENERGY_SOURCE: Schema = Schema::Object(&ENERGY_SOURCE_OBJ);

static ENVIRONMENTAL_IMPACT_OBJ: Object = Object {
    fields: &[
        Field::required("amount", Scalar::Number),
        Field::required(
            "category",
            Scalar::Enum(ENVIRONMENTAL_IMPACT_CATEGORY_VALUES),
        ),
    ],
    kind: BuilderKind::Ignore,
};
static ENVIRONMENTAL_IMPACT: Schema = Schema::Object(&ENVIRONMENTAL_IMPACT_OBJ);

static ENERGY_MIX_OBJ: Object = Object {
    fields: &[
        Field::optional(
            "energy_product_name",
            Scalar::StringMax(MAX_LEN_ENERGY_MIX_NAME),
        ),
        Field::optional_array("energy_sources", &ENERGY_SOURCE),
        Field::optional_array("environ_impact", &ENVIRONMENTAL_IMPACT),
        Field::required("is_green_energy", Scalar::Boolean),
        Field::optional("supplier_name", Scalar::StringMax(MAX_LEN_ENERGY_MIX_NAME)),
    ],
    kind: BuilderKind::Ignore,
};

static PRICE_COMPONENT_OBJ: Object = Object {
    fields: &[
        Field::required("price", Scalar::Number),
        Field::required("step_size", Scalar::Number),
        Field::required("type", Scalar::Enum(TARIFF_DIMENSION_TYPE_VALUES)),
        Field::optional("vat", Scalar::Number),
    ],
    kind: BuilderKind::V221PriceComponent,
};
static PRICE_COMPONENT: Schema = Schema::Object(&PRICE_COMPONENT_OBJ);

static TARIFF_RESTRICTIONS_OBJ: Object = Object {
    fields: &[
        Field::optional_array("day_of_week", &DAY_OF_WEEK),
        Field::optional("end_date", Scalar::String),
        Field::optional("end_time", Scalar::String),
        Field::optional("max_current", Scalar::Number),
        Field::optional("max_duration", Scalar::Number),
        Field::optional("max_kwh", Scalar::Number),
        Field::optional("max_power", Scalar::Number),
        Field::optional("min_current", Scalar::Number),
        Field::optional("min_duration", Scalar::Number),
        Field::optional("min_kwh", Scalar::Number),
        Field::optional("min_power", Scalar::Number),
        Field::optional(
            "reservation",
            Scalar::Enum(RESERVATION_RESTRICTION_TYPE_VALUES),
        ),
        Field::optional("start_date", Scalar::String),
        Field::optional("start_time", Scalar::String),
    ],
    kind: BuilderKind::V221Restrictions,
};

static TARIFF_ELEMENT_OBJ: Object = Object {
    fields: &[
        Field::required_array("price_components", &PRICE_COMPONENT),
        Field::optional_object("restrictions", &TARIFF_RESTRICTIONS_OBJ),
    ],
    kind: BuilderKind::V221Element,
};
static TARIFF_ELEMENT: Schema = Schema::Object(&TARIFF_ELEMENT_OBJ);

// -- Tariff ------------------------------------------------------------------

static TARIFF_OBJ: Object = Object {
    fields: &[
        Field::required("country_code", Scalar::StringMax(MAX_LEN_COUNTRY_CODE)),
        Field::required("currency", Scalar::StringMax(MAX_LEN_CURRENCY)),
        Field::required_array("elements", &TARIFF_ELEMENT),
        Field::optional("end_date_time", Scalar::String),
        Field::optional_object("energy_mix", &ENERGY_MIX_OBJ),
        Field::required("id", Scalar::StringMax(MAX_LEN_IDENTIFIER)),
        Field::required("last_updated", Scalar::String),
        Field::optional_object("max_price", &PRICE_OBJ),
        Field::optional_object("min_price", &PRICE_OBJ),
        Field::required("party_id", Scalar::StringMax(MAX_LEN_PARTY_ID)),
        Field::optional("start_date_time", Scalar::String),
        Field::optional_array("tariff_alt_text", &DISPLAY_TEXT),
        Field::optional("tariff_alt_url", Scalar::StringMax(MAX_LEN_URL)),
        Field::optional("type", Scalar::Enum(TARIFF_TYPE_VALUES)),
    ],
    kind: BuilderKind::V221Tariff,
};

/// OCPI 2.2.1 Tariff schema.
///
/// Validates top-level `Tariff` objects: required fields are `country_code`,
/// `currency`, `elements`, `id`, `last_updated`, and `party_id`.
static TARIFF: Schema = Schema::Object(&TARIFF_OBJ);

// -- CDR ---------------------------------------------------------------------

static CDR_OBJ: Object = Object {
    fields: &[
        Field::required("auth_method", Scalar::Enum(AUTH_METHOD_VALUES)),
        Field::optional(
            "authorization_reference",
            Scalar::StringMax(MAX_LEN_IDENTIFIER),
        ),
        Field::required_object("cdr_location", &CDR_LOCATION_OBJ),
        Field::required_object("cdr_token", &CDR_TOKEN_OBJ),
        Field::required_array("charging_periods", &CHARGING_PERIOD),
        Field::required("country_code", Scalar::StringMax(MAX_LEN_COUNTRY_CODE)),
        Field::optional("credit", Scalar::Boolean),
        Field::optional(
            "credit_reference_id",
            Scalar::StringMax(MAX_LEN_LONG_IDENTIFIER),
        ),
        Field::required("currency", Scalar::StringMax(MAX_LEN_CURRENCY)),
        Field::required("end_date_time", Scalar::String),
        Field::optional("home_charging_compensation", Scalar::Boolean),
        Field::required("id", Scalar::StringMax(MAX_LEN_LONG_IDENTIFIER)),
        Field::optional(
            "invoice_reference_id",
            Scalar::StringMax(MAX_LEN_LONG_IDENTIFIER),
        ),
        Field::required("last_updated", Scalar::String),
        Field::optional("meter_id", Scalar::StringMax(MAX_LEN_METER_ID)),
        Field::required("party_id", Scalar::StringMax(MAX_LEN_PARTY_ID)),
        Field::optional("remark", Scalar::StringMax(MAX_LEN_REMARK)),
        Field::optional("session_id", Scalar::StringMax(MAX_LEN_IDENTIFIER)),
        Field::optional_object("signed_data", &SIGNED_DATA_OBJ),
        Field::required("start_date_time", Scalar::String),
        Field::optional_array("tariffs", &TARIFF),
        Field::required_object("total_cost", &PRICE_OBJ),
        Field::required("total_energy", Scalar::Number),
        Field::optional_object("total_energy_cost", &PRICE_OBJ),
        Field::optional_object("total_fixed_cost", &PRICE_OBJ),
        Field::optional_object("total_parking_cost", &PRICE_OBJ),
        Field::optional("total_parking_time", Scalar::Number),
        Field::optional_object("total_reservation_cost", &PRICE_OBJ),
        Field::required("total_time", Scalar::Number),
        Field::optional_object("total_time_cost", &PRICE_OBJ),
    ],
    kind: BuilderKind::V221Cdr,
};

/// OCPI 2.2.1 CDR (Charge Detail Record) schema.
///
/// Validates top-level `CDR` objects including all nested types: `CdrToken`,
/// `CdrLocation`, `GeoLocation`, `ChargingPeriod`, `CdrDimension`,
/// `SignedData`, `SignedValue`, `Price`, and embedded `Tariff` objects.
static CDR: Schema = Schema::Object(&CDR_OBJ);

// -- CDR intermediate representation (IR) ------------------------------------
//
// The CDR IR models the fields pricing and validation consume: the session window,
// currency, charging periods (each with its CDR dimensions), the cost/volume totals,
// and the embedded `tariffs`. As with the tariff IR, every field is an `Integrity`
// value, so building is infallible. Fields the spec defines but this layer does not
// use (`cdr_token`, `cdr_location`, `signed_data`, the various identifiers) are
// validated by the walk but not represented here.

/// The intermediate representation of a `v2.2.1` CDR.
#[derive(Default, Clone, Debug)]
pub(crate) struct Cdr<'buf> {
    pub currency: Integrity<Str<'buf>>,
    pub start_date_time: Integrity<Str<'buf>>,
    pub end_date_time: Integrity<Str<'buf>>,
    pub charging_periods: Integrity<Vec<Integrity<ChargingPeriod<'buf>>>>,
    pub total_cost: Integrity<Price<'buf>>,
    pub total_energy: Integrity<Number<'buf>>,
    pub total_time: Integrity<Number<'buf>>,
    pub total_parking_time: Integrity<Option<Number<'buf>>>,
    pub total_energy_cost: Integrity<Option<Price<'buf>>>,
    pub total_fixed_cost: Integrity<Option<Price<'buf>>>,
    pub total_parking_cost: Integrity<Option<Price<'buf>>>,
    pub total_reservation_cost: Integrity<Option<Price<'buf>>>,
    pub total_time_cost: Integrity<Option<Price<'buf>>>,
    pub tariffs: Integrity<Option<Vec<Integrity<Tariff<'buf>>>>>,
}

/// The intermediate representation of a `v2.2.1` CDR charging period.
#[derive(Clone, Default, Debug)]
pub(crate) struct ChargingPeriod<'buf> {
    pub start_date_time: Integrity<Str<'buf>>,
    pub dimensions: Integrity<Vec<Integrity<Dimension<'buf>>>>,
}

/// The intermediate representation of a `v2.2.1` CDR dimension.
#[derive(Clone, Default, Debug)]
pub(crate) struct Dimension<'buf> {
    pub dimension_type: Integrity<Enum<'buf>>,
    pub volume: Integrity<Number<'buf>>,
}

/// The intermediate representation of a `v2.2.1` tariff.
#[derive(Clone, Default, Debug)]
pub(crate) struct Tariff<'buf> {
    pub country_code: Integrity<Str<'buf>>,
    pub currency: Integrity<Str<'buf>>,
    pub id: Integrity<Str<'buf>>,
    pub party_id: Integrity<Str<'buf>>,
    pub min_price: Integrity<Option<Price<'buf>>>,
    pub max_price: Integrity<Option<Price<'buf>>>,
    pub start_date_time: Integrity<Option<Str<'buf>>>,
    pub end_date_time: Integrity<Option<Str<'buf>>>,
    pub elements: Integrity<Vec<Integrity<Element<'buf>>>>,
}

/// The intermediate representation of a tariff element.
#[derive(Clone, Default, Debug)]
pub(crate) struct Element<'buf> {
    pub price_components: Integrity<Vec<Integrity<PriceComponent<'buf>>>>,
    pub restrictions: Integrity<Option<Restrictions<'buf>>>,
}

/// The intermediate representation of a price component.
#[derive(Clone, Default, Debug)]
pub(crate) struct PriceComponent<'buf> {
    pub dimension_type: Integrity<Enum<'buf>>,
    pub vat: Integrity<Option<Number<'buf>>>,
    pub price: Integrity<Number<'buf>>,
    pub step_size: Integrity<Number<'buf>>,
}

/// The intermediate representation of a `Price` object.
#[derive(Clone, Default, Debug)]
pub(crate) struct Price<'buf> {
    pub excl_vat: Integrity<Number<'buf>>,
    pub incl_vat: Integrity<Option<Number<'buf>>>,
}

/// The intermediate representation of a tariff element's restrictions.
#[derive(Clone, Default, Debug)]
pub(crate) struct Restrictions<'buf> {
    pub start_time: Integrity<Option<Str<'buf>>>,
    pub end_time: Integrity<Option<Str<'buf>>>,
    pub start_date: Integrity<Option<Str<'buf>>>,
    pub end_date: Integrity<Option<Str<'buf>>>,
    pub min_kwh: Integrity<Option<Number<'buf>>>,
    pub max_kwh: Integrity<Option<Number<'buf>>>,
    pub min_current: Integrity<Option<Number<'buf>>>,
    pub max_current: Integrity<Option<Number<'buf>>>,
    pub min_power: Integrity<Option<Number<'buf>>>,
    pub max_power: Integrity<Option<Number<'buf>>>,
    pub min_duration: Integrity<Option<Number<'buf>>>,
    pub max_duration: Integrity<Option<Number<'buf>>>,
    pub day_of_week: Integrity<Option<Vec<Integrity<Enum<'buf>>>>>,
    pub reservation: Integrity<Option<Enum<'buf>>>,
}

/// Build and validate a `v2.2.1` tariff from a `json::Document`.
///
/// Building is infallible: the returned [`Tariff`] always exists, with each field in
/// an [`Integrity`] state, alongside the [`Warning`]s found during the walk.
pub(crate) fn build_tariff<'buf>(doc: &json::Document<'buf>) -> Caveat<Tariff<'buf>, Warning> {
    let (node, warnings) = super::walk(doc, &TARIFF).into_parts();

    // An object root yields `Node::Tariff` (the `TARIFF` schema's `BuilderKind`); a
    // non-object root yields no tariff builder, so it falls back to an empty tariff.
    let tariff = if let build::Node::Tariff(tariff) = node {
        tariff
    } else {
        Tariff::default()
    };

    Caveat::new(tariff, warnings)
}

/// Build and validate a `v2.2.1` CDR from a `json::Document`.
///
/// Building is infallible: the returned [`Cdr`] always exists, with each field in
/// an [`Integrity`] state, alongside the [`Warning`]s found during the walk.
pub(crate) fn build_cdr<'buf>(doc: &json::Document<'buf>) -> Caveat<Cdr<'buf>, Warning> {
    let (node, warnings) = super::walk(doc, &CDR).into_parts();

    // An object root yields `Node::Cdr` (the `CDR` schema's `BuilderKind`); a
    // non-object root yields no CDR builder, so it falls back to an empty CDR.
    let cdr = if let build::Node::Cdr(cdr) = node {
        cdr
    } else {
        Cdr::default()
    };

    Caveat::new(cdr, warnings)
}
