summaryrefslogtreecommitdiff
path: root/serde_dhall/src/options
diff options
context:
space:
mode:
authorNadrieril2020-10-28 21:45:42 +0000
committerNadrieril2020-10-28 22:52:41 +0000
commit3b728aff86a086f71f013b77a715c33748d9f6a8 (patch)
tree7c1ceba27495481b766b4ea84601d7381530f5c2 /serde_dhall/src/options
parente070270c3f1f10d46281ed7751ff95e15092e7f4 (diff)
Make type annotation optional to allow serializing SimpleValue
Diffstat (limited to 'serde_dhall/src/options')
-rw-r--r--serde_dhall/src/options/de.rs37
-rw-r--r--serde_dhall/src/options/mod.rs34
-rw-r--r--serde_dhall/src/options/ser.rs33
3 files changed, 49 insertions, 55 deletions
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<A> {
- fn get_annot(a: &A) -> Option<SimpleType>;
-}
-impl<T> OptionalAnnot<NoAnnot> for T {
- fn get_annot(_: &NoAnnot) -> Option<SimpleType> {
- None
- }
-}
-impl<'ty, T> OptionalAnnot<ManualAnnot<'ty>> for T {
- fn get_annot(a: &ManualAnnot<'ty>) -> Option<SimpleType> {
- Some(a.0.clone())
- }
-}
-impl<T: StaticType> OptionalAnnot<StaticAnnot> for T {
- fn get_annot(_: &StaticAnnot) -> Option<SimpleType> {
- 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<T>(&self) -> dhall::error::Result<Value>
where
- T: OptionalAnnot<A>,
+ A: TypeAnnot,
+ T: HasAnnot<A>,
{
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<T>(&self) -> Result<T>
where
- T: FromDhall + OptionalAnnot<A>,
+ A: TypeAnnot,
+ T: FromDhall + HasAnnot<A>,
{
let val = self
._parse::<T>()
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<A: TypeAnnot> {
+ fn get_annot(a: A) -> Option<SimpleType>;
+}
+
+impl TypeAnnot for NoAnnot {}
+impl TypeAnnot for ManualAnnot<'_> {}
+impl TypeAnnot for StaticAnnot {}
+
+impl<T> HasAnnot<NoAnnot> for T {
+ fn get_annot(_: NoAnnot) -> Option<SimpleType> {
+ None
+ }
+}
+impl<T> HasAnnot<ManualAnnot<'_>> for T {
+ fn get_annot(a: ManualAnnot<'_>) -> Option<SimpleType> {
+ Some(a.0.clone())
+ }
+}
+impl<T: StaticType> HasAnnot<StaticAnnot> for T {
+ fn get_annot(_: StaticAnnot) -> Option<SimpleType> {
+ 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<A> {
- fn get_annot(a: &A) -> SimpleType;
-}
-impl<'ty, T> RequiredAnnot<ManualAnnot<'ty>> for T {
- fn get_annot(a: &ManualAnnot<'ty>) -> SimpleType {
- a.0.clone()
- }
-}
-impl<T: StaticType> RequiredAnnot<StaticAnnot> 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<String>
where
- T: ToDhall + RequiredAnnot<A>,
+ T: ToDhall + HasAnnot<A>,
{
- 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())
}
}