meteroid-rs 0.31.4

Official Rust SDK for Meteroid, the open-source billing platform: subscriptions, invoicing, usage-based billing and webhooks.
Documentation
// this file is @generated by Perseid; edit the shared runtime template.
//! Serde helpers for the generated models: unions and nullable fields.
#![allow(dead_code)]
use serde::{
    de::{DeserializeOwned, Error as _},
    ser::Error as _,
    Deserialize, Deserializer, Serialize, Serializer,
};
use serde_json::{Map, Value};

/// Writes `value` as an object carrying `field: tag`, whether or not its type has that field.
pub(crate) fn internally_tagged<S: Serializer, T: Serialize>(
    serializer: S,
    field: &str,
    tag: &str,
    value: &T,
) -> Result<S::Ok, S::Error> {
    match serde_json::to_value(value).map_err(S::Error::custom)? {
        Value::Object(mut object) => {
            object.insert(field.to_owned(), tag.into());
            object.serialize(serializer)
        }
        _ => Err(S::Error::custom(format!(
            "the `{tag}` variant is not a JSON object"
        ))),
    }
}

/// Writes `{field: tag, content: value}`.
pub(crate) fn adjacently_tagged<S: Serializer, T: Serialize>(
    serializer: S,
    field: &str,
    tag: &str,
    content: &str,
    value: Option<&T>,
) -> Result<S::Ok, S::Error> {
    let mut object = Map::new();
    object.insert(field.to_owned(), tag.into());
    if let Some(value) = value {
        let value = serde_json::to_value(value).map_err(S::Error::custom)?;
        object.insert(content.to_owned(), value);
    }
    object.serialize(serializer)
}

/// Reads a tagged union as raw JSON along with its `field` value.
pub(crate) fn split_tag<'de, D: Deserializer<'de>>(
    deserializer: D,
    field: &str,
) -> Result<(String, Value), D::Error> {
    let value = Value::deserialize(deserializer)?;
    let tag = match value.get(field) {
        Some(Value::String(tag)) => tag.clone(),
        Some(_) => return Err(D::Error::custom(format!("`{field}` is not a string"))),
        None => return Err(D::Error::custom(format!("missing field `{field}`"))),
    };
    Ok((tag, value))
}

pub(crate) fn from_value<T: DeserializeOwned, E: serde::de::Error>(value: Value) -> Result<T, E> {
    serde_json::from_value(value).map_err(E::custom)
}

/// `value` without its `field`, such as the tag a variant struct does not declare.
pub(crate) fn without(mut value: Value, field: &str) -> Value {
    if let Value::Object(object) = &mut value {
        object.remove(field);
    }
    value
}

pub(crate) fn take(value: &mut Value, field: &str) -> Value {
    value.get_mut(field).map(Value::take).unwrap_or_default()
}

/// Reads a field that may be absent (`None`), `null` (`Some(None)`) or set.
#[allow(clippy::option_option)]
pub(crate) fn nullable<'de, D, T>(deserializer: D) -> Result<Option<Option<T>>, D::Error>
where
    D: Deserializer<'de>,
    T: Deserialize<'de>,
{
    Option::<T>::deserialize(deserializer).map(Some)
}

/// The JSON type telling apart the variants of an untagged union; `""` is `empty`.
pub(crate) fn json_type(value: &Value) -> &'static str {
    match value {
        Value::Null => "null",
        Value::Bool(_) => "boolean",
        Value::Number(number) if number.is_f64() => "number",
        Value::Number(_) => "integer",
        Value::String(text) if text.is_empty() => "empty",
        Value::String(_) => "string",
        Value::Array(_) => "array",
        Value::Object(_) => "object",
    }
}