summaryrefslogtreecommitdiff
path: root/serde_dhall/src/options
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--serde_dhall/src/options/de.rs (renamed from serde_dhall/src/options.rs)12
-rw-r--r--serde_dhall/src/options/mod.rs2
-rw-r--r--serde_dhall/src/options/ser.rs67
3 files changed, 75 insertions, 6 deletions
diff --git a/serde_dhall/src/options.rs b/serde_dhall/src/options/de.rs
index 7b27114..8ff794d 100644
--- a/serde_dhall/src/options.rs
+++ b/serde_dhall/src/options/de.rs
@@ -19,20 +19,20 @@ pub struct ManualAnnot<'ty>(&'ty SimpleType);
#[derive(Debug, Clone, Copy)]
pub struct StaticAnnot;
-pub trait HasAnnot<A> {
+pub trait OptionalAnnot<A> {
fn get_annot(a: &A) -> Option<SimpleType>;
}
-impl<T> HasAnnot<NoAnnot> for T {
+impl<T> OptionalAnnot<NoAnnot> for T {
fn get_annot(_: &NoAnnot) -> Option<SimpleType> {
None
}
}
-impl<'ty, T> HasAnnot<ManualAnnot<'ty>> for T {
+impl<'ty, T> OptionalAnnot<ManualAnnot<'ty>> for T {
fn get_annot(a: &ManualAnnot<'ty>) -> Option<SimpleType> {
Some(a.0.clone())
}
}
-impl<T: StaticType> HasAnnot<StaticAnnot> for T {
+impl<T: StaticType> OptionalAnnot<StaticAnnot> for T {
fn get_annot(_: &StaticAnnot) -> Option<SimpleType> {
Some(T::static_type())
}
@@ -252,7 +252,7 @@ impl<'a, A> Deserializer<'a, A> {
fn _parse<T>(&self) -> dhall::error::Result<Value>
where
- T: HasAnnot<A>,
+ T: OptionalAnnot<A>,
{
let parsed = match &self.source {
Source::Str(s) => Parsed::parse_str(s)?,
@@ -287,7 +287,7 @@ impl<'a, A> Deserializer<'a, A> {
/// [`StaticType`]: trait.StaticType.html
pub fn parse<T>(&self) -> Result<T>
where
- T: FromDhall + HasAnnot<A>,
+ T: FromDhall + OptionalAnnot<A>,
{
let val = self
._parse::<T>()
diff --git a/serde_dhall/src/options/mod.rs b/serde_dhall/src/options/mod.rs
new file mode 100644
index 0000000..384f318
--- /dev/null
+++ b/serde_dhall/src/options/mod.rs
@@ -0,0 +1,2 @@
+pub(crate) mod de;
+pub(crate) mod ser;
diff --git a/serde_dhall/src/options/ser.rs b/serde_dhall/src/options/ser.rs
new file mode 100644
index 0000000..026dd21
--- /dev/null
+++ b/serde_dhall/src/options/ser.rs
@@ -0,0 +1,67 @@
+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()
+ }
+}
+
+#[derive(Debug, Clone)]
+pub struct Serializer<'a, T, A> {
+ data: &'a T,
+ annot: A,
+}
+
+impl<'a, T> Serializer<'a, T, NoAnnot> {
+ pub fn type_annotation<'ty>(
+ self,
+ ty: &'ty SimpleType,
+ ) -> Serializer<'a, T, ManualAnnot<'ty>> {
+ Serializer {
+ annot: ManualAnnot(ty),
+ data: self.data,
+ }
+ }
+
+ pub fn static_type_annotation(self) -> Serializer<'a, T, StaticAnnot> {
+ Serializer {
+ annot: StaticAnnot,
+ data: self.data,
+ }
+ }
+}
+
+impl<'a, T, A> Serializer<'a, T, A> {
+ pub fn to_string(&self) -> Result<String>
+ where
+ T: ToDhall + RequiredAnnot<A>,
+ {
+ let val = self.data.to_dhall(&T::get_annot(&self.annot))?;
+ Ok(val.to_string())
+ }
+}
+
+pub fn serialize<'a, T>(data: &'a T) -> Serializer<'a, T, NoAnnot>
+where
+ T: ToDhall,
+{
+ Serializer {
+ data,
+ annot: NoAnnot,
+ }
+}