calmlens 0.29.2

Rust SDK for CalmLens moderation, storage and CDN delivery
Documentation
//! Contract-test support, not part of the public API: a description of each options and model
//! type's declared shape (wire fields with their serde attributes, Rust kinds, `Option`, `Vec`,
//! maps, nested types and enum wire values), so the declared-type contract test can compare it
//! with the API's JSON Schema. Rust has no runtime reflection, so the shapes come from
//! `#[macro_rules_derive(described)]` on each type and from `string_enum!`.
use bytes::Bytes;
use serde_json::{Map, Value};
use std::collections::{BTreeMap, HashMap};

/// A struct field, or the payload of an enum variant.
#[derive(Debug, Clone, Copy)]
pub struct Field {
    /// The Rust name.
    pub name: &'static str,
    /// The field's attributes, as written (`serde(...)`, `doc = "..."`).
    pub attrs: &'static [&'static str],
    /// The field type's shape.
    pub shape: fn() -> Shape,
}

/// The declared shape of a Rust type.
#[derive(Debug, Clone)]
pub enum Shape {
    /// `()`: the method returns no model.
    Unit,
    /// `String`.
    String,
    /// `bool`.
    Bool,
    /// An integer type.
    Integer,
    /// A floating-point type.
    Number,
    /// Any JSON value (`serde_json::Value`).
    Json,
    /// An SDK-native value with no JSON form, such as upload content.
    Native(&'static str),
    /// `Option<T>`.
    Option(Box<Shape>),
    /// [`Patch<T>`](crate::models::Patch).
    Patch(Box<Shape>),
    /// `Vec<T>`.
    List(Box<Shape>),
    /// A map from keys to values.
    Map(Box<Shape>, Box<Shape>),
    /// A `string_enum!` type with its wire values.
    Enum {
        /// The Rust name.
        name: &'static str,
        /// The wire values of its variants, without `Unknown`.
        values: &'static [&'static str],
    },
    /// A struct.
    Struct {
        /// The Rust name.
        name: &'static str,
        /// The struct's attributes, as written.
        attrs: &'static [&'static str],
        /// Its named fields.
        fields: fn() -> Vec<Field>,
    },
    /// A serde enum: unit variants have no shape, newtype variants their payload's.
    Union {
        /// The Rust name.
        name: &'static str,
        /// The enum's attributes, as written.
        attrs: &'static [&'static str],
        /// Its variants, as fields whose shape is `Unit` for a unit variant.
        variants: fn() -> Vec<Field>,
    },
}

/// A type whose declared shape the contract test can read.
pub trait Described {
    /// The type's shape.
    fn shape() -> Shape;
}

macro_rules! leaf {
    ($shape:ident: $($ty:ty),*) => {
        $(impl Described for $ty {
            fn shape() -> Shape {
                Shape::$shape
            }
        })*
    };
}
leaf!(Unit: ());
leaf!(String: String);
leaf!(Bool: bool);
leaf!(Integer: u8, u16, u32, u64, usize, i8, i16, i32, i64);
leaf!(Number: f32, f64);
leaf!(Json: Value);

impl Described for Bytes {
    fn shape() -> Shape {
        Shape::Native("Bytes")
    }
}
impl<T: Described> Described for Option<T> {
    fn shape() -> Shape {
        Shape::Option(Box::new(T::shape()))
    }
}
impl<T: Described> Described for Box<T> {
    fn shape() -> Shape {
        T::shape()
    }
}
impl<T: Described> Described for Vec<T> {
    fn shape() -> Shape {
        Shape::List(Box::new(T::shape()))
    }
}
impl<T: Described> Described for super::Patch<T> {
    fn shape() -> Shape {
        Shape::Patch(Box::new(T::shape()))
    }
}
impl<K: Described, V: Described, S> Described for HashMap<K, V, S> {
    fn shape() -> Shape {
        Shape::Map(Box::new(K::shape()), Box::new(V::shape()))
    }
}
impl<K: Described, V: Described> Described for BTreeMap<K, V> {
    fn shape() -> Shape {
        Shape::Map(Box::new(K::shape()), Box::new(V::shape()))
    }
}
impl Described for Map<String, Value> {
    fn shape() -> Shape {
        Shape::Map(Box::new(Shape::String), Box::new(Shape::Json))
    }
}
impl Described for super::Content {
    fn shape() -> Shape {
        Shape::Native("Content")
    }
}
impl Described for std::time::Duration {
    fn shape() -> Shape {
        Shape::Native("Duration")
    }
}
// Generated by the transform options generator, so described here: the CDN's transform schema
// (`transform-options.json`) types these options, not the API's.
impl Described for super::ImageTransformOptions {
    fn shape() -> Shape {
        Shape::Native("ImageTransformOptions")
    }
}