shape

Enum ShapeCase

Source
pub enum ShapeCase {
    Bool(Option<bool>),
    String(Option<String>),
    Int(Option<i64>),
    Float,
    Null,
    Array {
        prefix: Vec<Shape>,
        tail: Shape,
    },
    Object {
        fields: IndexMap<String, Shape>,
        rest: Shape,
    },
    One(IndexSet<Shape>),
    All(IndexSet<Shape>),
    Name(String, Vec<NamedShapePathKey>),
    None,
    Error {
        message: String,
        range: OffsetRange,
        partial: Option<Shape>,
    },
}
Expand description

The ShapeCase enum attempts to capture all the common shapes of JSON values, not just the JSON types themselves, but also patterns of usage (such as using JSON arrays as either static tuples or dynamic lists).

A ShapeCase enum variant like ShapeCase::One may temporarily represent a structure that has not been fully simplified, but simplication is required to turn the ShapeCase into a Shape, using the ShapeCase::simplify(&self) -> Shape method.

Variants§

§

Bool(Option<bool>)

The Bool, String, and Int variants can either represent a general shape using None for the Option, or a specific shape using Some. For example, ShapeCase::Bool(None) is a shape satisfied by either true or false, like the Rust bool type, while ShapeCase::Bool(Some(true)) is a shape satisfied only by true (and likewise for false).

§

String(Option<String>)

Similar to ShapeCase::Bool, ShapeCase::String represents a shape matching either any string (in the None case) or some specific string (in the Some case).

§

Int(Option<i64>)

Similar to ShapeCase::Bool and ShapeCase::String, ShapeCase::Int can represent either any integer or some specific integer.

§

Float

ShapeCase::Float is a shape that captures the set of all floating point numbers. We do not allow singleton floating point value shapes, since floating point equality is unreliable.

§

Null

ShapeCase::Null is a singleton shape whose only possible value is null. Note that ShapeCase::Null is different from ShapeCase::None, which represents the absence of a value.

§

Array

ShapeCase::Array represents a prefix of statically known shapes (like a tuple type), followed by an optional tail shape for all other (dynamic) elements. When only the prefix elements are defined, the tail shape is ShapeCase::None. ShapeCase::Array(vec![], ShapeCase::None) is the shape of an empty array.

Fields

§prefix: Vec<Shape>
§tail: Shape
§

Object

ShapeCase::Object is a map of statically known field names to field shapes, together with an optional type for all other string keys. When dynamic string indexing is disabled, the rest shape will be ShapeCase::None. Note that accessing the dynamic map always returns ShapeCase::One([rest_shape, ShapeCase::None]), to reflect the uncertainty of the shape of dynamic keys not present in the static fields.

Fields

§fields: IndexMap<String, Shape>
§rest: Shape
§

One(IndexSet<Shape>)

A union of shapes, satisfied by values that satisfy any of the shapes in the set.

§

All(IndexSet<Shape>)

An intersection of shapes, satisfied by values that satisfy all of the shapes in the set. When applied to multiple ShapeCase::Object shapes, ShapeCase::All represents merging the fields of the objects, as reflected by the simplification logic.

§

Name(String, Vec<NamedShapePathKey>)

ShapeCase::Name refers to a shape declared with the given name, possibly in the future. When shape processing needs to refer to the shape of some subproperty of a named shape, it uses the Vec<NamedShapePathKey> subpath to represent the nested shape. When the named shape is eventually declared, the subpath can be used to resolve the actual shape of the nested property path. As of now, the name String is expected to be either an identifier or a variable name like $root or $this or $args, allowing ShapeCase::Name to represent types of subproperties of variables as well as named types from some schema.

§

None

Represents the absence of a value, or the shape of a property not present in an object. Used by the rest parameters of both ShapeCase::Array and ShapeCase::Object to indicate no additional dynamic elements are allowed, and with ShapeCase::One to represent optionality of values, e.g. One<Bool, None>.

§

Error

Represents a local failure of shape processing.

Fields

§message: String
§partial: Option<Shape>

This partial shape can be another ShapeCase::Error shape, which allows for chaining of multiple errors.

Implementations§

Source§

impl ShapeCase

Source

pub fn is_none(&self) -> bool

Source

pub fn is_null(&self) -> bool

Source

pub fn error(message: &str) -> Self

Source

pub fn error_with_range(message: &str, range: OffsetRange) -> Self

Source

pub fn error_with_partial(message: &str, partial: Shape) -> Self

Source

pub fn error_with_range_and_partial( message: &str, range: OffsetRange, partial: Shape, ) -> Self

Source§

impl ShapeCase

Source

pub fn pretty_print(&self) -> String

Source§

impl ShapeCase

Source

pub fn from_json(json: &JSON) -> Self

Source

pub fn from_json_bytes(json: &JSONBytes) -> Self

Source§

impl ShapeCase

Source

pub fn compute_hash(&self) -> u64

Source§

impl ShapeCase

Source

pub fn simplify(self) -> Shape

Returns a Shape wrapper around a simplified ShapeCase. This function is a primary bottleneck for constructing a Shape, and helps guarantee all Shape values have been reliably simplified and hashed.

Note that this method takes ownership of self in order to efficiently return case unmodified in already-simplified cases.

Trait Implementations§

Source§

impl Clone for ShapeCase

Source§

fn clone(&self) -> ShapeCase

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ShapeCase

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for ShapeCase

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&str> for ShapeCase

Source§

fn from(value: &str) -> Self

Converts to this type from the input type.
Source§

impl From<String> for ShapeCase

Source§

fn from(value: String) -> Self

Converts to this type from the input type.
Source§

impl From<bool> for ShapeCase

Source§

fn from(value: bool) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for ShapeCase

Source§

fn from(value: i64) -> Self

Converts to this type from the input type.
Source§

impl Hash for ShapeCase

Because ShapeCase uses IndexMap and IndexSet for its Object, One, and All variants, the default derived Hash trait implementation does not work. Instead, we hash those cases manually, using a commutative operation to combine the order-insensitive hashes of the elements.

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for ShapeCase

Source§

fn eq(&self, other: &ShapeCase) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for ShapeCase

Source§

impl StructuralPartialEq for ShapeCase

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.