Add Data type and implement Deserialize for it

master
parent bd3582787b
commit 0406cf96c9
  1. 13
      Cargo.toml
  2. 12
      data_validation/Cargo.toml
  3. 144
      data_validation/src/data.rs
  4. 1
      data_validation/src/lib.rs
  5. 9
      data_validation_derive/Cargo.toml
  6. 0
      data_validation_derive/src/lib.rs
  7. 7
      src/lib.rs

@ -1,9 +1,6 @@
[package]
name = "data-validation"
version = "0.1.0"
authors = [BlackSponge <blacksponge@tuta.io>, Jackred <jackred@tuta.io>]
edition = "2018"
[workspace]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
members = [
"data_validation",
"data_validation_derive"
]

@ -0,0 +1,12 @@
[package]
name = "data_validation"
version = "0.1.0"
authors = ["BlackSponge <blacksponge@tuta.io>", "Jackred <jackred@tuta.io>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde = "1.0"
serde_json = "1.0"
data_validation_derive = { path = "../data_validation_derive"}

@ -0,0 +1,144 @@
use std::collections::HashMap;
use std::fmt;
use serde::de::{Deserialize, Visitor, MapAccess, SeqAccess};
#[derive(Debug, PartialEq)]
pub enum Number {
Signed(i64),
Unsigned(u64),
Float(f64),
}
#[derive(Debug, PartialEq)]
pub enum Data {
Null,
String(String),
Number(Number),
Bool(bool),
Array(Vec<Data>),
Object(HashMap<String, Data>),
}
impl<'de> Deserialize<'de> for Data {
fn deserialize<D>(deserializer: D) -> Result<Data, D::Error>
where
D: serde::Deserializer<'de>
{
struct DataVisitor;
impl<'de> Visitor<'de> for DataVisitor {
type Value = Data;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("json value")
}
fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E> {
Ok(Data::Number(Number::Signed(value)))
}
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> {
Ok(Data::Number(Number::Unsigned(value)))
}
fn visit_f64<E>(self, value: f64) -> Result<Self::Value, E> {
Ok(Data::Number(Number::Float(value)))
}
fn visit_bool<E>(self, value: bool) -> Result<Self::Value, E>
{
Ok(Data::Bool(value))
}
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: serde::de::Error
{
self.visit_string(String::from(value))
}
fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
{
Ok(Data::String(value))
}
fn visit_unit<E>(self) -> Result<Self::Value, E>
{
Ok(Data::Null)
}
fn visit_seq<S>(self, mut access: S) -> Result<Self::Value, S::Error>
where
S: SeqAccess<'de>
{
let mut vec = Vec::new();
while let Some(item) = access.next_element()? {
vec.push(item);
}
Ok(Data::Array(vec))
}
fn visit_map<M>(self, mut access: M) -> Result<Self::Value, M::Error>
where
M: MapAccess<'de>
{
let mut map = HashMap::with_capacity(access.size_hint().unwrap_or(0));
while let Some((key, value)) = access.next_entry()? {
map.insert(key, value);
}
Ok(Data::Object(map))
}
}
deserializer.deserialize_any(DataVisitor)
}
}
#[cfg(test)]
mod tests {
use super::{Data, Number};
use std::collections::HashMap;
use serde_json;
#[test]
fn data_deserialize_json() {
let raw = r#"{
"a": -1,
"b" : 1,
"c": 1.2,
"d": null,
"e": false,
"f": {
"a": true,
"b": 6e5
},
"g": ["a", true, 5.1e-1]
}"#;
let data_deserialized = serde_json::from_str::<Data>(raw).unwrap();
let mut extern_map = HashMap::<String, Data>::new();
let mut intern_map = HashMap::<String, Data>::new();
let vec = vec![Data::String("a".to_string()), Data::Bool(true), Data::Number(Number::Float(0.51))];
intern_map.insert("a".to_string(), Data::Bool(true));
intern_map.insert("b".to_string(), Data::Number(Number::Float(600000.0)));
extern_map.insert("a".to_string(), Data::Number(Number::Signed(-1)));
extern_map.insert("b".to_string(), Data::Number(Number::Unsigned(1)));
extern_map.insert("c".to_string(), Data::Number(Number::Float(1.2)));
extern_map.insert("d".to_string(), Data::Null);
extern_map.insert("e".to_string(), Data::Bool(false));
extern_map.insert("f".to_string(), Data::Object(intern_map));
extern_map.insert("g".to_string(), Data::Array(vec));
let data_expected = Data::Object(extern_map);
assert_eq!(data_deserialized, data_expected);
}
}

@ -0,0 +1,9 @@
[package]
name = "data_validation_derive"
version = "0.1.0"
authors = ["BlackSponge <blacksponge@tuta.io>", "Jackred <jackred@tuta.io>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

@ -1,7 +0,0 @@
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}
Loading…
Cancel
Save