diff options
author | Nadrieril | 2020-10-28 23:40:28 +0000 |
---|---|---|
committer | GitHub | 2020-10-28 23:40:28 +0000 |
commit | f8234684b1129dac84d09adfb24bdc0f98448b9b (patch) | |
tree | 1440cfb514fa856718074f2b1ebce801459b5c0c /serde_dhall/src/options.rs | |
parent | 70727acbda68e104f60ae1dbbe95adbcec08a628 (diff) | |
parent | af16e9699799f8cfbd228e2832e1d5df3653116b (diff) |
Merge pull request #189 from Nadrieril/serialize
Diffstat (limited to '')
-rw-r--r-- | serde_dhall/src/options/de.rs (renamed from serde_dhall/src/options.rs) | 33 |
1 files changed, 5 insertions, 28 deletions
diff --git a/serde_dhall/src/options.rs b/serde_dhall/src/options/de.rs index 7b27114..e4f3456 100644 --- a/serde_dhall/src/options.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 HasAnnot<A> { - fn get_annot(a: &A) -> Option<SimpleType>; -} -impl<T> HasAnnot<NoAnnot> for T { - fn get_annot(_: &NoAnnot) -> Option<SimpleType> { - None - } -} -impl<'ty, T> HasAnnot<ManualAnnot<'ty>> for T { - fn get_annot(a: &ManualAnnot<'ty>) -> Option<SimpleType> { - Some(a.0.clone()) - } -} -impl<T: StaticType> HasAnnot<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,6 +227,7 @@ impl<'a, A> Deserializer<'a, A> { fn _parse<T>(&self) -> dhall::error::Result<Value> where + A: TypeAnnot, T: HasAnnot<A>, { let parsed = match &self.source { @@ -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,6 +263,7 @@ impl<'a, A> Deserializer<'a, A> { /// [`StaticType`]: trait.StaticType.html pub fn parse<T>(&self) -> Result<T> where + A: TypeAnnot, T: FromDhall + HasAnnot<A>, { let val = self |