s2json_core/
value.rs

1use crate::Map;
2use alloc::{string::String, vec::Vec};
3use serde::{Deserialize, Serialize};
4
5/// Primitive types supported by Properties
6#[derive(Serialize, Deserialize, Debug, Clone, Default)]
7#[serde(untagged)]
8pub enum PrimitiveValue {
9    /// String type utf8 encoded
10    String(String),
11    /// unsigned 64 bit integer
12    U64(u64),
13    /// signed 64 bit integer
14    I64(i64),
15    /// floating point number
16    F32(f32),
17    /// double precision floating point number
18    F64(f64),
19    /// boolean
20    Bool(bool),
21    /// null
22    #[default]
23    Null,
24}
25
26/// Arrays may contain either a primitive or an object whose values are primitives
27#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
28#[serde(untagged)]
29pub enum ValuePrimitiveType {
30    /// Primitive type
31    Primitive(PrimitiveValue),
32    /// Nested shape that can only contain primitives
33    NestedPrimitive(ValuePrimitive),
34}
35
36/// Supports primitive types `string`, `number`, `boolean`, `null`
37/// May be an array of those primitive types, or an object whose values are only primitives
38/// Object keys are always strings, values can be any basic type, an array, or a nested object.
39/// Array values must all be the same type.
40#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
41#[serde(untagged)]
42pub enum ValueType {
43    /// A primitive value
44    Primitive(PrimitiveValue),
45    /// An array of values
46    Array(Vec<ValuePrimitiveType>),
47    /// A nested object
48    Nested(Value),
49}
50
51/// Shape of a ValuePrimitiveType Nested object
52pub type ValuePrimitive = Map<String, PrimitiveValue>;
53/// Shape design
54pub type Value = Map<String, ValueType>;
55/// Shape of a features properties object
56pub type Properties = Value;
57/// Shape of a feature's M-Values object
58pub type MValue = Value;
59
60/// Ensure M implements MValueCompatible
61pub trait MValueCompatible:
62    for<'a> From<&'a MValue> + From<MValue> + Into<MValue> + Clone + Default
63{
64}
65impl From<&MValue> for MValue {
66    fn from(mvalue: &MValue) -> MValue {
67        mvalue.clone()
68    }
69}
70impl MValueCompatible for MValue {}
71
72/// LineString Properties Shape
73pub type LineStringMValues<M = MValue> = Vec<M>;
74/// MultiLineString MValues Shape
75pub type MultiLineStringMValues<M = MValue> = Vec<LineStringMValues<M>>;
76/// Polygon MValues Shape
77pub type PolygonMValues<M = MValue> = Vec<LineStringMValues<M>>;
78/// MultiPolygon MValues Shape
79pub type MultiPolygonMValues<M = MValue> = Vec<PolygonMValues<M>>;
80
81/// All possible M-Value shapes
82#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
83#[serde(untagged)]
84pub enum MValues<M: Clone = MValue> {
85    /// Single M-Value
86    MValue(M),
87    /// LineString M-Value
88    LineStringMValues(LineStringMValues<M>),
89    /// MultiLineString M-Value
90    MultiLineStringMValues(MultiLineStringMValues<M>),
91    /// Polygon M-Value
92    PolygonMValues(PolygonMValues<M>),
93    /// MultiPolygon M-Value
94    MultiPolygonMValues(MultiPolygonMValues<M>),
95}
96
97/// All possible JSON shapes
98#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
99#[serde(untagged)]
100pub enum JSONValue {
101    /// Represents a JSON primitive
102    Primitive(PrimitiveValue),
103    /// Represents a JSON array.
104    Array(Vec<JSONValue>),
105    /// Represents a JSON object.
106    Object(JSONProperties),
107}
108
109/// Shape of an un-restricted features properties object
110pub type JSONProperties = Map<String, JSONValue>;
111/// Ensure M implements MValueCompatible
112pub trait JSONPropertiesCompatible:
113    for<'a> From<&'a JSONProperties> + From<JSONProperties> + Into<JSONProperties> + Clone + Default
114{
115}
116impl From<&JSONProperties> for JSONProperties {
117    fn from(json: &JSONProperties) -> JSONProperties {
118        json.clone()
119    }
120}
121impl JSONPropertiesCompatible for JSONProperties {}
122
123/// Shape of the restricted Mapbox properties object
124pub type MapboxProperties = ValuePrimitive;