Skip to main content

Shape

Trait Shape 

Source
pub trait Shape: Sized {
    const CLASS: Class;
    const OPTIONAL: bool = false;
    const IS_OBJECT: bool = false;
    const CATCH_ALL: bool = false;
    const KEYS: &'static [&'static str] = _;

    // Required methods
    fn reference() -> ShapeDef;
    fn register(registry: &mut Registry);
    fn build(value: &Value, path: &mut Path) -> Result<Self, Issues>;

    // Provided methods
    fn catch_all_shape() -> Option<ShapeDef> { ... }
    fn definition() -> ShapeDef { ... }
    fn register_members(registry: &mut Registry) { ... }
    fn absent() -> Option<Self> { ... }
    fn definitions() -> Cow<'static, Definitions> { ... }
    fn build_rest(
        value: &Value,
        path: &mut Path,
        known: &[&str],
    ) -> Result<Self, Issues> { ... }
    fn from_json(value: &Value) -> Result<Self, Issues> { ... }
    fn accepts(value: &Value) -> bool { ... }
}
Expand description

A type with a shape. #[derive(Shape)] implements it from the type’s serde attributes and #[shape(...)].

Required Associated Constants§

Source

const CLASS: Class

The class of the shape, which decides at compile time which constraints a field of this type may carry.

Provided Associated Constants§

Source

const OPTIONAL: bool = false

Whether a field of this type may be absent from its object.

Source

const IS_OBJECT: bool = false

Whether the type is a struct, which is what a flattened field and a newtype variant of a tagged enum must be.

Source

const CATCH_ALL: bool = false

Whether the type collects the members of an object that no field names, so a flattened field of it types them.

Source

const KEYS: &'static [&'static str] = _

The keys of the object this type is, own and flattened, which a derive checks for duplicates at compile time.

Required Methods§

Source

fn reference() -> ShapeDef

How a field of this type is described: a named type gives a reference to its definition, a built-in gives its shape inline.

Source

fn register(registry: &mut Registry)

Adds this type and every named type it refers to to registry.

Source

fn build(value: &Value, path: &mut Path) -> Result<Self, Issues>

Converts a value that already matches this type’s shape. A mismatch means the shape and the conversion disagree, and is reported as an issue rather than a panic.

Provided Methods§

Source

fn catch_all_shape() -> Option<ShapeDef>

The shape of those members, for a type with CATCH_ALL.

Source

fn definition() -> ShapeDef

The complete shape of this type, which for a named type is what its definition holds.

Source

fn register_members(registry: &mut Registry)

Adds the named types this type’s fields refer to, without this type itself, for a type whose fields are merged into another object’s definition.

Source

fn absent() -> Option<Self>

The value of a field of this type that is absent from its object, for a type that allows that.

Source

fn definitions() -> Cow<'static, Definitions>

The definitions this type is described by.

Source

fn build_rest( value: &Value, path: &mut Path, known: &[&str], ) -> Result<Self, Issues>

Converts the members of an object that are not in known, for a type with CATCH_ALL.

Source

fn from_json(value: &Value) -> Result<Self, Issues>

Validates value against this type’s shape, collecting every issue with its path, then converts it. This is what deserialising a derived type does, so deserialising is validating.

Source

fn accepts(value: &Value) -> bool

Whether value matches this type’s shape.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl Shape for String

Source§

const CLASS: Class = Class::String

Source§

fn reference() -> ShapeDef

Source§

fn register(_: &mut Registry)

Source§

fn build(value: &Value, path: &mut Path) -> Result<Self, Issues>

Source§

impl Shape for bool

Source§

const CLASS: Class = Class::Bool

Source§

fn reference() -> ShapeDef

Source§

fn register(_: &mut Registry)

Source§

fn build(value: &Value, path: &mut Path) -> Result<Self, Issues>

Source§

impl Shape for f64

Source§

const CLASS: Class = Class::Number

Source§

fn reference() -> ShapeDef

Source§

fn register(_: &mut Registry)

Source§

fn build(value: &Value, path: &mut Path) -> Result<Self, Issues>

Source§

impl Shape for i32

Source§

const CLASS: Class = Class::Integer

Source§

fn reference() -> ShapeDef

Source§

fn register(_: &mut Registry)

Source§

fn build(value: &Value, path: &mut Path) -> Result<Self, Issues>

Source§

impl Shape for i64

Source§

const CLASS: Class = Class::Integer

Source§

fn reference() -> ShapeDef

Source§

fn register(_: &mut Registry)

Source§

fn build(value: &Value, path: &mut Path) -> Result<Self, Issues>

Source§

impl Shape for u8

Source§

const CLASS: Class = Class::Integer

Source§

fn reference() -> ShapeDef

Source§

fn register(_: &mut Registry)

Source§

fn build(value: &Value, path: &mut Path) -> Result<Self, Issues>

Source§

impl Shape for u16

Source§

const CLASS: Class = Class::Integer

Source§

fn reference() -> ShapeDef

Source§

fn register(_: &mut Registry)

Source§

fn build(value: &Value, path: &mut Path) -> Result<Self, Issues>

Source§

impl Shape for u32

Source§

const CLASS: Class = Class::Integer

Source§

fn reference() -> ShapeDef

Source§

fn register(_: &mut Registry)

Source§

fn build(value: &Value, path: &mut Path) -> Result<Self, Issues>

Source§

impl Shape for u64

Source§

const CLASS: Class = Class::Integer

Source§

fn reference() -> ShapeDef

Source§

fn register(_: &mut Registry)

Source§

fn build(value: &Value, path: &mut Path) -> Result<Self, Issues>

Source§

impl Shape for usize

Source§

const CLASS: Class = Class::Integer

Source§

fn reference() -> ShapeDef

Source§

fn register(_: &mut Registry)

Source§

fn build(value: &Value, path: &mut Path) -> Result<Self, Issues>

Source§

impl<T: Shape> Shape for Box<T>

Source§

const CLASS: Class = T::CLASS

Source§

const IS_OBJECT: bool = T::IS_OBJECT

Source§

const KEYS: &'static [&'static str] = T::KEYS

Source§

fn reference() -> ShapeDef

Source§

fn definition() -> ShapeDef

Source§

fn register(registry: &mut Registry)

Source§

fn register_members(registry: &mut Registry)

Source§

fn build(value: &Value, path: &mut Path) -> Result<Self, Issues>

Source§

impl<T: Shape> Shape for Option<T>

An absent field, and so an optional one. It is not an object type for a flattened field, whose fields would then be optional as a group, which a shape cannot say. A field that is present is described by T alone, so null is not an accepted spelling of absence.

Source§

const OPTIONAL: bool = true

Source§

const CLASS: Class = T::CLASS

Source§

fn reference() -> ShapeDef

Source§

fn register(registry: &mut Registry)

Source§

fn absent() -> Option<Self>

Source§

fn build(value: &Value, path: &mut Path) -> Result<Self, Issues>

Source§

impl<T: Shape> Shape for Vec<T>

Source§

const CLASS: Class = Class::Array

Source§

fn reference() -> ShapeDef

Source§

fn register(registry: &mut Registry)

Source§

fn build(value: &Value, path: &mut Path) -> Result<Self, Issues>

Implementors§

Source§

impl Shape for OpenObject

Source§

const CLASS: Class = Class::OpenObject

Source§

const CATCH_ALL: bool = true

Source§

impl Shape for Value

Any JSON value, kept in order.

Source§

const CLASS: Class = Class::Any

Source§

impl<T: Shape> Shape for Map<T>

Source§

const CLASS: Class = Class::Map

Source§

const CATCH_ALL: bool = true