summaryrefslogtreecommitdiff
path: root/serde_dhall/src/simple.rs
diff options
context:
space:
mode:
Diffstat (limited to 'serde_dhall/src/simple.rs')
-rw-r--r--serde_dhall/src/simple.rs195
1 files changed, 76 insertions, 119 deletions
diff --git a/serde_dhall/src/simple.rs b/serde_dhall/src/simple.rs
index 0b322ae..4cd4ab7 100644
--- a/serde_dhall/src/simple.rs
+++ b/serde_dhall/src/simple.rs
@@ -3,161 +3,114 @@ use std::collections::BTreeMap;
use dhall::semantics::{Hir, HirKind, Nir, NirKind};
use dhall::syntax::{Builtin, ExprKind, NumKind, Span};
-use crate::Error;
+use crate::{sealed::Sealed, Deserialize, Error, Result};
+/// A simple value of the kind that can be encoded/decoded with serde
#[derive(Debug, Clone, PartialEq, Eq)]
-pub struct SimpleValue {
- kind: Box<SValKind>,
+pub struct Value {
+ kind: Box<ValKind>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
-pub enum SValKind {
+pub enum ValKind {
+ // TODO: redefine NumKind locally
Num(NumKind),
Text(String),
- Optional(Option<SimpleValue>),
- List(Vec<SimpleValue>),
- Record(BTreeMap<String, SimpleValue>),
- Union(String, Option<SimpleValue>),
+ Optional(Option<Value>),
+ List(Vec<Value>),
+ // TODO: HashMap ?
+ Record(BTreeMap<String, Value>),
+ Union(String, Option<Value>),
}
+/// The type of a `simple::Value`.
#[derive(Debug, Clone, PartialEq, Eq)]
-pub struct SimpleType {
- kind: Box<STyKind>,
+pub struct Type {
+ kind: Box<TyKind>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
-pub enum STyKind {
+pub enum TyKind {
Bool,
Natural,
Integer,
Double,
Text,
- Optional(SimpleType),
- List(SimpleType),
- Record(BTreeMap<String, SimpleType>),
- Union(BTreeMap<String, Option<SimpleType>>),
+ Optional(Type),
+ List(Type),
+ Record(BTreeMap<String, Type>),
+ Union(BTreeMap<String, Option<Type>>),
}
-/// A Dhall value. This is a wrapper around [`dhall::SimpleValue`].
-#[derive(Debug, Clone, PartialEq, Eq)]
-pub struct Value(SimpleValue);
-
-/// A Dhall type. This is a wrapper around [`dhall::SimpleType`].
-#[derive(Debug, Clone, PartialEq, Eq)]
-pub struct Type(SimpleType);
-
impl Value {
- pub fn into_simple_value(self) -> SimpleValue {
- self.0
- }
-}
-
-impl Type {
- pub fn into_simple_type(self) -> SimpleType {
- self.0
- }
- pub fn to_value(&self) -> crate::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<Item = (String, Type)>) -> Self {
- Type::from_stykind(STyKind::Record(
- kts.map(|(k, t)| (k, t.0)).collect(),
- ))
- }
- #[doc(hidden)]
- pub fn make_union_type(
- kts: impl Iterator<Item = (String, Option<Type>)>,
- ) -> Self {
- Type::from_stykind(STyKind::Union(
- kts.map(|(k, t)| (k, t.map(|t| t.0))).collect(),
- ))
- }
-}
-
-impl SimpleValue {
- pub fn new(kind: SValKind) -> Self {
- SimpleValue {
+ pub(crate) fn new(kind: ValKind) -> Self {
+ Value {
kind: Box::new(kind),
}
}
- pub fn from_nir(nir: &Nir) -> Option<Self> {
- Some(SimpleValue::new(match nir.kind() {
- NirKind::Num(lit) => SValKind::Num(lit.clone()),
- NirKind::TextLit(x) => SValKind::Text(
+ 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(
x.as_text()
.expect("Normal form should ensure the text is a string"),
),
- NirKind::EmptyOptionalLit(_) => SValKind::Optional(None),
+ NirKind::EmptyOptionalLit(_) => ValKind::Optional(None),
NirKind::NEOptionalLit(x) => {
- SValKind::Optional(Some(Self::from_nir(x)?))
+ ValKind::Optional(Some(Self::from_nir(x)?))
}
- NirKind::EmptyListLit(_) => SValKind::List(vec![]),
- NirKind::NEListLit(xs) => SValKind::List(
+ NirKind::EmptyListLit(_) => ValKind::List(vec![]),
+ NirKind::NEListLit(xs) => ValKind::List(
xs.iter()
.map(|v| Self::from_nir(v))
.collect::<Option<_>>()?,
),
- NirKind::RecordLit(kvs) => SValKind::Record(
+ NirKind::RecordLit(kvs) => ValKind::Record(
kvs.iter()
.map(|(k, v)| Some((k.into(), Self::from_nir(v)?)))
.collect::<Option<_>>()?,
),
NirKind::UnionLit(field, x, _) => {
- SValKind::Union(field.into(), Some(Self::from_nir(x)?))
+ ValKind::Union(field.into(), Some(Self::from_nir(x)?))
}
NirKind::UnionConstructor(field, ty)
if ty.get(field).map(|f| f.is_some()) == Some(false) =>
{
- SValKind::Union(field.into(), None)
+ ValKind::Union(field.into(), None)
}
_ => return None,
}))
}
- pub fn kind(&self) -> &SValKind {
+ pub fn kind(&self) -> &ValKind {
self.kind.as_ref()
}
}
-impl SimpleType {
- pub fn new(kind: STyKind) -> Self {
- SimpleType {
+impl Type {
+ pub fn new(kind: TyKind) -> Self {
+ Type {
kind: Box::new(kind),
}
}
- pub fn from_nir(nir: &Nir) -> Option<Self> {
- Some(SimpleType::new(match nir.kind() {
+ pub(crate) fn from_nir(nir: &Nir) -> Option<Self> {
+ Some(Type::new(match nir.kind() {
NirKind::BuiltinType(b) => match b {
- Builtin::Bool => STyKind::Bool,
- Builtin::Natural => STyKind::Natural,
- Builtin::Integer => STyKind::Integer,
- Builtin::Double => STyKind::Double,
- Builtin::Text => STyKind::Text,
+ Builtin::Bool => TyKind::Bool,
+ Builtin::Natural => TyKind::Natural,
+ Builtin::Integer => TyKind::Integer,
+ Builtin::Double => TyKind::Double,
+ Builtin::Text => TyKind::Text,
_ => unreachable!(),
},
- NirKind::OptionalType(t) => STyKind::Optional(Self::from_nir(t)?),
- NirKind::ListType(t) => STyKind::List(Self::from_nir(t)?),
- NirKind::RecordType(kts) => STyKind::Record(
+ NirKind::OptionalType(t) => TyKind::Optional(Self::from_nir(t)?),
+ NirKind::ListType(t) => TyKind::List(Self::from_nir(t)?),
+ NirKind::RecordType(kts) => TyKind::Record(
kts.iter()
.map(|(k, v)| Some((k.into(), Self::from_nir(v)?)))
.collect::<Option<_>>()?,
),
- NirKind::UnionType(kts) => STyKind::Union(
+ NirKind::UnionType(kts) => TyKind::Union(
kts.iter()
.map(|(k, v)| {
Some((
@@ -173,37 +126,37 @@ impl SimpleType {
}))
}
- pub fn kind(&self) -> &STyKind {
+ pub fn kind(&self) -> &TyKind {
self.kind.as_ref()
}
- pub fn to_value(&self) -> crate::Value {
- crate::Value {
+ pub fn to_value(&self) -> crate::value::Value {
+ crate::value::Value {
hir: self.to_hir(),
as_simple_val: None,
as_simple_ty: Some(self.clone()),
}
}
- pub fn to_hir(&self) -> Hir {
+ pub(crate) fn to_hir(&self) -> Hir {
let hir = |k| Hir::new(HirKind::Expr(k), Span::Artificial);
hir(match self.kind() {
- STyKind::Bool => ExprKind::Builtin(Builtin::Bool),
- STyKind::Natural => ExprKind::Builtin(Builtin::Natural),
- STyKind::Integer => ExprKind::Builtin(Builtin::Integer),
- STyKind::Double => ExprKind::Builtin(Builtin::Double),
- STyKind::Text => ExprKind::Builtin(Builtin::Text),
- STyKind::Optional(t) => ExprKind::App(
+ TyKind::Bool => ExprKind::Builtin(Builtin::Bool),
+ TyKind::Natural => ExprKind::Builtin(Builtin::Natural),
+ TyKind::Integer => ExprKind::Builtin(Builtin::Integer),
+ TyKind::Double => ExprKind::Builtin(Builtin::Double),
+ TyKind::Text => ExprKind::Builtin(Builtin::Text),
+ TyKind::Optional(t) => ExprKind::App(
hir(ExprKind::Builtin(Builtin::Optional)),
t.to_hir(),
),
- STyKind::List(t) => {
+ TyKind::List(t) => {
ExprKind::App(hir(ExprKind::Builtin(Builtin::List)), t.to_hir())
}
- STyKind::Record(kts) => ExprKind::RecordType(
+ TyKind::Record(kts) => ExprKind::RecordType(
kts.into_iter()
.map(|(k, t)| (k.as_str().into(), t.to_hir()))
.collect(),
),
- STyKind::Union(kts) => ExprKind::UnionType(
+ TyKind::Union(kts) => ExprKind::UnionType(
kts.into_iter()
.map(|(k, t)| {
(k.as_str().into(), t.as_ref().map(|t| t.to_hir()))
@@ -214,30 +167,34 @@ impl SimpleType {
}
}
-impl super::sealed::Sealed for Value {}
+impl Sealed for Value {}
-impl super::Deserialize for Value {
- fn from_dhall(v: &crate::Value) -> super::Result<Self> {
- let sval = v.to_simple_value().ok_or_else(|| {
+impl Deserialize for Value {
+ fn from_dhall(v: &crate::value::Value) -> Result<Self> {
+ 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 Sealed for Type {}
-impl super::Deserialize for Type {
- fn from_dhall(v: &crate::Value) -> super::Result<Self> {
- let sty = v.to_simple_type().ok_or_else(|| {
+impl Deserialize for Type {
+ fn from_dhall(v: &crate::value::Value) -> Result<Self> {
+ v.to_simple_type().ok_or_else(|| {
Error::Deserialize(format!(
"this cannot be deserialized into a simple type: {}",
v
))
- })?;
- Ok(Type(sty))
+ })
+ }
+}
+
+impl From<TyKind> for Type {
+ fn from(x: TyKind) -> Type {
+ Type::new(x)
}
}