Bladeren bron

Implemented cast_into feature for Data

This covers TryInto<bool> for Data with the cast_into conversion.

- Data::String will check not is_empty
- signed/unsigned integers and floats will check not equal 0 or 0.0
- Data::Bool will be passed as it is
- TryInto<bool> will only fail for Data::None, Data::Vec & Data::Map
Steve Thielemann 2 maanden geleden
bovenliggende
commit
9c5cd1a5d5
1 gewijzigde bestanden met toevoegingen van 33 en 8 verwijderingen
  1. 33 8
      src/data.rs

+ 33 - 8
src/data.rs

@@ -20,7 +20,7 @@ pub enum Data {
     F64(f64),
     Bool(bool),
     Vec(Vec<Data>),
-    HashMap(HashMap<String, Data>),
+    Map(HashMap<String, Data>),
 }
 
 impl Default for Data {
@@ -58,7 +58,7 @@ impl Display for Data {
                 out.push(']');
                 f.write_str(out.as_str())
             },
-            Self::HashMap(hm) => {
+            Self::Map(hm) => {
                 let mut out: String = "{".to_string();
                 for (k, v) in hm.iter() {
                     if out != "{" {
@@ -165,7 +165,7 @@ impl From<Vec<Data>> for Data {
 
 impl From<HashMap<String, Data>> for Data {
     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 {
     type 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
 impl TryInto<Vec<Data>> for Data {
     type Error = ();
@@ -467,16 +491,17 @@ impl TryInto<HashMap<String, Data>> for Data {
     type Error = ();
     fn try_into(self) -> Result<HashMap<String, Data>, Self::Error> {
         match self {
-            Self::HashMap(v) => Ok(v),
+            Self::Map(v) => Ok(v),
             _ => Err(()),
         }
     }
 }
 
 // To cast Data into Shared<Data>
+// (Because we're still in the crate, we can do this)
 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
     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
     ///