From e070270c3f1f10d46281ed7751ff95e15092e7f4 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 10 May 2020 22:09:43 +0100 Subject: Implement serialization --- serde_dhall/tests/de.rs | 59 +----------------- serde_dhall/tests/serde.rs | 145 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+), 58 deletions(-) create mode 100644 serde_dhall/tests/serde.rs (limited to 'serde_dhall/tests') diff --git a/serde_dhall/tests/de.rs b/serde_dhall/tests/de.rs index 3abaad2..0b43de2 100644 --- a/serde_dhall/tests/de.rs +++ b/serde_dhall/tests/de.rs @@ -1,62 +1,5 @@ use serde::Deserialize; -use serde_dhall::{from_str, FromDhall, NumKind, SimpleValue, StaticType}; - -#[test] -fn test_de_typed() { - fn parse(s: &str) -> T { - from_str(s).static_type_annotation().parse().unwrap() - } - - assert_eq!(parse::("True"), true); - - assert_eq!(parse::("1"), 1); - assert_eq!(parse::("1"), 1); - assert_eq!(parse::("1"), 1); - - assert_eq!(parse::("+1"), 1); - assert_eq!(parse::("+1"), 1); - assert_eq!(parse::("+1"), 1); - - assert_eq!(parse::("1.0"), 1.0); - assert_eq!(parse::("1.0"), 1.0); - - assert_eq!(parse::(r#""foo""#), "foo".to_owned()); - assert_eq!(parse::>("[] : List Natural"), >::new()); - assert_eq!(parse::>("[1, 2]"), vec![1, 2]); - assert_eq!(parse::>("None Natural"), None); - assert_eq!(parse::>("Some 1"), Some(1)); - - assert_eq!( - parse::<(u64, String)>(r#"{ _1 = 1, _2 = "foo" }"#), - (1, "foo".to_owned()) - ); - - #[derive(Debug, PartialEq, Eq, Deserialize, StaticType)] - struct Foo { - x: u64, - y: i64, - } - assert_eq!(parse::("{ x = 1, y = -2 }"), Foo { x: 1, y: -2 }); - - #[derive(Debug, PartialEq, Eq, Deserialize, StaticType)] - enum Bar { - X(u64), - Y(i64), - } - assert_eq!(parse::("< X: Natural | Y: Integer >.X 1"), Bar::X(1)); - - #[derive(Debug, PartialEq, Eq, Deserialize, StaticType)] - enum Baz { - X, - Y(i64), - } - assert_eq!(parse::("< X | Y: Integer >.X"), Baz::X); - - assert!(from_str("< X | Y: Integer >.Y") - .static_type_annotation() - .parse::() - .is_err()); -} +use serde_dhall::{from_str, FromDhall, NumKind, SimpleValue}; #[test] fn test_de_untyped() { diff --git a/serde_dhall/tests/serde.rs b/serde_dhall/tests/serde.rs new file mode 100644 index 0000000..39f2f79 --- /dev/null +++ b/serde_dhall/tests/serde.rs @@ -0,0 +1,145 @@ +mod serde { + use serde::{Deserialize, Serialize}; + use serde_dhall::{from_str, serialize, FromDhall, StaticType, ToDhall}; + + fn assert_de(s: &str, x: T) + where + T: FromDhall + StaticType + PartialEq + std::fmt::Debug, + { + assert_eq!( + from_str(s) + .static_type_annotation() + .parse::() + .map_err(|e| e.to_string()), + Ok(x) + ); + } + fn assert_ser(s: &str, x: T) + where + T: ToDhall + StaticType + PartialEq + std::fmt::Debug, + { + assert_eq!( + serialize(&x) + .static_type_annotation() + .to_string() + .map_err(|e| e.to_string()), + Ok(s.to_string()) + ); + } + fn assert_serde(s: &str, x: T) + where + T: ToDhall + + FromDhall + + StaticType + + PartialEq + + std::fmt::Debug + + Clone, + { + assert_de(s, x.clone()); + assert_ser(s, x); + } + + #[test] + fn numbers() { + assert_serde("True", true); + + assert_serde("1", 1u64); + assert_serde("1", 1u32); + assert_serde("1", 1usize); + + assert_serde("+1", 1i64); + assert_serde("+1", 1i32); + assert_serde("+1", 1isize); + + assert_serde("1.0", 1.0f64); + assert_serde("1.0", 1.0f32); + } + + #[test] + fn text() { + assert_serde(r#""foo""#, "foo".to_owned()); + assert_ser(r#""foo""#, "foo"); + } + + #[test] + fn list() { + assert_serde("[] : List Natural", >::new()); + assert_serde("[] : List Text", >::new()); + assert_serde( + r#"["foo", "bar"]"#, + vec!["foo".to_owned(), "bar".to_owned()], + ); + assert_ser(r#"["foo", "bar"]"#, vec!["foo", "bar"]); + assert_serde::>("[1, 2]", vec![1, 2]); + } + + #[test] + fn optional() { + assert_serde::>("None Natural", None); + assert_serde::>("None Text", None); + assert_serde("Some 1", Some(1u64)); + } + + #[test] + fn tuple() { + assert_serde::<()>(r#"{=}"#, ()); + assert_serde::<(u64, String)>( + r#"{ _1 = 1, _2 = "foo" }"#, + (1, "foo".to_owned()), + ); + assert_serde::<(u64, u64, u64, u64)>( + r#"{ _1 = 1, _2 = 2, _3 = 3, _4 = 4 }"#, + (1, 2, 3, 4), + ); + } + + #[test] + fn structs() { + // #[derive( + // Debug, Clone, PartialEq, Eq, Deserialize, Serialize, StaticType, + // )] + // struct Foo; + // assert_serde::("{=}", Foo); + + // #[derive( + // Debug, Clone, PartialEq, Eq, Deserialize, Serialize, StaticType, + // )] + // struct Bar(u64); + // assert_serde::("{ _1 = 1 }", Bar (1)); + + #[derive( + Debug, Clone, PartialEq, Eq, Deserialize, Serialize, StaticType, + )] + struct Baz { + x: u64, + y: i64, + } + assert_serde::("{ x = 1, y = -2 }", Baz { x: 1, y: -2 }); + } + + #[test] + fn enums() { + #[derive( + Debug, Clone, PartialEq, Eq, Deserialize, Serialize, StaticType, + )] + enum Foo { + X(u64), + Y(i64), + } + assert_serde::("< X: Natural | Y: Integer >.X 1", Foo::X(1)); + + #[derive( + Debug, Clone, PartialEq, Eq, Deserialize, Serialize, StaticType, + )] + enum Bar { + X, + Y(i64), + } + assert_serde::("< X | Y: Integer >.X", Bar::X); + + assert!(from_str("< X | Y: Integer >.Y") + .static_type_annotation() + .parse::() + .is_err()); + } +} -- cgit v1.3.1 From 3b728aff86a086f71f013b77a715c33748d9f6a8 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 28 Oct 2020 21:45:42 +0000 Subject: Make type annotation optional to allow serializing SimpleValue --- serde_dhall/src/options/de.rs | 37 ++-------- serde_dhall/src/options/mod.rs | 34 +++++++++ serde_dhall/src/options/ser.rs | 33 +++------ serde_dhall/src/serialize.rs | 16 ++++- serde_dhall/src/value.rs | 140 ++++++++++++++++++++------------------ serde_dhall/tests/de.rs | 34 +-------- serde_dhall/tests/serde.rs | 8 ++- serde_dhall/tests/simple_value.rs | 57 ++++++++++++++++ 8 files changed, 202 insertions(+), 157 deletions(-) create mode 100644 serde_dhall/tests/simple_value.rs (limited to 'serde_dhall/tests') diff --git a/serde_dhall/src/options/de.rs b/serde_dhall/src/options/de.rs index 8ff794d..e4f3456 100644 --- a/serde_dhall/src/options/de.rs +++ b/serde_dhall/src/options/de.rs @@ -2,8 +2,9 @@ use std::path::{Path, PathBuf}; use dhall::Parsed; +use crate::options::{HasAnnot, ManualAnnot, NoAnnot, StaticAnnot, TypeAnnot}; use crate::SimpleType; -use crate::{Error, ErrorKind, FromDhall, Result, StaticType, Value}; +use crate::{Error, ErrorKind, FromDhall, Result, Value}; #[derive(Debug, Clone)] enum Source<'a> { @@ -12,32 +13,6 @@ enum Source<'a> { // Url(&'a str), } -#[derive(Debug, Clone, Copy)] -pub struct NoAnnot; -#[derive(Debug, Clone, Copy)] -pub struct ManualAnnot<'ty>(&'ty SimpleType); -#[derive(Debug, Clone, Copy)] -pub struct StaticAnnot; - -pub trait OptionalAnnot { - fn get_annot(a: &A) -> Option; -} -impl OptionalAnnot for T { - fn get_annot(_: &NoAnnot) -> Option { - None - } -} -impl<'ty, T> OptionalAnnot> for T { - fn get_annot(a: &ManualAnnot<'ty>) -> Option { - Some(a.0.clone()) - } -} -impl OptionalAnnot for T { - fn get_annot(_: &StaticAnnot) -> Option { - Some(T::static_type()) - } -} - /// Controls how a Dhall value is read. /// /// This builder exposes the ability to configure how a value is deserialized and what operations @@ -252,7 +227,8 @@ impl<'a, A> Deserializer<'a, A> { fn _parse(&self) -> dhall::error::Result where - T: OptionalAnnot, + A: TypeAnnot, + T: HasAnnot, { let parsed = match &self.source { Source::Str(s) => Parsed::parse_str(s)?, @@ -263,7 +239,7 @@ impl<'a, A> Deserializer<'a, A> { } else { parsed.skip_resolve()? }; - let typed = match &T::get_annot(&self.annot) { + let typed = match &T::get_annot(self.annot) { None => resolved.typecheck()?, Some(ty) => resolved.typecheck_with(ty.to_value().as_hir())?, }; @@ -287,7 +263,8 @@ impl<'a, A> Deserializer<'a, A> { /// [`StaticType`]: trait.StaticType.html pub fn parse(&self) -> Result where - T: FromDhall + OptionalAnnot, + A: TypeAnnot, + T: FromDhall + HasAnnot, { let val = self ._parse::() diff --git a/serde_dhall/src/options/mod.rs b/serde_dhall/src/options/mod.rs index 384f318..9241c45 100644 --- a/serde_dhall/src/options/mod.rs +++ b/serde_dhall/src/options/mod.rs @@ -1,2 +1,36 @@ +use crate::{SimpleType, StaticType}; + pub(crate) mod de; pub(crate) mod ser; + +#[derive(Debug, Clone, Copy)] +pub struct NoAnnot; +#[derive(Debug, Clone, Copy)] +pub struct ManualAnnot<'ty>(&'ty SimpleType); +#[derive(Debug, Clone, Copy)] +pub struct StaticAnnot; + +pub trait TypeAnnot: Copy {} +pub trait HasAnnot { + fn get_annot(a: A) -> Option; +} + +impl TypeAnnot for NoAnnot {} +impl TypeAnnot for ManualAnnot<'_> {} +impl TypeAnnot for StaticAnnot {} + +impl HasAnnot for T { + fn get_annot(_: NoAnnot) -> Option { + None + } +} +impl HasAnnot> for T { + fn get_annot(a: ManualAnnot<'_>) -> Option { + Some(a.0.clone()) + } +} +impl HasAnnot for T { + fn get_annot(_: StaticAnnot) -> Option { + Some(T::static_type()) + } +} diff --git a/serde_dhall/src/options/ser.rs b/serde_dhall/src/options/ser.rs index 026dd21..ea5d16a 100644 --- a/serde_dhall/src/options/ser.rs +++ b/serde_dhall/src/options/ser.rs @@ -1,25 +1,5 @@ -use crate::{Result, SimpleType, StaticType, ToDhall}; - -#[derive(Debug, Clone, Copy)] -pub struct NoAnnot; -#[derive(Debug, Clone, Copy)] -pub struct ManualAnnot<'ty>(&'ty SimpleType); -#[derive(Debug, Clone, Copy)] -pub struct StaticAnnot; - -pub trait RequiredAnnot { - fn get_annot(a: &A) -> SimpleType; -} -impl<'ty, T> RequiredAnnot> for T { - fn get_annot(a: &ManualAnnot<'ty>) -> SimpleType { - a.0.clone() - } -} -impl RequiredAnnot for T { - fn get_annot(_: &StaticAnnot) -> SimpleType { - T::static_type() - } -} +use crate::options::{HasAnnot, ManualAnnot, NoAnnot, StaticAnnot, TypeAnnot}; +use crate::{Result, SimpleType, ToDhall}; #[derive(Debug, Clone)] pub struct Serializer<'a, T, A> { @@ -46,12 +26,15 @@ impl<'a, T> Serializer<'a, T, NoAnnot> { } } -impl<'a, T, A> Serializer<'a, T, A> { +impl<'a, T, A> Serializer<'a, T, A> +where + A: TypeAnnot, +{ pub fn to_string(&self) -> Result where - T: ToDhall + RequiredAnnot, + T: ToDhall + HasAnnot, { - let val = self.data.to_dhall(&T::get_annot(&self.annot))?; + let val = self.data.to_dhall(T::get_annot(self.annot).as_ref())?; Ok(val.to_string()) } } diff --git a/serde_dhall/src/serialize.rs b/serde_dhall/src/serialize.rs index dd3e426..21b1931 100644 --- a/serde_dhall/src/serialize.rs +++ b/serde_dhall/src/serialize.rs @@ -11,7 +11,7 @@ pub trait Sealed {} pub trait ToDhall: Sealed { #[doc(hidden)] - fn to_dhall(&self, ty: &SimpleType) -> Result; + fn to_dhall(&self, ty: Option<&SimpleType>) -> Result; } impl Sealed for T where T: ser::Serialize {} @@ -20,7 +20,7 @@ impl ToDhall for T where T: ser::Serialize, { - fn to_dhall(&self, ty: &SimpleType) -> Result { + fn to_dhall(&self, ty: Option<&SimpleType>) -> Result { let sval: SimpleValue = self.serialize(Serializer)?; sval.into_value(ty) } @@ -310,3 +310,15 @@ impl ser::SerializeStruct for StructSerializer { Ok(Record(self.0)) } } + +impl serde::ser::Serialize for SimpleValue { + fn serialize( + &self, + serializer: S, + ) -> std::result::Result + where + S: serde::ser::Serializer, + { + todo!() + } +} diff --git a/serde_dhall/src/value.rs b/serde_dhall/src/value.rs index ca05658..50c12bd 100644 --- a/serde_dhall/src/value.rs +++ b/serde_dhall/src/value.rs @@ -293,87 +293,103 @@ impl SimpleValue { }) } - fn to_hir(&self, ty: &SimpleType) -> Result { + // Converts this to `Hir`, using the optional type annotation. Without the type, things like + // empty lists and unions will fail to convert. + fn to_hir(&self, ty: Option<&SimpleType>) -> Result { use SimpleType as T; use SimpleValue as V; let hir = |k| Hir::new(HirKind::Expr(k), Span::Artificial); - let type_error = |msg| Error(ErrorKind::Serialize(msg)); + let type_error = || { + Error(ErrorKind::Serialize(format!( + "expected a value of type {}, found {:?}", + ty.unwrap().to_hir().to_expr(Default::default()), + self + ))) + }; + let type_missing = || { + Error(ErrorKind::Serialize(format!( + "cannot serialize value without a type annotation: {:?}", + self + ))) + }; let kind = match (self, ty) { - (V::Num(num @ NumKind::Bool(_)), T::Bool) - | (V::Num(num @ NumKind::Natural(_)), T::Natural) - | (V::Num(num @ NumKind::Integer(_)), T::Integer) - | (V::Num(num @ NumKind::Double(_)), T::Double) => { - ExprKind::Num(num.clone()) + (V::Num(num @ NumKind::Bool(_)), Some(T::Bool)) + | (V::Num(num @ NumKind::Natural(_)), Some(T::Natural)) + | (V::Num(num @ NumKind::Integer(_)), Some(T::Integer)) + | (V::Num(num @ NumKind::Double(_)), Some(T::Double)) + | (V::Num(num), None) => ExprKind::Num(num.clone()), + (V::Text(v), Some(T::Text)) | (V::Text(v), None) => { + ExprKind::TextLit(v.clone().into()) } - (V::Text(v), T::Text) => ExprKind::TextLit(v.clone().into()), - (V::Optional(None), T::Optional(t)) => ExprKind::Op(OpKind::App( - hir(ExprKind::Builtin(Builtin::OptionalNone)), - t.to_hir(), - )), - (V::Optional(Some(v)), T::Optional(t)) => { - ExprKind::SomeLit(v.to_hir(t)?) + + (V::Optional(None), None) => return Err(type_missing()), + (V::Optional(None), Some(T::Optional(t))) => { + ExprKind::Op(OpKind::App( + hir(ExprKind::Builtin(Builtin::OptionalNone)), + t.to_hir(), + )) + } + (V::Optional(Some(v)), None) => ExprKind::SomeLit(v.to_hir(None)?), + (V::Optional(Some(v)), Some(T::Optional(t))) => { + ExprKind::SomeLit(v.to_hir(Some(t))?) } - (V::List(v), T::List(t)) if v.is_empty() => { + + (V::List(v), None) if v.is_empty() => return Err(type_missing()), + (V::List(v), Some(T::List(t))) if v.is_empty() => { ExprKind::EmptyListLit(hir(ExprKind::Op(OpKind::App( hir(ExprKind::Builtin(Builtin::List)), t.to_hir(), )))) } - (V::List(v), T::List(t)) => ExprKind::NEListLit( - v.iter().map(|v| v.to_hir(t)).collect::>()?, + (V::List(v), None) => ExprKind::NEListLit( + v.iter().map(|v| v.to_hir(None)).collect::>()?, + ), + (V::List(v), Some(T::List(t))) => ExprKind::NEListLit( + v.iter().map(|v| v.to_hir(Some(t))).collect::>()?, + ), + + (V::Record(v), None) => ExprKind::RecordLit( + v.iter() + .map(|(k, v)| Ok((k.clone().into(), v.to_hir(None)?))) + .collect::>()?, ), - (V::Record(v), T::Record(t)) => ExprKind::RecordLit( + (V::Record(v), Some(T::Record(t))) => ExprKind::RecordLit( v.iter() .map(|(k, v)| match t.get(k) { - Some(t) => Ok((k.clone().into(), v.to_hir(t)?)), - None => Err(type_error(format!( - "expected a value of type {}, found {:?}", - ty.to_hir().to_expr(Default::default()), - self - ))), + Some(t) => Ok((k.clone().into(), v.to_hir(Some(t))?)), + None => Err(type_error()), }) .collect::>()?, ), - (V::Union(variant, Some(v)), T::Union(t)) => match t.get(variant) { - Some(Some(variant_t)) => ExprKind::Op(OpKind::App( - hir(ExprKind::Op(OpKind::Field( - ty.to_hir(), - variant.clone().into(), - ))), - v.to_hir(variant_t)?, - )), - _ => { - return Err(type_error(format!( - "expected a value of type {}, found {:?}", - ty.to_hir().to_expr(Default::default()), - self - ))) + + (V::Union(..), None) => return Err(type_missing()), + (V::Union(variant, Some(v)), Some(T::Union(t))) => { + match t.get(variant) { + Some(Some(variant_t)) => ExprKind::Op(OpKind::App( + hir(ExprKind::Op(OpKind::Field( + ty.unwrap().to_hir(), + variant.clone().into(), + ))), + v.to_hir(Some(variant_t))?, + )), + _ => return Err(type_error()), } - }, - (V::Union(variant, None), T::Union(t)) => match t.get(variant) { - Some(None) => ExprKind::Op(OpKind::Field( - ty.to_hir(), - variant.clone().into(), - )), - _ => { - return Err(type_error(format!( - "expected a value of type {}, found {:?}", - ty.to_hir().to_expr(Default::default()), - self - ))) + } + (V::Union(variant, None), Some(T::Union(t))) => { + match t.get(variant) { + Some(None) => ExprKind::Op(OpKind::Field( + ty.unwrap().to_hir(), + variant.clone().into(), + )), + _ => return Err(type_error()), } - }, - _ => { - return Err(type_error(format!( - "expected a value of type {}, found {:?}", - ty.to_hir().to_expr(Default::default()), - self - ))) } + + (_, Some(_)) => return Err(type_error()), }; Ok(hir(kind)) } - pub(crate) fn into_value(self, ty: &SimpleType) -> Result { + pub(crate) fn into_value(self, ty: Option<&SimpleType>) -> Result { Ok(Value { hir: self.to_hir(ty)?, as_simple_val: Some(self), @@ -467,7 +483,6 @@ impl SimpleType { impl crate::deserialize::Sealed for Value {} impl crate::deserialize::Sealed for SimpleType {} impl crate::serialize::Sealed for Value {} -impl crate::serialize::Sealed for SimpleValue {} impl FromDhall for Value { fn from_dhall(v: &Value) -> Result { @@ -485,15 +500,10 @@ impl FromDhall for SimpleType { } } impl ToDhall for Value { - fn to_dhall(&self, _ty: &SimpleType) -> Result { + fn to_dhall(&self, _ty: Option<&SimpleType>) -> Result { Ok(self.clone()) } } -impl ToDhall for SimpleValue { - fn to_dhall(&self, ty: &SimpleType) -> Result { - self.clone().into_value(ty) - } -} impl Eq for Value {} impl PartialEq for Value { diff --git a/serde_dhall/tests/de.rs b/serde_dhall/tests/de.rs index 0b43de2..f49bee4 100644 --- a/serde_dhall/tests/de.rs +++ b/serde_dhall/tests/de.rs @@ -1,5 +1,5 @@ use serde::Deserialize; -use serde_dhall::{from_str, FromDhall, NumKind, SimpleValue}; +use serde_dhall::{from_str, FromDhall}; #[test] fn test_de_untyped() { @@ -57,37 +57,5 @@ fn test_de_untyped() { assert!(from_str("List/length [True, 42]").parse::().is_err()); } -#[test] -fn test_de_simplevalue() { - let bool_true = SimpleValue::Num(NumKind::Bool(true)); - // https://github.com/Nadrieril/dhall-rust/issues/184 - assert_eq!( - from_str("[ True ]").parse::>().unwrap(), - vec![bool_true.clone()] - ); - - assert_eq!( - from_str("< Foo >.Foo").parse::().unwrap(), - SimpleValue::Union("Foo".into(), None) - ); - assert_eq!( - from_str("< Foo: Bool >.Foo True") - .parse::() - .unwrap(), - SimpleValue::Union("Foo".into(), Some(Box::new(bool_true.clone()))) - ); - - #[derive(Debug, PartialEq, Eq, Deserialize)] - struct Foo { - foo: SimpleValue, - } - assert_eq!( - from_str("{ foo = True }").parse::().unwrap(), - Foo { - foo: bool_true.clone() - }, - ); -} - // TODO: test various builder configurations // In particular test cloning and reusing builder diff --git a/serde_dhall/tests/serde.rs b/serde_dhall/tests/serde.rs index 39f2f79..ce25380 100644 --- a/serde_dhall/tests/serde.rs +++ b/serde_dhall/tests/serde.rs @@ -75,9 +75,13 @@ mod serde { #[test] fn optional() { - assert_serde::>("None Natural", None); - assert_serde::>("None Text", None); + assert_serde("None Natural", None::); + assert_serde("None Text", None::); assert_serde("Some 1", Some(1u64)); + assert_eq!( + serialize(&None::).to_string().map_err(|e| e.to_string()), + Err("cannot serialize value without a type annotation: Optional(None)".to_string()) + ); } #[test] diff --git a/serde_dhall/tests/simple_value.rs b/serde_dhall/tests/simple_value.rs new file mode 100644 index 0000000..d2792d4 --- /dev/null +++ b/serde_dhall/tests/simple_value.rs @@ -0,0 +1,57 @@ +mod simple_value { + use serde::{Deserialize, Serialize}; + use serde_dhall::{ + from_str, serialize, FromDhall, NumKind, SimpleValue, ToDhall, + }; + + fn assert_de(s: &str, x: T) + where + T: FromDhall + PartialEq + std::fmt::Debug, + { + assert_eq!(from_str(s).parse::().map_err(|e| e.to_string()), Ok(x)); + } + fn assert_ser(s: &str, x: T) + where + T: ToDhall + PartialEq + std::fmt::Debug, + { + assert_eq!( + serialize(&x).to_string().map_err(|e| e.to_string()), + Ok(s.to_string()) + ); + } + fn assert_serde(s: &str, x: T) + where + T: ToDhall + FromDhall + PartialEq + std::fmt::Debug + Clone, + { + assert_de(s, x.clone()); + assert_ser(s, x); + } + + #[test] + fn test_serde() { + let bool_true = SimpleValue::Num(NumKind::Bool(true)); + // https://github.com/Nadrieril/dhall-rust/issues/184 + // assert_serde("[ True ]", vec![bool_true.clone()]); + assert_de("< Foo >.Foo", SimpleValue::Union("Foo".into(), None)); + assert_de( + "< Foo: Bool >.Foo True", + SimpleValue::Union("Foo".into(), Some(Box::new(bool_true.clone()))), + ); + + // assert_eq!( + // serialize(&SimpleValue::Optional(None)).to_string().map_err(|e| e.to_string()), + // Err("cannot serialize value without a type annotation: Optional(None)".to_string()) + // ); + + // #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] + // struct Foo { + // foo: SimpleValue, + // } + // assert_serde( + // "{ foo = True }", + // Foo { + // foo: bool_true.clone(), + // }, + // ); + } +} -- cgit v1.3.1 From 93ed3cf67c49bf7016b8b1780d873cfdffcb84c5 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 28 Oct 2020 23:21:19 +0000 Subject: Implement SimpleValue serialization --- serde_dhall/src/serialize.rs | 46 ++++++++++++++++++++++++++++++++++++++- serde_dhall/tests/simple_value.rs | 31 +++++++++++++------------- 2 files changed, 61 insertions(+), 16 deletions(-) (limited to 'serde_dhall/tests') diff --git a/serde_dhall/src/serialize.rs b/serde_dhall/src/serialize.rs index 632dace..286c04d 100644 --- a/serde_dhall/src/serialize.rs +++ b/serde_dhall/src/serialize.rs @@ -347,6 +347,50 @@ impl serde::ser::Serialize for SimpleValue { where S: serde::ser::Serializer, { - todo!() + use serde::ser::{SerializeMap, SerializeSeq}; + use NumKind::*; + use SimpleValue::*; + + match self { + Num(Bool(x)) => serializer.serialize_bool(*x), + Num(Natural(x)) => serializer.serialize_u64(*x), + Num(Integer(x)) => serializer.serialize_i64(*x), + Num(Double(x)) => serializer.serialize_f64((*x).into()), + Text(x) => serializer.serialize_str(x), + List(xs) => { + let mut seq = serializer.serialize_seq(Some(xs.len()))?; + for x in xs { + seq.serialize_element(x)?; + } + seq.end() + } + Optional(None) => serializer.serialize_none(), + Optional(Some(x)) => serializer.serialize_some(x), + Record(m) => { + let mut map = serializer.serialize_map(Some(m.len()))?; + for (k, v) in m { + map.serialize_entry(k, v)?; + } + map.end() + } + // serde's enum support is yet again really limited. We can't avoid a memleak here :(. + Union(field_name, None) => { + let field_name: Box = field_name.clone().into(); + serializer.serialize_unit_variant( + "SimpleValue", + 0, + Box::leak(field_name), + ) + } + Union(field_name, Some(x)) => { + let field_name: Box = field_name.clone().into(); + serializer.serialize_newtype_variant( + "SimpleValue", + 0, + Box::leak(field_name), + x, + ) + } + } } } diff --git a/serde_dhall/tests/simple_value.rs b/serde_dhall/tests/simple_value.rs index d2792d4..3bd9d64 100644 --- a/serde_dhall/tests/simple_value.rs +++ b/serde_dhall/tests/simple_value.rs @@ -31,27 +31,28 @@ mod simple_value { fn test_serde() { let bool_true = SimpleValue::Num(NumKind::Bool(true)); // https://github.com/Nadrieril/dhall-rust/issues/184 - // assert_serde("[ True ]", vec![bool_true.clone()]); + assert_serde("[True]", vec![bool_true.clone()]); + assert_de("< Foo >.Foo", SimpleValue::Union("Foo".into(), None)); assert_de( "< Foo: Bool >.Foo True", SimpleValue::Union("Foo".into(), Some(Box::new(bool_true.clone()))), ); - // assert_eq!( - // serialize(&SimpleValue::Optional(None)).to_string().map_err(|e| e.to_string()), - // Err("cannot serialize value without a type annotation: Optional(None)".to_string()) - // ); + assert_eq!( + serialize(&SimpleValue::Union("Foo".into(), None)).to_string().map_err(|e| e.to_string()), + Err("cannot serialize value without a type annotation: Union(\"Foo\", None)".to_string()) + ); - // #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] - // struct Foo { - // foo: SimpleValue, - // } - // assert_serde( - // "{ foo = True }", - // Foo { - // foo: bool_true.clone(), - // }, - // ); + #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] + struct Foo { + foo: SimpleValue, + } + assert_serde( + "{ foo = True }", + Foo { + foo: bool_true.clone(), + }, + ); } } -- cgit v1.3.1 From 1a53f10c6ed1ef65621b39c40e05da79161047cc Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 28 Oct 2020 23:26:22 +0000 Subject: Move some tests --- serde_dhall/tests/de.rs | 61 ---------------------------------------------- serde_dhall/tests/serde.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 61 deletions(-) delete mode 100644 serde_dhall/tests/de.rs (limited to 'serde_dhall/tests') diff --git a/serde_dhall/tests/de.rs b/serde_dhall/tests/de.rs deleted file mode 100644 index f49bee4..0000000 --- a/serde_dhall/tests/de.rs +++ /dev/null @@ -1,61 +0,0 @@ -use serde::Deserialize; -use serde_dhall::{from_str, FromDhall}; - -#[test] -fn test_de_untyped() { - use std::collections::BTreeMap; - use std::collections::HashMap; - - fn parse(s: &str) -> T { - from_str(s).parse().unwrap() - } - - // Test tuples on record of wrong type - assert_eq!( - parse::<(u64, String, i64)>(r#"{ y = "foo", x = 1, z = +42 }"#), - (1, "foo".to_owned(), 42) - ); - - let mut expected_map = HashMap::new(); - expected_map.insert("x".to_string(), 1); - expected_map.insert("y".to_string(), 2); - assert_eq!( - parse::>("{ x = 1, y = 2 }"), - expected_map - ); - assert_eq!( - parse::>("toMap { x = 1, y = 2 }"), - expected_map - ); - - let mut expected_map = HashMap::new(); - expected_map.insert("if".to_string(), 1); - expected_map.insert("FOO_BAR".to_string(), 2); - expected_map.insert("baz-kux".to_string(), 3); - assert_eq!( - parse::>("{ `if` = 1, FOO_BAR = 2, baz-kux = 3 }"), - expected_map - ); - - let mut expected_map = BTreeMap::new(); - expected_map.insert("x".to_string(), 1); - expected_map.insert("y".to_string(), 2); - assert_eq!( - parse::>("{ x = 1, y = 2 }"), - expected_map - ); - - #[derive(Debug, PartialEq, Eq, Deserialize)] - struct Foo { - x: u64, - y: Option, - } - // Omit optional field - assert_eq!(parse::("{ x = 1 }"), Foo { x: 1, y: None }); - - // https://github.com/Nadrieril/dhall-rust/issues/155 - assert!(from_str("List/length [True, 42]").parse::().is_err()); -} - -// TODO: test various builder configurations -// In particular test cloning and reusing builder diff --git a/serde_dhall/tests/serde.rs b/serde_dhall/tests/serde.rs index ce25380..03bcc58 100644 --- a/serde_dhall/tests/serde.rs +++ b/serde_dhall/tests/serde.rs @@ -146,4 +146,63 @@ mod serde { .parse::() .is_err()); } + + #[test] + fn test_de_untyped() { + use std::collections::BTreeMap; + use std::collections::HashMap; + + fn parse(s: &str) -> T { + from_str(s).parse().unwrap() + } + + // Test tuples on record of wrong type. Fields are just taken in alphabetic order. + assert_eq!( + parse::<(u64, String, i64)>(r#"{ y = "foo", x = 1, z = +42 }"#), + (1, "foo".to_owned(), 42) + ); + + let mut expected_map = HashMap::new(); + expected_map.insert("x".to_string(), 1); + expected_map.insert("y".to_string(), 2); + assert_eq!( + parse::>("{ x = 1, y = 2 }"), + expected_map + ); + assert_eq!( + parse::>("toMap { x = 1, y = 2 }"), + expected_map + ); + + let mut expected_map = HashMap::new(); + expected_map.insert("if".to_string(), 1); + expected_map.insert("FOO_BAR".to_string(), 2); + expected_map.insert("baz-kux".to_string(), 3); + assert_eq!( + parse::>("{ `if` = 1, FOO_BAR = 2, baz-kux = 3 }"), + expected_map + ); + + let mut expected_map = BTreeMap::new(); + expected_map.insert("x".to_string(), 1); + expected_map.insert("y".to_string(), 2); + assert_eq!( + parse::>("{ x = 1, y = 2 }"), + expected_map + ); + + #[derive(Debug, PartialEq, Eq, Deserialize)] + struct Foo { + x: u64, + y: Option, + } + // Omit optional field + assert_eq!(parse::("{ x = 1 }"), Foo { x: 1, y: None }); + + // https://github.com/Nadrieril/dhall-rust/issues/155 + assert!(from_str("List/length [True, 42]").parse::().is_err()); + } + + // TODO: test various builder configurations + // In particular test cloning and reusing builder } -- cgit v1.3.1 From af16e9699799f8cfbd228e2832e1d5df3653116b Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 28 Oct 2020 23:31:57 +0000 Subject: Fix clippy and formatting --- serde_dhall/src/options/ser.rs | 2 +- serde_dhall/tests/serde.rs | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'serde_dhall/tests') diff --git a/serde_dhall/src/options/ser.rs b/serde_dhall/src/options/ser.rs index 3332735..d74beb0 100644 --- a/serde_dhall/src/options/ser.rs +++ b/serde_dhall/src/options/ser.rs @@ -223,7 +223,7 @@ where /// [`type_annotation`]: struct.Serializer.html#method.type_annotation /// [`static_type_annotation`]: struct.Serializer.html#method.static_type_annotation /// [`to_string`]: struct.Serializer.html#method.to_string -pub fn serialize<'a, T>(data: &'a T) -> Serializer<'a, T, NoAnnot> +pub fn serialize(data: &T) -> Serializer<'_, T, NoAnnot> where T: ToDhall, { diff --git a/serde_dhall/tests/serde.rs b/serde_dhall/tests/serde.rs index 03bcc58..4c184e7 100644 --- a/serde_dhall/tests/serde.rs +++ b/serde_dhall/tests/serde.rs @@ -179,7 +179,9 @@ mod serde { expected_map.insert("FOO_BAR".to_string(), 2); expected_map.insert("baz-kux".to_string(), 3); assert_eq!( - parse::>("{ `if` = 1, FOO_BAR = 2, baz-kux = 3 }"), + parse::>( + "{ `if` = 1, FOO_BAR = 2, baz-kux = 3 }" + ), expected_map ); -- cgit v1.3.1