|
@@ -1,5 +1,6 @@
|
|
|
use std::{collections::HashMap, fmt::Display, slice::{SliceIndex, Iter, IterMut}};
|
|
|
|
|
|
+#[cfg(feature = "shared")]
|
|
|
use crate::Shared;
|
|
|
|
|
|
#[cfg(not(feature = "serde"))]
|
|
@@ -520,8 +521,10 @@ impl TryInto<HashMap<String, Data>> for Data {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
// To cast Data into `Shared<Data>`
|
|
|
// (Because we're still in the crate, we can do this)
|
|
|
+#[cfg(feature = "shared")]
|
|
|
impl From<Data> for Shared<Data> {
|
|
|
fn from(v: Data) -> Self {
|
|
|
Shared::new(v)
|
|
@@ -546,7 +549,7 @@ impl Data {
|
|
|
Self::Map(HashMap::with_capacity(cap))
|
|
|
}
|
|
|
/// Is Data not the variant None
|
|
|
- ///
|
|
|
+ ///
|
|
|
/// This is useful for using Data similar to a Option
|
|
|
pub fn is_some(&self) -> bool {
|
|
|
!matches!(self, Self::None)
|
|
@@ -560,19 +563,19 @@ impl Data {
|
|
|
matches!(self, Self::String(_))
|
|
|
}
|
|
|
/// Is Data a signed/unsigned integer or float
|
|
|
- ///
|
|
|
+ ///
|
|
|
/// JSON Number's can hold both a whole number (i.e. signed/unsigned integers) and floats
|
|
|
pub fn is_number(&self) -> bool {
|
|
|
matches!(self, Self::I8(_) | Self::I16(_) | Self::I32(_) | Self::I64(_) | Self::I128(_) | Self::U8(_) | Self::U16(_) | Self::U32(_) | Self::U64(_) | Self::U128(_) | Self::F32(_) | Self::F64(_))
|
|
|
}
|
|
|
/// Is Data a signed integer (Regardless of bits)
|
|
|
- ///
|
|
|
+ ///
|
|
|
/// i.e. i8, i16, i32, i64, i128 or neither
|
|
|
pub fn is_int(&self) -> bool {
|
|
|
matches!(self, Self::I8(_) | Self::I16(_) | Self::I32(_) | Self::I64(_) | Self::I128(_))
|
|
|
}
|
|
|
/// Is Data a unsigned integer (Regardless of bits)
|
|
|
- ///
|
|
|
+ ///
|
|
|
/// i.e. u8, u16, u32, u64, or u128 or neither
|
|
|
pub fn is_uint(&self) -> bool {
|
|
|
matches!(self, Self::U8(_) | Self::U16(_) | Self::U32(_) | Self::U64(_) | Self::U128(_))
|
|
@@ -582,7 +585,7 @@ impl Data {
|
|
|
matches!(self, Self::F32(_) | Self::F64(_))
|
|
|
}
|
|
|
/// Does Data contain something that holds other Data
|
|
|
- ///
|
|
|
+ ///
|
|
|
/// `Vec<Data>` or `HashMap<String, Data>` are examples of "nested" Data
|
|
|
pub fn is_nested(&self) -> bool {
|
|
|
matches!(self, Self::Vec(_) | Self::Map(_))
|
|
@@ -596,7 +599,7 @@ impl Data {
|
|
|
matches!(self, Self::Map(_))
|
|
|
}
|
|
|
/// Push a value into the Data
|
|
|
- ///
|
|
|
+ ///
|
|
|
/// Must be a Vec or no-op
|
|
|
pub fn vec_push(&mut self, v: Data) {
|
|
|
if let Self::Vec(vec) = self {
|
|
@@ -604,7 +607,7 @@ impl Data {
|
|
|
}
|
|
|
}
|
|
|
/// Assigns/Reassigns a key the value into the Data
|
|
|
- ///
|
|
|
+ ///
|
|
|
/// Must be a Map or no-op
|
|
|
pub fn map_insert(&mut self, k: String, v: Data) {
|
|
|
if let Self::Map(hm) = self {
|
|
@@ -612,7 +615,7 @@ impl Data {
|
|
|
}
|
|
|
}
|
|
|
/// Obtains the length of String, Vec or Map
|
|
|
- ///
|
|
|
+ ///
|
|
|
/// All other variants of Data will return 0
|
|
|
pub fn len(&self) -> usize {
|
|
|
match self {
|
|
@@ -623,7 +626,7 @@ impl Data {
|
|
|
}
|
|
|
}
|
|
|
/// Obtains the capacity of Vec or Map
|
|
|
- ///
|
|
|
+ ///
|
|
|
/// All other variants of Data return 0
|
|
|
pub fn capacity(&self) -> usize {
|
|
|
match self {
|
|
@@ -633,7 +636,7 @@ impl Data {
|
|
|
}
|
|
|
}
|
|
|
/// Reserve {additional} in a Vec or Map
|
|
|
- ///
|
|
|
+ ///
|
|
|
/// This no-op's if the Data variants are not Vec or Map
|
|
|
pub fn reserve(&mut self, additional: usize) {
|
|
|
match self {
|
|
@@ -670,7 +673,7 @@ impl Data {
|
|
|
}
|
|
|
}
|
|
|
/// Removes the last item in the Vec, see [Vec::pop]
|
|
|
- ///
|
|
|
+ ///
|
|
|
/// This will return None also if the Data isn't a Vec
|
|
|
pub fn vec_pop(&mut self) -> Option<Data> {
|
|
|
match self {
|
|
@@ -738,7 +741,7 @@ impl Data {
|
|
|
_ => None,
|
|
|
}
|
|
|
}
|
|
|
- /// Gets like [Data::vec_get] except as mutable
|
|
|
+ /// Gets like [Data::vec_get] except as mutable
|
|
|
pub fn vec_get_mut<I>(&mut self, index: I) -> Option<&mut I::Output>
|
|
|
where
|
|
|
I: SliceIndex<[Data]>,
|
|
@@ -791,7 +794,7 @@ impl Data {
|
|
|
if let Self::Vec(v) = self { v.reverse() }
|
|
|
}
|
|
|
/// Attempts to form an iterator for Vec
|
|
|
- ///
|
|
|
+ ///
|
|
|
/// This can fail if Data isn't a Vec
|
|
|
pub fn vec_iter(&self) -> Option<Iter<'_, Data>> {
|
|
|
match self {
|
|
@@ -800,7 +803,7 @@ impl Data {
|
|
|
}
|
|
|
}
|
|
|
/// Attempts to form a mutable iterator for Vec
|
|
|
- ///
|
|
|
+ ///
|
|
|
/// This can fail if Data isn't a Vec
|
|
|
pub fn vec_iter_mut(&mut self) -> Option<IterMut<'_, Data>> {
|
|
|
match self {
|
|
@@ -809,7 +812,7 @@ impl Data {
|
|
|
}
|
|
|
}
|
|
|
/// This will determine the size of signed or unsigned integers and floats
|
|
|
- ///
|
|
|
+ ///
|
|
|
/// i.e. i32 is 32, u64 is 64, f32 is 32, u128 is 128 (Meanwhile, anything else is 0, String is 0, Bool is 0, etc.)
|
|
|
pub fn bit_variant(&self) -> u8 {
|
|
|
match self {
|