|
@@ -20,7 +20,7 @@ pub enum Data {
|
|
F64(f64),
|
|
F64(f64),
|
|
Bool(bool),
|
|
Bool(bool),
|
|
Vec(Vec<Data>),
|
|
Vec(Vec<Data>),
|
|
- HashMap(HashMap<String, Data>),
|
|
|
|
|
|
+ Map(HashMap<String, Data>),
|
|
}
|
|
}
|
|
|
|
|
|
impl Default for Data {
|
|
impl Default for Data {
|
|
@@ -58,7 +58,7 @@ impl Display for Data {
|
|
out.push(']');
|
|
out.push(']');
|
|
f.write_str(out.as_str())
|
|
f.write_str(out.as_str())
|
|
},
|
|
},
|
|
- Self::HashMap(hm) => {
|
|
|
|
|
|
+ Self::Map(hm) => {
|
|
let mut out: String = "{".to_string();
|
|
let mut out: String = "{".to_string();
|
|
for (k, v) in hm.iter() {
|
|
for (k, v) in hm.iter() {
|
|
if out != "{" {
|
|
if out != "{" {
|
|
@@ -165,7 +165,7 @@ impl From<Vec<Data>> for Data {
|
|
|
|
|
|
impl From<HashMap<String, Data>> for Data {
|
|
impl From<HashMap<String, Data>> for Data {
|
|
fn from(value: HashMap<String, Data>) -> Self {
|
|
fn from(value: HashMap<String, Data>) -> Self {
|
|
- Self::HashMap(value)
|
|
|
|
|
|
+ Self::Map(value)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -440,7 +440,7 @@ impl TryInto<f64> for Data {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-// cast_into doesn't make sense here (Todo: Decide if we do want to cast integers based on not equal to zero)
|
|
|
|
|
|
+#[cfg(not(feature = "cast_into"))]
|
|
impl TryInto<bool> for Data {
|
|
impl TryInto<bool> for Data {
|
|
type Error = ();
|
|
type Error = ();
|
|
fn try_into(self) -> Result<bool, Self::Error> {
|
|
fn try_into(self) -> Result<bool, Self::Error> {
|
|
@@ -451,6 +451,30 @@ impl TryInto<bool> for Data {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#[cfg(feature = "cast_into")]
|
|
|
|
+impl TryInto<bool> for Data {
|
|
|
|
+ type Error = ();
|
|
|
|
+ fn try_into(self) -> Result<bool, Self::Error> {
|
|
|
|
+ match self {
|
|
|
|
+ Self::String(s) => Ok(s.is_empty()),
|
|
|
|
+ Self::I8(v) => Ok(v != 0),
|
|
|
|
+ Self::I16(v) => Ok(v != 0),
|
|
|
|
+ Self::I32(v) => Ok(v != 0),
|
|
|
|
+ Self::I64(v) => Ok(v != 0),
|
|
|
|
+ Self::I128(v) => Ok(v != 0),
|
|
|
|
+ Self::U8(v) => Ok(v != 0),
|
|
|
|
+ Self::U16(v) => Ok(v != 0),
|
|
|
|
+ Self::U32(v) => Ok(v != 0),
|
|
|
|
+ Self::U64(v) => Ok(v != 0),
|
|
|
|
+ Self::U128(v) => Ok(v != 0),
|
|
|
|
+ Self::F32(v) => Ok(v != 0.0),
|
|
|
|
+ Self::F64(v) => Ok(v != 0.0),
|
|
|
|
+ Self::Bool(b) => Ok(b),
|
|
|
|
+ _ => Err(()),
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
// cast_into doesn't make sense here
|
|
// cast_into doesn't make sense here
|
|
impl TryInto<Vec<Data>> for Data {
|
|
impl TryInto<Vec<Data>> for Data {
|
|
type Error = ();
|
|
type Error = ();
|
|
@@ -467,16 +491,17 @@ impl TryInto<HashMap<String, Data>> for Data {
|
|
type Error = ();
|
|
type Error = ();
|
|
fn try_into(self) -> Result<HashMap<String, Data>, Self::Error> {
|
|
fn try_into(self) -> Result<HashMap<String, Data>, Self::Error> {
|
|
match self {
|
|
match self {
|
|
- Self::HashMap(v) => Ok(v),
|
|
|
|
|
|
+ Self::Map(v) => Ok(v),
|
|
_ => Err(()),
|
|
_ => Err(()),
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// To cast Data into Shared<Data>
|
|
// To cast Data into Shared<Data>
|
|
|
|
+// (Because we're still in the crate, we can do this)
|
|
impl From<Data> for Shared<Data> {
|
|
impl From<Data> for Shared<Data> {
|
|
- fn from(val: Data) -> Self {
|
|
|
|
- Shared::new(val)
|
|
|
|
|
|
+ fn from(v: Data) -> Self {
|
|
|
|
+ Shared::new(v)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -521,7 +546,7 @@ impl Data {
|
|
///
|
|
///
|
|
/// Vec<Data> or HashMap<String, Data> are examples of "nested" Data
|
|
/// Vec<Data> or HashMap<String, Data> are examples of "nested" Data
|
|
pub fn is_nested(&self) -> bool {
|
|
pub fn is_nested(&self) -> bool {
|
|
- matches!(self, Self::Vec(_) | Self::HashMap(_))
|
|
|
|
|
|
+ matches!(self, Self::Vec(_) | Self::Map(_))
|
|
}
|
|
}
|
|
/// This will determine the size of signed or unsigned integers and floats
|
|
/// This will determine the size of signed or unsigned integers and floats
|
|
///
|
|
///
|