crates/edge/json/src/value.rs 3.9 KiB 149 lines Raw
1 //! The JSON value that the whole tree shares.2 //!3 //! Objects are `BTreeMap`s, so a document is a function of its content and not4 //! of the order a peer happened to write the keys in. Two equal documents5 //! serialise to the same bytes, which is what the web layer needs when a reply6 //! is compared or cached.7 8 use std::collections::BTreeMap;9 10 /// One JSON value.11 #[derive(Clone, PartialEq, Debug)]12 pub enum Value {13     /// `null`.14     Null,15     /// `true` or `false`.16     Bool(bool),17     /// A number that fits `i64`. It always writes without a decimal point.18     Int(i64),19     /// A number written with a fraction or an exponent, or one too large for20     /// `i64`. A whole value in here still writes as an integer, so it reads21     /// back as [`Value::Int`].22     Float(f64),23     /// A string. It holds text, never a lone surrogate.24     Str(String),25     /// `[ ... ]`.26     Array(Vec<Value>),27     /// `{ ... }`. Keys are unique and sorted.28     Object(BTreeMap<String, Value>),29 }30 31 impl Value {32     /// A string value.33     pub fn str(s: impl Into<String>) -> Value {34         Value::Str(s.into())35     }36 37     /// An integer value.38     pub const fn int(n: i64) -> Value {39         Value::Int(n)40     }41 42     /// A float value.43     pub const fn float(x: f64) -> Value {44         Value::Float(x)45     }46 47     /// A bool value.48     pub const fn bool(b: bool) -> Value {49         Value::Bool(b)50     }51 52     /// An object under construction. `Object::build` finishes it.53     pub fn object() -> Object {54         Object(BTreeMap::new())55     }56 57     /// The text of a string value.58     pub fn as_str(&self) -> Option<&str> {59         match self {60             Value::Str(s) => Some(s),61             _ => None,62         }63     }64 65     /// The value of an integer. A float is never read as an integer, so a66     /// whole float stays a float.67     pub fn as_i64(&self) -> Option<i64> {68         match self {69             Value::Int(n) => Some(*n),70             _ => None,71         }72     }73 74     /// The value of a non-negative integer.75     pub fn as_u64(&self) -> Option<u64> {76         match self {77             Value::Int(n) => u64::try_from(*n).ok(),78             _ => None,79         }80     }81 82     /// The value as f64. An integer widens, and above 2^53 the widening is not83     /// exact.84     pub fn as_f64(&self) -> Option<f64> {85         match self {86             Value::Int(n) => Some(*n as f64),87             Value::Float(x) => Some(*x),88             _ => None,89         }90     }91 92     /// The value of a bool.93     pub fn as_bool(&self) -> Option<bool> {94         match self {95             Value::Bool(b) => Some(*b),96             _ => None,97         }98     }99 100     /// The items of an array.101     pub fn as_array(&self) -> Option<&[Value]> {102         match self {103             Value::Array(items) => Some(items),104             _ => None,105         }106     }107 108     /// The entries of an object, in sorted key order.109     pub fn as_object(&self) -> Option<&BTreeMap<String, Value>> {110         match self {111             Value::Object(map) => Some(map),112             _ => None,113         }114     }115 116     /// The value under a key, when this is an object.117     pub fn get(&self, key: &str) -> Option<&Value> {118         self.as_object()?.get(key)119     }120 }121 122 /// An object being built, so that a document reads as one expression.123 #[derive(Clone, PartialEq, Debug, Default)]124 pub struct Object(BTreeMap<String, Value>);125 126 impl Object {127     /// An empty object.128     pub fn new() -> Object {129         Object(BTreeMap::new())130     }131 132     /// Sets a key and hands the builder back, so calls chain. A key set twice133     /// is replaced; the parser is the place that refuses a duplicate.134     pub fn put(mut self, key: impl Into<String>, value: Value) -> Object {135         self.0.insert(key.into(), value);136         self137     }138 139     /// The finished value. Keys are sorted because the map is a `BTreeMap`.140     pub fn build(self) -> Value {141         Value::Object(self.0)142     }143 }144 145 impl From<Object> for Value {146     fn from(object: Object) -> Value {147         object.build()148     }149 }