diff options
Diffstat (limited to 'serde_dhall/src')
-rw-r--r-- | serde_dhall/src/deserialize.rs | 10 | ||||
-rw-r--r-- | serde_dhall/src/simple.rs | 73 | ||||
-rw-r--r-- | serde_dhall/src/value.rs | 2 |
3 files changed, 35 insertions, 50 deletions
diff --git a/serde_dhall/src/deserialize.rs b/serde_dhall/src/deserialize.rs index cccadad..43b7914 100644 --- a/serde_dhall/src/deserialize.rs +++ b/serde_dhall/src/deserialize.rs @@ -5,7 +5,7 @@ use std::borrow::Cow; use dhall::syntax::NumKind; -use crate::simple::{ValKind, Value as SimpleValue}; +use crate::simple::SimpleValue; use crate::{Error, Result, Value}; pub trait Sealed {} @@ -58,10 +58,10 @@ impl<'de: 'a, 'a> serde::Deserializer<'de> for Deserializer<'a> { { use std::convert::TryInto; use NumKind::*; - use ValKind::*; + use SimpleValue::*; let val = |x| Deserializer(Cow::Borrowed(x)); - match self.0.kind() { + match self.0.as_ref() { Num(Bool(x)) => visitor.visit_bool(*x), Num(Natural(x)) => { if let Ok(x64) = (*x).try_into() { @@ -109,9 +109,9 @@ impl<'de: 'a, 'a> serde::Deserializer<'de> for Deserializer<'a> { V: serde::de::Visitor<'de>, { let val = |x| Deserializer(Cow::Borrowed(x)); - match self.0.kind() { + match self.0.as_ref() { // Blindly takes keys in sorted order. - ValKind::Record(m) => visitor + SimpleValue::Record(m) => visitor .visit_seq(SeqDeserializer::new(m.iter().map(|(_, v)| val(v)))), _ => self.deserialize_any(visitor), } diff --git a/serde_dhall/src/simple.rs b/serde_dhall/src/simple.rs index fc640b6..c1f0047 100644 --- a/serde_dhall/src/simple.rs +++ b/serde_dhall/src/simple.rs @@ -3,22 +3,17 @@ use std::collections::{BTreeMap, HashMap}; use dhall::semantics::{Hir, HirKind, Nir, NirKind}; use dhall::syntax::{Builtin, ExprKind, NumKind, Span}; -use crate::{Deserialize, Error, Result, Sealed}; +use crate::{Deserialize, Error, Result, Sealed, Value}; -/// A simple value of the kind that can be encoded/decoded with serde +/// A simple value of the kind that can be decoded with serde #[derive(Debug, Clone, PartialEq, Eq)] -pub(crate) struct Value { - kind: Box<ValKind>, -} - -#[derive(Debug, Clone, PartialEq, Eq)] -pub(crate) enum ValKind { +pub(crate) enum SimpleValue { Num(NumKind), Text(String), - Optional(Option<Value>), - List(Vec<Value>), - Record(BTreeMap<String, Value>), - Union(String, Option<Value>), + Optional(Option<Box<SimpleValue>>), + List(Vec<SimpleValue>), + Record(BTreeMap<String, SimpleValue>), + Union(String, Option<Box<SimpleValue>>), } /// The type of a value that can be decoded by Serde, like `{ x: Bool, y: List Natural }`. @@ -91,48 +86,38 @@ pub enum SimpleType { Union(HashMap<String, Option<SimpleType>>), } -impl Value { - pub(crate) fn new(kind: ValKind) -> Self { - Value { - kind: Box::new(kind), - } - } +impl SimpleValue { pub(crate) fn from_nir(nir: &Nir) -> Option<Self> { - Some(Value::new(match nir.kind() { - NirKind::Num(lit) => ValKind::Num(lit.clone()), - NirKind::TextLit(x) => ValKind::Text( + Some(match nir.kind() { + NirKind::Num(lit) => SimpleValue::Num(lit.clone()), + NirKind::TextLit(x) => SimpleValue::Text( x.as_text() .expect("Normal form should ensure the text is a string"), ), - NirKind::EmptyOptionalLit(_) => ValKind::Optional(None), + NirKind::EmptyOptionalLit(_) => SimpleValue::Optional(None), NirKind::NEOptionalLit(x) => { - ValKind::Optional(Some(Self::from_nir(x)?)) + SimpleValue::Optional(Some(Box::new(Self::from_nir(x)?))) } - NirKind::EmptyListLit(_) => ValKind::List(vec![]), - NirKind::NEListLit(xs) => ValKind::List( - xs.iter() - .map(|v| Self::from_nir(v)) - .collect::<Option<_>>()?, + NirKind::EmptyListLit(_) => SimpleValue::List(vec![]), + NirKind::NEListLit(xs) => SimpleValue::List( + xs.iter().map(Self::from_nir).collect::<Option<_>>()?, ), - NirKind::RecordLit(kvs) => ValKind::Record( + NirKind::RecordLit(kvs) => SimpleValue::Record( kvs.iter() .map(|(k, v)| Some((k.into(), Self::from_nir(v)?))) .collect::<Option<_>>()?, ), - NirKind::UnionLit(field, x, _) => { - ValKind::Union(field.into(), Some(Self::from_nir(x)?)) - } + NirKind::UnionLit(field, x, _) => SimpleValue::Union( + field.into(), + Some(Box::new(Self::from_nir(x)?)), + ), NirKind::UnionConstructor(field, ty) if ty.get(field).map(|f| f.is_some()) == Some(false) => { - ValKind::Union(field.into(), None) + SimpleValue::Union(field.into(), None) } _ => return None, - })) - } - - pub fn kind(&self) -> &ValKind { - self.kind.as_ref() + }) } } @@ -174,8 +159,8 @@ impl SimpleType { }) } - pub(crate) fn to_value(&self) -> crate::value::Value { - crate::value::Value { + pub(crate) fn to_value(&self) -> Value { + Value { hir: self.to_hir(), as_simple_val: None, as_simple_ty: Some(self.clone()), @@ -212,10 +197,10 @@ impl SimpleType { } } -impl Sealed for Value {} +impl Sealed for SimpleValue {} -impl Deserialize for Value { - fn from_dhall(v: &crate::value::Value) -> Result<Self> { +impl Deserialize for SimpleValue { + fn from_dhall(v: &Value) -> Result<Self> { v.to_simple_value().ok_or_else(|| { Error::Deserialize(format!( "this cannot be deserialized into a simple type: {}", @@ -228,7 +213,7 @@ impl Deserialize for Value { impl Sealed for SimpleType {} impl Deserialize for SimpleType { - fn from_dhall(v: &crate::value::Value) -> Result<Self> { + fn from_dhall(v: &Value) -> Result<Self> { v.to_simple_type().ok_or_else(|| { Error::Deserialize(format!( "this cannot be deserialized into a simple type: {}", diff --git a/serde_dhall/src/value.rs b/serde_dhall/src/value.rs index e8aae49..ed932f5 100644 --- a/serde_dhall/src/value.rs +++ b/serde_dhall/src/value.rs @@ -1,7 +1,7 @@ use dhall::semantics::{Hir, Nir}; use dhall::syntax::Expr; -use crate::simple::{SimpleType, Value as SimpleValue}; +use crate::simple::{SimpleType, SimpleValue}; use crate::{Deserialize, Error, Sealed}; /// An arbitrary Dhall value. |