summaryrefslogtreecommitdiff
path: root/serde_dhall/src/serde.rs
diff options
context:
space:
mode:
authorNadrieril2020-04-05 17:57:07 +0100
committerGitHub2020-04-05 17:57:07 +0100
commit7e977f282fb6a0eff0ef45738b9b5c98dc4c6fee (patch)
treead4249609707fd8720a44469152105c2f6a67c79 /serde_dhall/src/serde.rs
parent5a5aa49e64197899006751db72e404f4b2292d4e (diff)
parent820214615547101f8f2b5de209b5189968bddfee (diff)
Merge pull request #154 from Nadrieril/cleanup-api
Rewrite serde_dhall API
Diffstat (limited to '')
-rw-r--r--serde_dhall/src/serde.rs144
1 files changed, 0 insertions, 144 deletions
diff --git a/serde_dhall/src/serde.rs b/serde_dhall/src/serde.rs
deleted file mode 100644
index 4fd7815..0000000
--- a/serde_dhall/src/serde.rs
+++ /dev/null
@@ -1,144 +0,0 @@
-use std::borrow::Cow;
-
-use serde::de::value::{
- MapAccessDeserializer, MapDeserializer, SeqDeserializer,
-};
-
-use dhall::syntax::{ExprKind, LitKind};
-use dhall::NormalizedExpr;
-
-use crate::de::{Deserialize, Error, Result};
-use crate::Value;
-
-impl<'a, T> crate::de::sealed::Sealed for T where T: serde::Deserialize<'a> {}
-
-impl<'a, T> Deserialize for T
-where
- T: serde::Deserialize<'a>,
-{
- fn from_dhall(v: &Value) -> Result<Self> {
- T::deserialize(Deserializer(Cow::Owned(v.to_expr())))
- }
-}
-
-struct Deserializer<'a>(Cow<'a, NormalizedExpr>);
-
-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<V>(self, visitor: V) -> Result<V::Value>
- where
- V: serde::de::Visitor<'de>,
- {
- use std::convert::TryInto;
- use ExprKind::*;
- use LitKind::*;
- let expr = self.0.as_ref();
- let not_serde_compatible = || {
- Err(Error::Deserialize(format!(
- "this cannot be deserialized into the serde data model: {}",
- expr
- )))
- };
-
- match expr.kind() {
- Lit(Bool(x)) => visitor.visit_bool(*x),
- Lit(Natural(x)) => {
- if let Ok(x64) = (*x).try_into() {
- visitor.visit_u64(x64)
- } else if let Ok(x32) = (*x).try_into() {
- visitor.visit_u32(x32)
- } else {
- unimplemented!()
- }
- }
- Lit(Integer(x)) => {
- if let Ok(x64) = (*x).try_into() {
- visitor.visit_i64(x64)
- } else if let Ok(x32) = (*x).try_into() {
- visitor.visit_i32(x32)
- } else {
- unimplemented!()
- }
- }
- Lit(Double(x)) => visitor.visit_f64((*x).into()),
- TextLit(x) => {
- // Normal form ensures that the tail is empty.
- assert!(x.tail().is_empty());
- visitor.visit_str(x.head())
- }
- EmptyListLit(..) => {
- visitor.visit_seq(SeqDeserializer::new(None::<()>.into_iter()))
- }
- NEListLit(xs) => visitor.visit_seq(SeqDeserializer::new(
- xs.iter().map(|x| Deserializer(Cow::Borrowed(x))),
- )),
- SomeLit(x) => visitor.visit_some(Deserializer(Cow::Borrowed(x))),
- App(f, x) => match f.kind() {
- Builtin(dhall::syntax::Builtin::OptionalNone) => {
- visitor.visit_none()
- }
- Field(y, name) => match y.kind() {
- UnionType(..) => {
- let name: String = name.into();
- visitor.visit_enum(MapAccessDeserializer::new(
- MapDeserializer::new(
- Some((name, Deserializer(Cow::Borrowed(x))))
- .into_iter(),
- ),
- ))
- }
- _ => not_serde_compatible(),
- },
- _ => not_serde_compatible(),
- },
- RecordLit(m) => visitor
- .visit_map(MapDeserializer::new(m.iter().map(|(k, v)| {
- (k.as_ref(), Deserializer(Cow::Borrowed(v)))
- }))),
- Field(y, name) => match y.kind() {
- UnionType(..) => {
- let name: String = name.into();
- visitor.visit_enum(MapAccessDeserializer::new(
- MapDeserializer::new(Some((name, ())).into_iter()),
- ))
- }
- _ => not_serde_compatible(),
- },
- Const(..) | Var(..) | Lam(..) | Pi(..) | Let(..) | Annot(..)
- | Assert(..) | Builtin(..) | BinOp(..) | BoolIf(..)
- | RecordType(..) | UnionType(..) | Merge(..) | ToMap(..)
- | Projection(..) | ProjectionByExpr(..) | Completion(..)
- | Import(..) => not_serde_compatible(),
- }
- }
-
- fn deserialize_tuple<V>(self, _: usize, visitor: V) -> Result<V::Value>
- where
- V: serde::de::Visitor<'de>,
- {
- use ExprKind::*;
- let expr = self.0.as_ref();
-
- match expr.kind() {
- // Blindly takes keys in sorted order.
- RecordLit(m) => visitor.visit_seq(SeqDeserializer::new(
- m.iter().map(|(_, v)| Deserializer(Cow::Borrowed(v))),
- )),
- _ => self.deserialize_any(visitor),
- }
- }
-
- 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_struct map struct enum identifier ignored_any
- }
-}