From 1a0929b52af57d5963dd9da9e5cf85ffbed3a8f7 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 13 Mar 2020 20:53:23 +0000 Subject: Reorganize serde modules --- serde_dhall/src/simple.rs | 81 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 serde_dhall/src/simple.rs (limited to 'serde_dhall/src/simple.rs') diff --git a/serde_dhall/src/simple.rs b/serde_dhall/src/simple.rs new file mode 100644 index 0000000..93364a0 --- /dev/null +++ b/serde_dhall/src/simple.rs @@ -0,0 +1,81 @@ +use super::Error; +use dhall::{STyKind, SimpleType, SimpleValue}; + +/// A Dhall value. This is a wrapper around [`dhall::SimpleValue`]. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Value(SimpleValue); + +impl Value { + pub fn into_simple_value(self) -> SimpleValue { + self.0 + } +} + +/// A Dhall type. This is a wrapper around [`dhall::SimpleType`]. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Type(SimpleType); + +impl Type { + pub fn into_simple_type(self) -> SimpleType { + self.0 + } + pub fn to_dhall_value(&self) -> dhall::Value { + self.0.to_value() + } + + pub(crate) fn from_simple_type(ty: SimpleType) -> Self { + Type(ty) + } + pub(crate) fn from_stykind(k: STyKind) -> Self { + Type(SimpleType::new(k)) + } + pub(crate) fn make_optional_type(t: Type) -> Self { + Type::from_stykind(STyKind::Optional(t.0)) + } + pub(crate) fn make_list_type(t: Type) -> Self { + Type::from_stykind(STyKind::List(t.0)) + } + // Made public for the StaticType derive macro + #[doc(hidden)] + pub fn make_record_type(kts: impl Iterator) -> Self { + Type::from_stykind(STyKind::Record( + kts.map(|(k, t)| (k, t.0)).collect(), + )) + } + #[doc(hidden)] + pub fn make_union_type( + kts: impl Iterator)>, + ) -> Self { + Type::from_stykind(STyKind::Union( + kts.map(|(k, t)| (k, t.map(|t| t.0))).collect(), + )) + } +} + +impl super::sealed::Sealed for Value {} + +impl super::Deserialize for Value { + fn from_dhall(v: &dhall::Value) -> super::Result { + let sval = v.to_simple_value().ok_or_else(|| { + Error::Deserialize(format!( + "this cannot be deserialized into a simple type: {}", + v + )) + })?; + Ok(Value(sval)) + } +} + +impl super::sealed::Sealed for Type {} + +impl super::Deserialize for Type { + fn from_dhall(v: &dhall::Value) -> super::Result { + let sty = v.to_simple_type().ok_or_else(|| { + Error::Deserialize(format!( + "this cannot be deserialized into a simple type: {}", + v + )) + })?; + Ok(Type(sty)) + } +} -- cgit v1.2.3