s2json_core/shape.rs
1use crate::Map;
2use alloc::{string::String, vec::Vec};
3use serde::{Deserialize, Serialize};
4
5//? Shape
6
7// Shapes exist solely to deconstruct and rebuild objects.
8//
9// Shape limitations:
10// - all keys are strings.
11// - all values are either:
12// - - primitive types: strings, numbers (f32, f64, u64, i64), true, false, or null
13// - - sub types: an array of a shape or a nested object which is itself a shape
14// - - if the sub type is an array, ensure all elements are of the same type
15// The interfaces below help describe how shapes are built by the user.
16
17/// Primitive types that can be found in a shape
18#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq)]
19#[serde(rename_all = "lowercase")]
20pub enum PrimitiveShape {
21 /// String type utf8 encoded
22 String,
23 /// unsigned 64 bit integer
24 U64,
25 /// signed 64 bit integer
26 I64,
27 /// floating point number
28 F32,
29 /// double precision floating point number
30 F64,
31 /// boolean
32 Bool,
33 /// null
34 #[default]
35 Null,
36}
37
38/// Arrays may contain either a primitive or an object whose values are primitives
39#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
40#[serde(untagged)]
41pub enum PrimitiveShapeType {
42 /// Primitive type
43 Primitive(PrimitiveShape),
44 /// Nested shape that can only contain primitives
45 NestedPrimitive(ShapePrimitive),
46}
47
48/// Shape types that can be found in a shapes object.
49/// Either a primitive, an array containing any type, or a nested shape.
50/// If the type is an array, all elements must be the same type
51#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
52#[serde(untagged)]
53pub enum ShapeType {
54 /// Primitive type
55 Primitive(PrimitiveShape),
56 /// Nested shape that can only contain primitives
57 Array(Vec<PrimitiveShapeType>),
58 /// Nested shape
59 Nested(Shape),
60}
61
62/// The Primitive Shape Object
63pub type ShapePrimitive = Map<String, PrimitiveShape>;
64/// The Shape Object
65pub type Shape = Map<String, ShapeType>;