From 52f9ecfc4dac65d305fd920e8c7f748889a0804f Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Mon, 12 Aug 2019 23:24:48 +0200 Subject: Move api into its own crate --- serde_dhall/src/serde.rs | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 serde_dhall/src/serde.rs (limited to 'serde_dhall/src/serde.rs') diff --git a/serde_dhall/src/serde.rs b/serde_dhall/src/serde.rs new file mode 100644 index 0000000..3dad2d8 --- /dev/null +++ b/serde_dhall/src/serde.rs @@ -0,0 +1,70 @@ +use crate::de::{Deserialize, Value}; +use dhall::error::{Error, Result}; +use dhall_syntax::{ExprF, SubExpr, X}; +use std::borrow::Cow; + +impl<'a, T> Deserialize for T +where + T: serde::Deserialize<'a>, +{ + fn from_dhall(v: &Value) -> Result { + T::deserialize(Deserializer(Cow::Owned(v.to_expr()))) + } +} + +struct Deserializer<'a>(Cow<'a, SubExpr>); + +// impl serde::de::Error for Error { +// fn custom(msg: T) -> Self +// where +// T: std::fmt::Display, +// { +// Error::Deserialize(msg.to_string()) +// } +// } + +impl<'de: 'a, 'a> serde::de::IntoDeserializer<'de, Error> for Deserializer<'a> { + type Deserializer = Deserializer<'a>; + fn into_deserializer(self) -> Self::Deserializer { + self + } +} + +impl<'de: 'a, 'a> serde::Deserializer<'de> for Deserializer<'a> { + type Error = Error; + fn deserialize_any(self, visitor: V) -> Result + where + V: serde::de::Visitor<'de>, + { + use std::convert::TryInto; + use ExprF::*; + match self.0.as_ref().as_ref() { + NaturalLit(n) => match (*n).try_into() { + Ok(n64) => visitor.visit_u64(n64), + Err(_) => match (*n).try_into() { + Ok(n32) => visitor.visit_u32(n32), + Err(_) => unimplemented!(), + }, + }, + IntegerLit(n) => match (*n).try_into() { + Ok(n64) => visitor.visit_i64(n64), + Err(_) => match (*n).try_into() { + Ok(n32) => visitor.visit_i32(n32), + Err(_) => unimplemented!(), + }, + }, + RecordLit(m) => visitor.visit_map( + serde::de::value::MapDeserializer::new(m.iter().map( + |(k, v)| (k.as_ref(), Deserializer(Cow::Borrowed(v))), + )), + ), + _ => unimplemented!(), + } + } + + serde::forward_to_deserialize_any! { + bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string + bytes byte_buf option unit unit_struct newtype_struct seq tuple + tuple_struct map struct enum identifier ignored_any + } +} -- cgit v1.2.3 From 74bb40e88c71b80ab785f07fd19da22404ab6e95 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 13 Aug 2019 15:11:31 +0200 Subject: Add new error type for serde_dhall --- serde_dhall/src/serde.rs | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) (limited to 'serde_dhall/src/serde.rs') diff --git a/serde_dhall/src/serde.rs b/serde_dhall/src/serde.rs index 3dad2d8..1f639e6 100644 --- a/serde_dhall/src/serde.rs +++ b/serde_dhall/src/serde.rs @@ -1,5 +1,4 @@ -use crate::de::{Deserialize, Value}; -use dhall::error::{Error, Result}; +use crate::de::{Deserialize, Error, Result, Value}; use dhall_syntax::{ExprF, SubExpr, X}; use std::borrow::Cow; @@ -14,15 +13,6 @@ where struct Deserializer<'a>(Cow<'a, SubExpr>); -// impl serde::de::Error for Error { -// fn custom(msg: T) -> Self -// where -// T: std::fmt::Display, -// { -// Error::Deserialize(msg.to_string()) -// } -// } - impl<'de: 'a, 'a> serde::de::IntoDeserializer<'de, Error> for Deserializer<'a> { type Deserializer = Deserializer<'a>; fn into_deserializer(self) -> Self::Deserializer { -- cgit v1.2.3 From bf5d33f3ba991afe398d58fb4fed38ec72d6f4c7 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 13 Aug 2019 16:31:41 +0200 Subject: Rework API to resemble that of serde_json --- serde_dhall/src/serde.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'serde_dhall/src/serde.rs') diff --git a/serde_dhall/src/serde.rs b/serde_dhall/src/serde.rs index 1f639e6..10d5a17 100644 --- a/serde_dhall/src/serde.rs +++ b/serde_dhall/src/serde.rs @@ -1,4 +1,5 @@ -use crate::de::{Deserialize, Error, Result, Value}; +use crate::de::{Deserialize, Error, Result}; +use crate::Value; use dhall_syntax::{ExprF, SubExpr, X}; use std::borrow::Cow; -- cgit v1.2.3 From 5895c3aa6552f75d7e5202be561f9734fe8945e7 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 13 Aug 2019 19:31:23 +0200 Subject: No need to track the absence of `Span`s at the type level --- serde_dhall/src/serde.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'serde_dhall/src/serde.rs') diff --git a/serde_dhall/src/serde.rs b/serde_dhall/src/serde.rs index 10d5a17..d891127 100644 --- a/serde_dhall/src/serde.rs +++ b/serde_dhall/src/serde.rs @@ -1,7 +1,10 @@ +use std::borrow::Cow; + +use dhall::phase::NormalizedSubExpr; +use dhall_syntax::ExprF; + use crate::de::{Deserialize, Error, Result}; use crate::Value; -use dhall_syntax::{ExprF, SubExpr, X}; -use std::borrow::Cow; impl<'a, T> Deserialize for T where @@ -12,7 +15,7 @@ where } } -struct Deserializer<'a>(Cow<'a, SubExpr>); +struct Deserializer<'a>(Cow<'a, NormalizedSubExpr>); impl<'de: 'a, 'a> serde::de::IntoDeserializer<'de, Error> for Deserializer<'a> { type Deserializer = Deserializer<'a>; -- cgit v1.2.3 From a981afc465f4279a7a4d6ce3ac5844e04846613b Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 27 Aug 2019 22:58:20 +0200 Subject: clippy --- serde_dhall/src/serde.rs | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) (limited to 'serde_dhall/src/serde.rs') diff --git a/serde_dhall/src/serde.rs b/serde_dhall/src/serde.rs index d891127..94b7e6c 100644 --- a/serde_dhall/src/serde.rs +++ b/serde_dhall/src/serde.rs @@ -33,20 +33,24 @@ impl<'de: 'a, 'a> serde::Deserializer<'de> for Deserializer<'a> { use std::convert::TryInto; use ExprF::*; match self.0.as_ref().as_ref() { - NaturalLit(n) => match (*n).try_into() { - Ok(n64) => visitor.visit_u64(n64), - Err(_) => match (*n).try_into() { - Ok(n32) => visitor.visit_u32(n32), - Err(_) => unimplemented!(), - }, - }, - IntegerLit(n) => match (*n).try_into() { - Ok(n64) => visitor.visit_i64(n64), - Err(_) => match (*n).try_into() { - Ok(n32) => visitor.visit_i32(n32), - Err(_) => unimplemented!(), - }, - }, + NaturalLit(n) => { + if let Ok(n64) = (*n).try_into() { + visitor.visit_u64(n64) + } else if let Ok(n32) = (*n).try_into() { + visitor.visit_u32(n32) + } else { + unimplemented!() + } + } + IntegerLit(n) => { + if let Ok(n64) = (*n).try_into() { + visitor.visit_i64(n64) + } else if let Ok(n32) = (*n).try_into() { + visitor.visit_i32(n32) + } else { + unimplemented!() + } + } RecordLit(m) => visitor.visit_map( serde::de::value::MapDeserializer::new(m.iter().map( |(k, v)| (k.as_ref(), Deserializer(Cow::Borrowed(v))), -- cgit v1.2.3 From a7363042a16364a6dafdd545f4069dcf04a4197e Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 27 Aug 2019 23:18:13 +0200 Subject: Rename SubExpr to Expr, and Expr to RawExpr For clarity, and consistency with Value --- serde_dhall/src/serde.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'serde_dhall/src/serde.rs') diff --git a/serde_dhall/src/serde.rs b/serde_dhall/src/serde.rs index 94b7e6c..26708c1 100644 --- a/serde_dhall/src/serde.rs +++ b/serde_dhall/src/serde.rs @@ -1,6 +1,6 @@ use std::borrow::Cow; -use dhall::phase::NormalizedSubExpr; +use dhall::phase::NormalizedExpr; use dhall_syntax::ExprF; use crate::de::{Deserialize, Error, Result}; @@ -15,7 +15,7 @@ where } } -struct Deserializer<'a>(Cow<'a, NormalizedSubExpr>); +struct Deserializer<'a>(Cow<'a, NormalizedExpr>); impl<'de: 'a, 'a> serde::de::IntoDeserializer<'de, Error> for Deserializer<'a> { type Deserializer = Deserializer<'a>; -- cgit v1.2.3