From e070270c3f1f10d46281ed7751ff95e15092e7f4 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 10 May 2020 22:09:43 +0100 Subject: Implement serialization --- serde_dhall/src/deserialize.rs | 16 +- serde_dhall/src/error.rs | 11 ++ serde_dhall/src/lib.rs | 6 +- serde_dhall/src/options.rs | 372 ----------------------------------------- serde_dhall/src/options/de.rs | 372 +++++++++++++++++++++++++++++++++++++++++ serde_dhall/src/options/mod.rs | 2 + serde_dhall/src/options/ser.rs | 67 ++++++++ serde_dhall/src/serialize.rs | 312 ++++++++++++++++++++++++++++++++++ serde_dhall/src/static_type.rs | 60 +++++++ serde_dhall/src/value.rs | 106 +++++++++++- serde_dhall/tests/de.rs | 59 +------ serde_dhall/tests/serde.rs | 145 ++++++++++++++++ 12 files changed, 1090 insertions(+), 438 deletions(-) delete mode 100644 serde_dhall/src/options.rs create mode 100644 serde_dhall/src/options/de.rs create mode 100644 serde_dhall/src/options/mod.rs create mode 100644 serde_dhall/src/options/ser.rs create mode 100644 serde_dhall/src/serialize.rs create mode 100644 serde_dhall/tests/serde.rs (limited to 'serde_dhall') diff --git a/serde_dhall/src/deserialize.rs b/serde_dhall/src/deserialize.rs index 12b4703..0e518ea 100644 --- a/serde_dhall/src/deserialize.rs +++ b/serde_dhall/src/deserialize.rs @@ -47,8 +47,6 @@ pub trait FromDhall: Sealed + Sized { impl Sealed for T where T: serde::de::DeserializeOwned {} -struct Deserializer<'a>(Cow<'a, SimpleValue>); - /// Deserialize a Rust value from a Dhall [`SimpleValue`]. /// /// # Example @@ -109,6 +107,8 @@ where } } +struct Deserializer<'a>(Cow<'a, SimpleValue>); + impl<'de: 'a, 'a> serde::de::IntoDeserializer<'de, Error> for Deserializer<'a> { type Deserializer = Deserializer<'a>; fn into_deserializer(self) -> Self::Deserializer { @@ -171,9 +171,19 @@ impl<'de: 'a, 'a> serde::Deserializer<'de> for Deserializer<'a> { } } + fn deserialize_unit(self, visitor: V) -> crate::Result + where + V: serde::de::Visitor<'de>, + { + match self.0.as_ref() { + SimpleValue::Record(m) if m.is_empty() => visitor.visit_unit(), + _ => 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 + bytes byte_buf option unit_struct newtype_struct seq tuple_struct map struct enum identifier ignored_any } } diff --git a/serde_dhall/src/error.rs b/serde_dhall/src/error.rs index 896e8b9..3309fe4 100644 --- a/serde_dhall/src/error.rs +++ b/serde_dhall/src/error.rs @@ -11,6 +11,7 @@ pub struct Error(pub(crate) ErrorKind); pub(crate) enum ErrorKind { Dhall(DhallError), Deserialize(String), + Serialize(String), } impl From for Error { @@ -24,6 +25,7 @@ impl std::fmt::Display for Error { match &self.0 { ErrorKind::Dhall(err) => write!(f, "{}", err), ErrorKind::Deserialize(err) => write!(f, "{}", err), + ErrorKind::Serialize(err) => write!(f, "{}", err), } } } @@ -38,3 +40,12 @@ impl serde::de::Error for Error { ErrorKind::Deserialize(msg.to_string()).into() } } + +impl serde::ser::Error for Error { + fn custom(msg: T) -> Self + where + T: std::fmt::Display, + { + ErrorKind::Serialize(msg.to_string()).into() + } +} diff --git a/serde_dhall/src/lib.rs b/serde_dhall/src/lib.rs index 8291f74..e236e37 100644 --- a/serde_dhall/src/lib.rs +++ b/serde_dhall/src/lib.rs @@ -169,6 +169,7 @@ mod test_readme { mod deserialize; mod error; mod options; +mod serialize; mod static_type; /// Dhall values mod value; @@ -176,10 +177,11 @@ mod value; #[doc(hidden)] pub use dhall_proc_macros::StaticType; -pub(crate) use deserialize::Sealed; pub use deserialize::{from_simple_value, FromDhall}; pub(crate) use error::ErrorKind; pub use error::{Error, Result}; -pub use options::{from_file, from_str, Deserializer}; +pub use options::de::{from_file, from_str, Deserializer}; +pub use options::ser::{serialize, Serializer}; +pub use serialize::ToDhall; pub use static_type::StaticType; pub use value::{NumKind, SimpleType, SimpleValue, Value}; diff --git a/serde_dhall/src/options.rs b/serde_dhall/src/options.rs deleted file mode 100644 index 7b27114..0000000 --- a/serde_dhall/src/options.rs +++ /dev/null @@ -1,372 +0,0 @@ -use std::path::{Path, PathBuf}; - -use dhall::Parsed; - -use crate::SimpleType; -use crate::{Error, ErrorKind, FromDhall, Result, StaticType, Value}; - -#[derive(Debug, Clone)] -enum Source<'a> { - Str(&'a str), - File(PathBuf), - // 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 { - fn get_annot(a: &A) -> Option; -} -impl HasAnnot for T { - fn get_annot(_: &NoAnnot) -> Option { - None - } -} -impl<'ty, T> HasAnnot> for T { - fn get_annot(a: &ManualAnnot<'ty>) -> Option { - Some(a.0.clone()) - } -} -impl HasAnnot for T { - fn get_annot(_: &StaticAnnot) -> Option { - 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 -/// are permitted during evaluation. -/// -/// Generally speaking, when using [`Deserializer`], you'll create it with [`from_str`] or [`from_file`], then -/// chain calls to methods to set each option, then call [`parse`]. This will give you a -/// [`Result`] where `T` is a deserializable type of your choice. -/// -/// [`Deserializer`]: struct.Deserializer.html -/// [`from_str`]: fn.from_str.html -/// [`from_file`]: fn.from_file.html -/// [`parse`]: struct.Deserializer.html#method.parse -/// [`Result`]: type.Result.html -/// -/// # Examples -/// -/// Reading from a file: -/// -/// ```no_run -/// # fn main() -> serde_dhall::Result<()> { -/// use serde_dhall::from_file; -/// -/// let data = from_file("foo.dhall").parse::()?; -/// # Ok(()) -/// # } -/// ``` -/// -/// Reading from a file and checking the value against a provided type: -/// -/// ```no_run -/// # fn main() -> serde_dhall::Result<()> { -/// use std::collections::HashMap; -/// use serde_dhall::{from_file, from_str}; -/// -/// let ty = from_str("{ x: Natural, y: Natural }").parse()?; -/// let data = from_file("foo.dhall") -/// .type_annotation(&ty) -/// .parse::>()?; -/// # Ok(()) -/// # } -/// ``` -#[derive(Debug, Clone)] -pub struct Deserializer<'a, A> { - source: Source<'a>, - annot: A, - allow_imports: bool, - // allow_remote_imports: bool, - // use_cache: bool, -} - -impl<'a> Deserializer<'a, NoAnnot> { - fn default_with_source(source: Source<'a>) -> Self { - Deserializer { - source, - annot: NoAnnot, - allow_imports: true, - // allow_remote_imports: true, - // use_cache: true, - } - } - fn from_str(s: &'a str) -> Self { - Self::default_with_source(Source::Str(s)) - } - fn from_file>(path: P) -> Self { - Self::default_with_source(Source::File(path.as_ref().to_owned())) - } - // fn from_url(url: &'a str) -> Self { - // Self::default_with_source(Source::Url(url)) - // } - - /// Ensures that the parsed value matches the provided type. - /// - /// In many cases the Dhall type that corresponds to a Rust type can be inferred automatically. - /// See the [`StaticType`] trait and the [`static_type_annotation`] method for that. - /// - /// # Example - /// - /// ``` - /// # fn main() -> serde_dhall::Result<()> { - /// use std::collections::HashMap; - /// use serde::Deserialize; - /// use serde_dhall::{from_str, SimpleType}; - /// - /// // Parse a Dhall type - /// let type_str = "{ x: Natural, y: Natural }"; - /// let ty = from_str(type_str).parse::()?; - /// - /// // Parse some Dhall data. - /// let data = "{ x = 1, y = 1 + 1 }"; - /// let point = from_str(data) - /// .type_annotation(&ty) - /// .parse::>()?; - /// assert_eq!(point.get("y"), Some(&2)); - /// - /// // Invalid data fails the type validation; deserialization would have succeeded otherwise. - /// let invalid_data = "{ x = 1, z = 3 }"; - /// assert!( - /// from_str(invalid_data) - /// .type_annotation(&ty) - /// .parse::>() - /// .is_err() - /// ); - /// # Ok(()) - /// # } - /// ``` - /// - /// [`static_type_annotation`]: struct.Deserializer.html#method.static_type_annotation - /// [`StaticType`]: trait.StaticType.html - pub fn type_annotation<'ty>( - self, - ty: &'ty SimpleType, - ) -> Deserializer<'a, ManualAnnot<'ty>> { - Deserializer { - annot: ManualAnnot(ty), - source: self.source, - allow_imports: self.allow_imports, - } - } - - /// Ensures that the parsed value matches the type of `T`. - /// - /// `T` must implement the [`StaticType`] trait. If it doesn't, you can use [`type_annotation`] - /// to provide a type manually. - /// - /// # Example - /// - /// ``` - /// # fn main() -> serde_dhall::Result<()> { - /// use serde::Deserialize; - /// use serde_dhall::StaticType; - /// - /// #[derive(Deserialize, StaticType)] - /// struct Point { - /// x: u64, - /// y: Option, - /// } - /// - /// // Some Dhall data - /// let data = "{ x = 1, y = Some (1 + 1) }"; - /// - /// // Convert the Dhall string to a Point. - /// let point = serde_dhall::from_str(data) - /// .static_type_annotation() - /// .parse::()?; - /// assert_eq!(point.x, 1); - /// assert_eq!(point.y, Some(2)); - /// - /// // Invalid data fails the type validation; deserialization would have succeeded otherwise. - /// let invalid_data = "{ x = 1 }"; - /// assert!( - /// serde_dhall::from_str(invalid_data) - /// .static_type_annotation() - /// .parse::() - /// .is_err() - /// ); - /// # Ok(()) - /// # } - /// ``` - /// - /// [`type_annotation`]: struct.Deserializer.html#method.type_annotation - /// [`StaticType`]: trait.StaticType.html - pub fn static_type_annotation(self) -> Deserializer<'a, StaticAnnot> { - Deserializer { - annot: StaticAnnot, - source: self.source, - allow_imports: self.allow_imports, - } - } -} - -impl<'a, A> Deserializer<'a, A> { - /// Sets whether to enable imports. - /// - /// By default, imports are enabled. - /// - /// # Example - /// - /// ``` - /// # fn main() -> serde_dhall::Result<()> { - /// use serde::Deserialize; - /// use serde_dhall::SimpleType; - /// - /// let data = "12 + ./other_file.dhall : Natural"; - /// assert!( - /// serde_dhall::from_str(data) - /// .imports(false) - /// .parse::() - /// .is_err() - /// ); - /// # Ok(()) - /// # } - /// ``` - /// - /// [`static_type_annotation`]: struct.Deserializer.html#method.static_type_annotation - /// [`StaticType`]: trait.StaticType.html - pub fn imports(self, imports: bool) -> Self { - Deserializer { - allow_imports: imports, - ..self - } - } - - // /// TODO - // pub fn remote_imports(&mut self, imports: bool) -> &mut Self { - // self.allow_remote_imports = imports; - // if imports { - // self.allow_imports = true; - // } - // self - // } - - fn _parse(&self) -> dhall::error::Result - where - T: HasAnnot, - { - let parsed = match &self.source { - Source::Str(s) => Parsed::parse_str(s)?, - Source::File(p) => Parsed::parse_file(p.as_ref())?, - }; - let resolved = if self.allow_imports { - parsed.resolve()? - } else { - parsed.skip_resolve()? - }; - let typed = match &T::get_annot(&self.annot) { - None => resolved.typecheck()?, - Some(ty) => resolved.typecheck_with(ty.to_value().as_hir())?, - }; - Ok(Value::from_nir(typed.normalize().as_nir())) - } - - /// Parses the chosen dhall value with the options provided. - /// - /// If you enabled static annotations, `T` is required to implement [`StaticType`]. - /// - /// - /// # Example - /// - /// ``` - /// # fn main() -> serde_dhall::Result<()> { - /// let data = serde_dhall::from_str("6 * 7").parse::()?; - /// assert_eq!(data, 42); - /// # Ok(()) - /// # } - /// ``` - /// [`StaticType`]: trait.StaticType.html - pub fn parse(&self) -> Result - where - T: FromDhall + HasAnnot, - { - let val = self - ._parse::() - .map_err(ErrorKind::Dhall) - .map_err(Error)?; - T::from_dhall(&val) - } -} - -/// Deserialize a value from a string of Dhall text. -/// -/// This returns a [`Deserializer`] object. Call the [`parse`] method to get the deserialized -/// value, or use other [`Deserializer`] methods to control the deserialization process. -/// -/// Imports will be resolved relative to the current directory. -/// -/// # Example -/// -/// ```rust -/// # fn main() -> serde_dhall::Result<()> { -/// use serde::Deserialize; -/// -/// // We use serde's derive feature -/// #[derive(Deserialize)] -/// struct Point { -/// x: u64, -/// y: u64, -/// } -/// -/// // Some Dhall data -/// let data = "{ x = 1, y = 1 + 1 } : { x: Natural, y: Natural }"; -/// -/// // Parse the Dhall string as a Point. -/// let point: Point = serde_dhall::from_str(data).parse()?; -/// -/// assert_eq!(point.x, 1); -/// assert_eq!(point.y, 2); -/// # Ok(()) -/// # } -/// ``` -/// -/// [`Deserializer`]: struct.Deserializer.html -/// [`parse`]: struct.Deserializer.html#method.parse -pub fn from_str(s: &str) -> Deserializer<'_, NoAnnot> { - Deserializer::from_str(s) -} - -/// Deserialize a value from a Dhall file. -/// -/// This returns a [`Deserializer`] object. Call the [`parse`] method to get the deserialized -/// value, or use other [`Deserializer`] methods to control the deserialization process. -/// -/// Imports will be resolved relative to the provided file's path. -/// -/// # Example -/// -/// ```no_run -/// # fn main() -> serde_dhall::Result<()> { -/// use serde::Deserialize; -/// -/// // We use serde's derive feature -/// #[derive(Deserialize)] -/// struct Point { -/// x: u64, -/// y: u64, -/// } -/// -/// // Parse the Dhall file as a Point. -/// let point: Point = serde_dhall::from_file("foo.dhall").parse()?; -/// # Ok(()) -/// # } -/// ``` -/// -/// [`Deserializer`]: struct.Deserializer.html -/// [`parse`]: struct.Deserializer.html#method.parse -pub fn from_file<'a, P: AsRef>(path: P) -> Deserializer<'a, NoAnnot> { - Deserializer::from_file(path) -} - -// pub fn from_url(url: &str) -> Deserializer<'_, NoAnnot> { -// Deserializer::from_url(url) -// } diff --git a/serde_dhall/src/options/de.rs b/serde_dhall/src/options/de.rs new file mode 100644 index 0000000..8ff794d --- /dev/null +++ b/serde_dhall/src/options/de.rs @@ -0,0 +1,372 @@ +use std::path::{Path, PathBuf}; + +use dhall::Parsed; + +use crate::SimpleType; +use crate::{Error, ErrorKind, FromDhall, Result, StaticType, Value}; + +#[derive(Debug, Clone)] +enum Source<'a> { + Str(&'a str), + File(PathBuf), + // 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 OptionalAnnot { + fn get_annot(a: &A) -> Option; +} +impl OptionalAnnot for T { + fn get_annot(_: &NoAnnot) -> Option { + None + } +} +impl<'ty, T> OptionalAnnot> for T { + fn get_annot(a: &ManualAnnot<'ty>) -> Option { + Some(a.0.clone()) + } +} +impl OptionalAnnot for T { + fn get_annot(_: &StaticAnnot) -> Option { + 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 +/// are permitted during evaluation. +/// +/// Generally speaking, when using [`Deserializer`], you'll create it with [`from_str`] or [`from_file`], then +/// chain calls to methods to set each option, then call [`parse`]. This will give you a +/// [`Result`] where `T` is a deserializable type of your choice. +/// +/// [`Deserializer`]: struct.Deserializer.html +/// [`from_str`]: fn.from_str.html +/// [`from_file`]: fn.from_file.html +/// [`parse`]: struct.Deserializer.html#method.parse +/// [`Result`]: type.Result.html +/// +/// # Examples +/// +/// Reading from a file: +/// +/// ```no_run +/// # fn main() -> serde_dhall::Result<()> { +/// use serde_dhall::from_file; +/// +/// let data = from_file("foo.dhall").parse::()?; +/// # Ok(()) +/// # } +/// ``` +/// +/// Reading from a file and checking the value against a provided type: +/// +/// ```no_run +/// # fn main() -> serde_dhall::Result<()> { +/// use std::collections::HashMap; +/// use serde_dhall::{from_file, from_str}; +/// +/// let ty = from_str("{ x: Natural, y: Natural }").parse()?; +/// let data = from_file("foo.dhall") +/// .type_annotation(&ty) +/// .parse::>()?; +/// # Ok(()) +/// # } +/// ``` +#[derive(Debug, Clone)] +pub struct Deserializer<'a, A> { + source: Source<'a>, + annot: A, + allow_imports: bool, + // allow_remote_imports: bool, + // use_cache: bool, +} + +impl<'a> Deserializer<'a, NoAnnot> { + fn default_with_source(source: Source<'a>) -> Self { + Deserializer { + source, + annot: NoAnnot, + allow_imports: true, + // allow_remote_imports: true, + // use_cache: true, + } + } + fn from_str(s: &'a str) -> Self { + Self::default_with_source(Source::Str(s)) + } + fn from_file>(path: P) -> Self { + Self::default_with_source(Source::File(path.as_ref().to_owned())) + } + // fn from_url(url: &'a str) -> Self { + // Self::default_with_source(Source::Url(url)) + // } + + /// Ensures that the parsed value matches the provided type. + /// + /// In many cases the Dhall type that corresponds to a Rust type can be inferred automatically. + /// See the [`StaticType`] trait and the [`static_type_annotation`] method for that. + /// + /// # Example + /// + /// ``` + /// # fn main() -> serde_dhall::Result<()> { + /// use std::collections::HashMap; + /// use serde::Deserialize; + /// use serde_dhall::{from_str, SimpleType}; + /// + /// // Parse a Dhall type + /// let type_str = "{ x: Natural, y: Natural }"; + /// let ty = from_str(type_str).parse::()?; + /// + /// // Parse some Dhall data. + /// let data = "{ x = 1, y = 1 + 1 }"; + /// let point = from_str(data) + /// .type_annotation(&ty) + /// .parse::>()?; + /// assert_eq!(point.get("y"), Some(&2)); + /// + /// // Invalid data fails the type validation; deserialization would have succeeded otherwise. + /// let invalid_data = "{ x = 1, z = 3 }"; + /// assert!( + /// from_str(invalid_data) + /// .type_annotation(&ty) + /// .parse::>() + /// .is_err() + /// ); + /// # Ok(()) + /// # } + /// ``` + /// + /// [`static_type_annotation`]: struct.Deserializer.html#method.static_type_annotation + /// [`StaticType`]: trait.StaticType.html + pub fn type_annotation<'ty>( + self, + ty: &'ty SimpleType, + ) -> Deserializer<'a, ManualAnnot<'ty>> { + Deserializer { + annot: ManualAnnot(ty), + source: self.source, + allow_imports: self.allow_imports, + } + } + + /// Ensures that the parsed value matches the type of `T`. + /// + /// `T` must implement the [`StaticType`] trait. If it doesn't, you can use [`type_annotation`] + /// to provide a type manually. + /// + /// # Example + /// + /// ``` + /// # fn main() -> serde_dhall::Result<()> { + /// use serde::Deserialize; + /// use serde_dhall::StaticType; + /// + /// #[derive(Deserialize, StaticType)] + /// struct Point { + /// x: u64, + /// y: Option, + /// } + /// + /// // Some Dhall data + /// let data = "{ x = 1, y = Some (1 + 1) }"; + /// + /// // Convert the Dhall string to a Point. + /// let point = serde_dhall::from_str(data) + /// .static_type_annotation() + /// .parse::()?; + /// assert_eq!(point.x, 1); + /// assert_eq!(point.y, Some(2)); + /// + /// // Invalid data fails the type validation; deserialization would have succeeded otherwise. + /// let invalid_data = "{ x = 1 }"; + /// assert!( + /// serde_dhall::from_str(invalid_data) + /// .static_type_annotation() + /// .parse::() + /// .is_err() + /// ); + /// # Ok(()) + /// # } + /// ``` + /// + /// [`type_annotation`]: struct.Deserializer.html#method.type_annotation + /// [`StaticType`]: trait.StaticType.html + pub fn static_type_annotation(self) -> Deserializer<'a, StaticAnnot> { + Deserializer { + annot: StaticAnnot, + source: self.source, + allow_imports: self.allow_imports, + } + } +} + +impl<'a, A> Deserializer<'a, A> { + /// Sets whether to enable imports. + /// + /// By default, imports are enabled. + /// + /// # Example + /// + /// ``` + /// # fn main() -> serde_dhall::Result<()> { + /// use serde::Deserialize; + /// use serde_dhall::SimpleType; + /// + /// let data = "12 + ./other_file.dhall : Natural"; + /// assert!( + /// serde_dhall::from_str(data) + /// .imports(false) + /// .parse::() + /// .is_err() + /// ); + /// # Ok(()) + /// # } + /// ``` + /// + /// [`static_type_annotation`]: struct.Deserializer.html#method.static_type_annotation + /// [`StaticType`]: trait.StaticType.html + pub fn imports(self, imports: bool) -> Self { + Deserializer { + allow_imports: imports, + ..self + } + } + + // /// TODO + // pub fn remote_imports(&mut self, imports: bool) -> &mut Self { + // self.allow_remote_imports = imports; + // if imports { + // self.allow_imports = true; + // } + // self + // } + + fn _parse(&self) -> dhall::error::Result + where + T: OptionalAnnot, + { + let parsed = match &self.source { + Source::Str(s) => Parsed::parse_str(s)?, + Source::File(p) => Parsed::parse_file(p.as_ref())?, + }; + let resolved = if self.allow_imports { + parsed.resolve()? + } else { + parsed.skip_resolve()? + }; + let typed = match &T::get_annot(&self.annot) { + None => resolved.typecheck()?, + Some(ty) => resolved.typecheck_with(ty.to_value().as_hir())?, + }; + Ok(Value::from_nir(typed.normalize().as_nir())) + } + + /// Parses the chosen dhall value with the options provided. + /// + /// If you enabled static annotations, `T` is required to implement [`StaticType`]. + /// + /// + /// # Example + /// + /// ``` + /// # fn main() -> serde_dhall::Result<()> { + /// let data = serde_dhall::from_str("6 * 7").parse::()?; + /// assert_eq!(data, 42); + /// # Ok(()) + /// # } + /// ``` + /// [`StaticType`]: trait.StaticType.html + pub fn parse(&self) -> Result + where + T: FromDhall + OptionalAnnot, + { + let val = self + ._parse::() + .map_err(ErrorKind::Dhall) + .map_err(Error)?; + T::from_dhall(&val) + } +} + +/// Deserialize a value from a string of Dhall text. +/// +/// This returns a [`Deserializer`] object. Call the [`parse`] method to get the deserialized +/// value, or use other [`Deserializer`] methods to control the deserialization process. +/// +/// Imports will be resolved relative to the current directory. +/// +/// # Example +/// +/// ```rust +/// # fn main() -> serde_dhall::Result<()> { +/// use serde::Deserialize; +/// +/// // We use serde's derive feature +/// #[derive(Deserialize)] +/// struct Point { +/// x: u64, +/// y: u64, +/// } +/// +/// // Some Dhall data +/// let data = "{ x = 1, y = 1 + 1 } : { x: Natural, y: Natural }"; +/// +/// // Parse the Dhall string as a Point. +/// let point: Point = serde_dhall::from_str(data).parse()?; +/// +/// assert_eq!(point.x, 1); +/// assert_eq!(point.y, 2); +/// # Ok(()) +/// # } +/// ``` +/// +/// [`Deserializer`]: struct.Deserializer.html +/// [`parse`]: struct.Deserializer.html#method.parse +pub fn from_str(s: &str) -> Deserializer<'_, NoAnnot> { + Deserializer::from_str(s) +} + +/// Deserialize a value from a Dhall file. +/// +/// This returns a [`Deserializer`] object. Call the [`parse`] method to get the deserialized +/// value, or use other [`Deserializer`] methods to control the deserialization process. +/// +/// Imports will be resolved relative to the provided file's path. +/// +/// # Example +/// +/// ```no_run +/// # fn main() -> serde_dhall::Result<()> { +/// use serde::Deserialize; +/// +/// // We use serde's derive feature +/// #[derive(Deserialize)] +/// struct Point { +/// x: u64, +/// y: u64, +/// } +/// +/// // Parse the Dhall file as a Point. +/// let point: Point = serde_dhall::from_file("foo.dhall").parse()?; +/// # Ok(()) +/// # } +/// ``` +/// +/// [`Deserializer`]: struct.Deserializer.html +/// [`parse`]: struct.Deserializer.html#method.parse +pub fn from_file<'a, P: AsRef>(path: P) -> Deserializer<'a, NoAnnot> { + Deserializer::from_file(path) +} + +// pub fn from_url(url: &str) -> Deserializer<'_, NoAnnot> { +// Deserializer::from_url(url) +// } 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 { + fn get_annot(a: &A) -> SimpleType; +} +impl<'ty, T> RequiredAnnot> for T { + fn get_annot(a: &ManualAnnot<'ty>) -> SimpleType { + a.0.clone() + } +} +impl RequiredAnnot 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 + where + T: ToDhall + RequiredAnnot, + { + 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, + } +} diff --git a/serde_dhall/src/serialize.rs b/serde_dhall/src/serialize.rs new file mode 100644 index 0000000..dd3e426 --- /dev/null +++ b/serde_dhall/src/serialize.rs @@ -0,0 +1,312 @@ +use serde::ser; +use std::collections::BTreeMap; + +use dhall::syntax::NumKind; + +use crate::value::SimpleValue; +use crate::{Error, ErrorKind, Result, SimpleType, Value}; +use SimpleValue::*; + +pub trait Sealed {} + +pub trait ToDhall: Sealed { + #[doc(hidden)] + fn to_dhall(&self, ty: &SimpleType) -> Result; +} + +impl Sealed for T where T: ser::Serialize {} + +impl ToDhall for T +where + T: ser::Serialize, +{ + fn to_dhall(&self, ty: &SimpleType) -> Result { + let sval: SimpleValue = self.serialize(Serializer)?; + sval.into_value(ty) + } +} + +#[derive(Default, Clone, Copy)] +struct Serializer; + +impl ser::Serializer for Serializer { + type Ok = SimpleValue; + type Error = Error; + + type SerializeSeq = SeqSerializer; + type SerializeTuple = TupleSerializer; + type SerializeTupleStruct = ser::Impossible; + type SerializeTupleVariant = ser::Impossible; + type SerializeMap = MapSerializer; + type SerializeStruct = StructSerializer; + type SerializeStructVariant = ser::Impossible; + + fn serialize_bool(self, v: bool) -> Result { + Ok(Num(NumKind::Bool(v))) + } + + fn serialize_i8(self, v: i8) -> Result { + self.serialize_i64(i64::from(v)) + } + fn serialize_i16(self, v: i16) -> Result { + self.serialize_i64(i64::from(v)) + } + fn serialize_i32(self, v: i32) -> Result { + self.serialize_i64(i64::from(v)) + } + fn serialize_i64(self, v: i64) -> Result { + Ok(Num(NumKind::Integer(v))) + } + + fn serialize_u8(self, v: u8) -> Result { + self.serialize_u64(u64::from(v)) + } + fn serialize_u16(self, v: u16) -> Result { + self.serialize_u64(u64::from(v)) + } + fn serialize_u32(self, v: u32) -> Result { + self.serialize_u64(u64::from(v)) + } + fn serialize_u64(self, v: u64) -> Result { + Ok(Num(NumKind::Natural(v))) + } + + fn serialize_f32(self, v: f32) -> Result { + self.serialize_f64(f64::from(v)) + } + fn serialize_f64(self, v: f64) -> Result { + Ok(Num(NumKind::Double(v.into()))) + } + + fn serialize_char(self, v: char) -> Result { + self.serialize_str(&v.to_string()) + } + fn serialize_str(self, v: &str) -> Result { + Ok(Text(v.to_owned())) + } + + fn serialize_bytes(self, _v: &[u8]) -> Result { + Err(ErrorKind::Serialize( + "Unsupported data for serialization: byte array".to_owned(), + ) + .into()) + } + + fn serialize_none(self) -> Result { + Ok(Optional(None)) + } + fn serialize_some(self, v: &T) -> Result + where + T: ?Sized + ser::Serialize, + { + Ok(Optional(Some(Box::new(v.serialize(self)?)))) + } + + fn serialize_unit(self) -> Result { + Ok(Record(Default::default())) + } + + fn serialize_unit_struct(self, _name: &'static str) -> Result { + self.serialize_unit() + } + fn serialize_newtype_struct( + self, + _name: &'static str, + _value: &T, + ) -> Result + where + T: ?Sized + ser::Serialize, + { + Err(ErrorKind::Serialize( + "Unsupported data for serialization: newtype struct".to_owned(), + ) + .into()) + } + fn serialize_struct( + self, + _name: &'static str, + _len: usize, + ) -> Result { + Ok(StructSerializer::default()) + } + + fn serialize_unit_variant( + self, + _name: &'static str, + _variant_index: u32, + variant: &'static str, + ) -> Result { + Ok(Union(variant.to_owned(), None)) + } + fn serialize_newtype_variant( + self, + _name: &'static str, + _variant_index: u32, + variant: &'static str, + value: &T, + ) -> Result + where + T: ?Sized + ser::Serialize, + { + let value = value.serialize(self)?; + Ok(Union(variant.to_owned(), Some(Box::new(value)))) + } + fn serialize_tuple_variant( + self, + _name: &'static str, + _variant_index: u32, + _variant: &'static str, + _len: usize, + ) -> Result { + Err(ErrorKind::Serialize( + "Unsupported data for serialization: tuple variant".to_owned(), + ) + .into()) + } + fn serialize_struct_variant( + self, + _name: &'static str, + _variant_index: u32, + _variant: &'static str, + _len: usize, + ) -> Result { + Err(ErrorKind::Serialize( + "Unsupported data for serialization: struct variant".to_owned(), + ) + .into()) + } + + fn serialize_tuple(self, _len: usize) -> Result { + Ok(TupleSerializer::default()) + } + fn serialize_tuple_struct( + self, + _name: &'static str, + _len: usize, + ) -> Result { + Err(ErrorKind::Serialize( + "Unsupported data for serialization: tuple struct".to_owned(), + ) + .into()) + } + + fn serialize_seq(self, _len: Option) -> Result { + Ok(SeqSerializer::default()) + } + + fn serialize_map(self, _len: Option) -> Result { + Ok(MapSerializer::default()) + } +} + +#[derive(Default)] +struct SeqSerializer(Vec); + +impl ser::SerializeSeq for SeqSerializer { + type Ok = SimpleValue; + type Error = Error; + + fn serialize_element(&mut self, value: &T) -> Result<()> + where + T: ?Sized + ser::Serialize, + { + self.0.push(value.serialize(Serializer)?); + Ok(()) + } + + fn end(self) -> Result { + Ok(List(self.0)) + } +} + +#[derive(Default)] +struct TupleSerializer(Vec); + +impl ser::SerializeTuple for TupleSerializer { + type Ok = SimpleValue; + type Error = Error; + + fn serialize_element(&mut self, value: &T) -> Result<()> + where + T: ?Sized + ser::Serialize, + { + self.0.push(value.serialize(Serializer)?); + Ok(()) + } + + fn end(self) -> Result { + Ok(Record( + self.0 + .into_iter() + .enumerate() + .map(|(i, x)| (format!("_{}", i + 1), x)) + .collect(), + )) + } +} + +#[derive(Default)] +struct MapSerializer { + map: BTreeMap, + key: Option, + val: Option, +} + +impl ser::SerializeMap for MapSerializer { + type Ok = SimpleValue; + type Error = Error; + + fn serialize_key(&mut self, key: &T) -> Result<()> + where + T: ?Sized + ser::Serialize, + { + let key = match key.serialize(Serializer)? { + Text(key) => key, + _ => return Err(::custom("not a string")), + }; + if let Some(val) = self.val.take() { + self.map.insert(key, val); + } else { + self.key = Some(key); + } + Ok(()) + } + + fn serialize_value(&mut self, val: &T) -> Result<()> + where + T: ?Sized + ser::Serialize, + { + let val: SimpleValue = val.serialize(Serializer)?; + if let Some(key) = self.key.take() { + self.map.insert(key, val); + } else { + self.val = Some(val); + } + Ok(()) + } + + fn end(self) -> Result { + Ok(Record(self.map)) + } +} + +#[derive(Default)] +struct StructSerializer(BTreeMap); + +impl ser::SerializeStruct for StructSerializer { + type Ok = SimpleValue; + type Error = Error; + + fn serialize_field(&mut self, key: &'static str, val: &T) -> Result<()> + where + T: ?Sized + ser::Serialize, + { + let val: SimpleValue = val.serialize(Serializer)?; + self.0.insert(key.into(), val); + Ok(()) + } + + fn end(self) -> Result { + Ok(Record(self.0)) + } +} diff --git a/serde_dhall/src/static_type.rs b/serde_dhall/src/static_type.rs index 3c5da18..7e6c27c 100644 --- a/serde_dhall/src/static_type.rs +++ b/serde_dhall/src/static_type.rs @@ -103,6 +103,26 @@ derive_builtin!(i32, Integer); derive_builtin!(f64, Double); derive_builtin!(f32, Double); derive_builtin!(String, Text); +derive_builtin!(&str, Text); + +impl StaticType for () { + fn static_type() -> SimpleType { + SimpleType::Record(vec![].into_iter().collect()) + } +} + +impl StaticType for (A,) +where + A: StaticType, +{ + fn static_type() -> SimpleType { + SimpleType::Record( + vec![("_1".to_owned(), A::static_type())] + .into_iter() + .collect(), + ) + } +} impl StaticType for (A, B) where @@ -121,6 +141,46 @@ where } } +impl StaticType for (A, B, C) +where + A: StaticType, + B: StaticType, + C: StaticType, +{ + fn static_type() -> SimpleType { + SimpleType::Record( + vec![ + ("_1".to_owned(), A::static_type()), + ("_2".to_owned(), B::static_type()), + ("_3".to_owned(), C::static_type()), + ] + .into_iter() + .collect(), + ) + } +} + +impl StaticType for (A, B, C, D) +where + A: StaticType, + B: StaticType, + C: StaticType, + D: StaticType, +{ + fn static_type() -> SimpleType { + SimpleType::Record( + vec![ + ("_1".to_owned(), A::static_type()), + ("_2".to_owned(), B::static_type()), + ("_3".to_owned(), C::static_type()), + ("_4".to_owned(), D::static_type()), + ] + .into_iter() + .collect(), + ) + } +} + impl StaticType for std::result::Result where T: StaticType, diff --git a/serde_dhall/src/value.rs b/serde_dhall/src/value.rs index d7116d9..ca05658 100644 --- a/serde_dhall/src/value.rs +++ b/serde_dhall/src/value.rs @@ -6,7 +6,7 @@ use dhall::semantics::{Hir, HirKind, Nir, NirKind}; pub use dhall::syntax::NumKind; use dhall::syntax::{Expr, ExprKind, Span}; -use crate::{Error, ErrorKind, FromDhall, Result, Sealed}; +use crate::{Error, ErrorKind, FromDhall, Result, ToDhall}; #[doc(hidden)] /// An arbitrary Dhall value. @@ -292,6 +292,94 @@ impl SimpleValue { _ => return None, }) } + + fn to_hir(&self, ty: &SimpleType) -> Result { + use SimpleType as T; + use SimpleValue as V; + let hir = |k| Hir::new(HirKind::Expr(k), Span::Artificial); + let type_error = |msg| Error(ErrorKind::Serialize(msg)); + let kind = match (self, ty) { + (V::Num(num @ NumKind::Bool(_)), T::Bool) + | (V::Num(num @ NumKind::Natural(_)), T::Natural) + | (V::Num(num @ NumKind::Integer(_)), T::Integer) + | (V::Num(num @ NumKind::Double(_)), T::Double) => { + ExprKind::Num(num.clone()) + } + (V::Text(v), T::Text) => ExprKind::TextLit(v.clone().into()), + (V::Optional(None), T::Optional(t)) => ExprKind::Op(OpKind::App( + hir(ExprKind::Builtin(Builtin::OptionalNone)), + t.to_hir(), + )), + (V::Optional(Some(v)), T::Optional(t)) => { + ExprKind::SomeLit(v.to_hir(t)?) + } + (V::List(v), T::List(t)) if v.is_empty() => { + ExprKind::EmptyListLit(hir(ExprKind::Op(OpKind::App( + hir(ExprKind::Builtin(Builtin::List)), + t.to_hir(), + )))) + } + (V::List(v), T::List(t)) => ExprKind::NEListLit( + v.iter().map(|v| v.to_hir(t)).collect::>()?, + ), + (V::Record(v), T::Record(t)) => ExprKind::RecordLit( + v.iter() + .map(|(k, v)| match t.get(k) { + Some(t) => Ok((k.clone().into(), v.to_hir(t)?)), + None => Err(type_error(format!( + "expected a value of type {}, found {:?}", + ty.to_hir().to_expr(Default::default()), + self + ))), + }) + .collect::>()?, + ), + (V::Union(variant, Some(v)), T::Union(t)) => match t.get(variant) { + Some(Some(variant_t)) => ExprKind::Op(OpKind::App( + hir(ExprKind::Op(OpKind::Field( + ty.to_hir(), + variant.clone().into(), + ))), + v.to_hir(variant_t)?, + )), + _ => { + return Err(type_error(format!( + "expected a value of type {}, found {:?}", + ty.to_hir().to_expr(Default::default()), + self + ))) + } + }, + (V::Union(variant, None), T::Union(t)) => match t.get(variant) { + Some(None) => ExprKind::Op(OpKind::Field( + ty.to_hir(), + variant.clone().into(), + )), + _ => { + return Err(type_error(format!( + "expected a value of type {}, found {:?}", + ty.to_hir().to_expr(Default::default()), + self + ))) + } + }, + _ => { + return Err(type_error(format!( + "expected a value of type {}, found {:?}", + ty.to_hir().to_expr(Default::default()), + self + ))) + } + }; + Ok(hir(kind)) + } + pub(crate) fn into_value(self, ty: &SimpleType) -> Result { + Ok(Value { + hir: self.to_hir(ty)?, + as_simple_val: Some(self), + as_simple_ty: None, + }) + } } impl SimpleType { @@ -376,8 +464,10 @@ impl SimpleType { } } -impl Sealed for Value {} -impl Sealed for SimpleType {} +impl crate::deserialize::Sealed for Value {} +impl crate::deserialize::Sealed for SimpleType {} +impl crate::serialize::Sealed for Value {} +impl crate::serialize::Sealed for SimpleValue {} impl FromDhall for Value { fn from_dhall(v: &Value) -> Result { @@ -394,6 +484,16 @@ impl FromDhall for SimpleType { }) } } +impl ToDhall for Value { + fn to_dhall(&self, _ty: &SimpleType) -> Result { + Ok(self.clone()) + } +} +impl ToDhall for SimpleValue { + fn to_dhall(&self, ty: &SimpleType) -> Result { + self.clone().into_value(ty) + } +} impl Eq for Value {} impl PartialEq for Value { diff --git a/serde_dhall/tests/de.rs b/serde_dhall/tests/de.rs index 3abaad2..0b43de2 100644 --- a/serde_dhall/tests/de.rs +++ b/serde_dhall/tests/de.rs @@ -1,62 +1,5 @@ use serde::Deserialize; -use serde_dhall::{from_str, FromDhall, NumKind, SimpleValue, StaticType}; - -#[test] -fn test_de_typed() { - fn parse(s: &str) -> T { - from_str(s).static_type_annotation().parse().unwrap() - } - - assert_eq!(parse::("True"), true); - - assert_eq!(parse::("1"), 1); - assert_eq!(parse::("1"), 1); - assert_eq!(parse::("1"), 1); - - assert_eq!(parse::("+1"), 1); - assert_eq!(parse::("+1"), 1); - assert_eq!(parse::("+1"), 1); - - assert_eq!(parse::("1.0"), 1.0); - assert_eq!(parse::("1.0"), 1.0); - - assert_eq!(parse::(r#""foo""#), "foo".to_owned()); - assert_eq!(parse::>("[] : List Natural"), >::new()); - assert_eq!(parse::>("[1, 2]"), vec![1, 2]); - assert_eq!(parse::>("None Natural"), None); - assert_eq!(parse::>("Some 1"), Some(1)); - - assert_eq!( - parse::<(u64, String)>(r#"{ _1 = 1, _2 = "foo" }"#), - (1, "foo".to_owned()) - ); - - #[derive(Debug, PartialEq, Eq, Deserialize, StaticType)] - struct Foo { - x: u64, - y: i64, - } - assert_eq!(parse::("{ x = 1, y = -2 }"), Foo { x: 1, y: -2 }); - - #[derive(Debug, PartialEq, Eq, Deserialize, StaticType)] - enum Bar { - X(u64), - Y(i64), - } - assert_eq!(parse::("< X: Natural | Y: Integer >.X 1"), Bar::X(1)); - - #[derive(Debug, PartialEq, Eq, Deserialize, StaticType)] - enum Baz { - X, - Y(i64), - } - assert_eq!(parse::("< X | Y: Integer >.X"), Baz::X); - - assert!(from_str("< X | Y: Integer >.Y") - .static_type_annotation() - .parse::() - .is_err()); -} +use serde_dhall::{from_str, FromDhall, NumKind, SimpleValue}; #[test] fn test_de_untyped() { diff --git a/serde_dhall/tests/serde.rs b/serde_dhall/tests/serde.rs new file mode 100644 index 0000000..39f2f79 --- /dev/null +++ b/serde_dhall/tests/serde.rs @@ -0,0 +1,145 @@ +mod serde { + use serde::{Deserialize, Serialize}; + use serde_dhall::{from_str, serialize, FromDhall, StaticType, ToDhall}; + + fn assert_de(s: &str, x: T) + where + T: FromDhall + StaticType + PartialEq + std::fmt::Debug, + { + assert_eq!( + from_str(s) + .static_type_annotation() + .parse::() + .map_err(|e| e.to_string()), + Ok(x) + ); + } + fn assert_ser(s: &str, x: T) + where + T: ToDhall + StaticType + PartialEq + std::fmt::Debug, + { + assert_eq!( + serialize(&x) + .static_type_annotation() + .to_string() + .map_err(|e| e.to_string()), + Ok(s.to_string()) + ); + } + fn assert_serde(s: &str, x: T) + where + T: ToDhall + + FromDhall + + StaticType + + PartialEq + + std::fmt::Debug + + Clone, + { + assert_de(s, x.clone()); + assert_ser(s, x); + } + + #[test] + fn numbers() { + assert_serde("True", true); + + assert_serde("1", 1u64); + assert_serde("1", 1u32); + assert_serde("1", 1usize); + + assert_serde("+1", 1i64); + assert_serde("+1", 1i32); + assert_serde("+1", 1isize); + + assert_serde("1.0", 1.0f64); + assert_serde("1.0", 1.0f32); + } + + #[test] + fn text() { + assert_serde(r#""foo""#, "foo".to_owned()); + assert_ser(r#""foo""#, "foo"); + } + + #[test] + fn list() { + assert_serde("[] : List Natural", >::new()); + assert_serde("[] : List Text", >::new()); + assert_serde( + r#"["foo", "bar"]"#, + vec!["foo".to_owned(), "bar".to_owned()], + ); + assert_ser(r#"["foo", "bar"]"#, vec!["foo", "bar"]); + assert_serde::>("[1, 2]", vec![1, 2]); + } + + #[test] + fn optional() { + assert_serde::>("None Natural", None); + assert_serde::>("None Text", None); + assert_serde("Some 1", Some(1u64)); + } + + #[test] + fn tuple() { + assert_serde::<()>(r#"{=}"#, ()); + assert_serde::<(u64, String)>( + r#"{ _1 = 1, _2 = "foo" }"#, + (1, "foo".to_owned()), + ); + assert_serde::<(u64, u64, u64, u64)>( + r#"{ _1 = 1, _2 = 2, _3 = 3, _4 = 4 }"#, + (1, 2, 3, 4), + ); + } + + #[test] + fn structs() { + // #[derive( + // Debug, Clone, PartialEq, Eq, Deserialize, Serialize, StaticType, + // )] + // struct Foo; + // assert_serde::("{=}", Foo); + + // #[derive( + // Debug, Clone, PartialEq, Eq, Deserialize, Serialize, StaticType, + // )] + // struct Bar(u64); + // assert_serde::("{ _1 = 1 }", Bar (1)); + + #[derive( + Debug, Clone, PartialEq, Eq, Deserialize, Serialize, StaticType, + )] + struct Baz { + x: u64, + y: i64, + } + assert_serde::("{ x = 1, y = -2 }", Baz { x: 1, y: -2 }); + } + + #[test] + fn enums() { + #[derive( + Debug, Clone, PartialEq, Eq, Deserialize, Serialize, StaticType, + )] + enum Foo { + X(u64), + Y(i64), + } + assert_serde::("< X: Natural | Y: Integer >.X 1", Foo::X(1)); + + #[derive( + Debug, Clone, PartialEq, Eq, Deserialize, Serialize, StaticType, + )] + enum Bar { + X, + Y(i64), + } + assert_serde::("< X | Y: Integer >.X", Bar::X); + + assert!(from_str("< X | Y: Integer >.Y") + .static_type_annotation() + .parse::() + .is_err()); + } +} -- cgit v1.2.3 From 3b728aff86a086f71f013b77a715c33748d9f6a8 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 28 Oct 2020 21:45:42 +0000 Subject: Make type annotation optional to allow serializing SimpleValue --- serde_dhall/src/options/de.rs | 37 ++-------- serde_dhall/src/options/mod.rs | 34 +++++++++ serde_dhall/src/options/ser.rs | 33 +++------ serde_dhall/src/serialize.rs | 16 ++++- serde_dhall/src/value.rs | 140 ++++++++++++++++++++------------------ serde_dhall/tests/de.rs | 34 +-------- serde_dhall/tests/serde.rs | 8 ++- serde_dhall/tests/simple_value.rs | 57 ++++++++++++++++ 8 files changed, 202 insertions(+), 157 deletions(-) create mode 100644 serde_dhall/tests/simple_value.rs (limited to 'serde_dhall') diff --git a/serde_dhall/src/options/de.rs b/serde_dhall/src/options/de.rs index 8ff794d..e4f3456 100644 --- a/serde_dhall/src/options/de.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 OptionalAnnot { - fn get_annot(a: &A) -> Option; -} -impl OptionalAnnot for T { - fn get_annot(_: &NoAnnot) -> Option { - None - } -} -impl<'ty, T> OptionalAnnot> for T { - fn get_annot(a: &ManualAnnot<'ty>) -> Option { - Some(a.0.clone()) - } -} -impl OptionalAnnot for T { - fn get_annot(_: &StaticAnnot) -> Option { - 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,7 +227,8 @@ impl<'a, A> Deserializer<'a, A> { fn _parse(&self) -> dhall::error::Result where - T: OptionalAnnot, + A: TypeAnnot, + T: HasAnnot, { let parsed = match &self.source { Source::Str(s) => Parsed::parse_str(s)?, @@ -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,7 +263,8 @@ impl<'a, A> Deserializer<'a, A> { /// [`StaticType`]: trait.StaticType.html pub fn parse(&self) -> Result where - T: FromDhall + OptionalAnnot, + A: TypeAnnot, + T: FromDhall + HasAnnot, { let val = self ._parse::() diff --git a/serde_dhall/src/options/mod.rs b/serde_dhall/src/options/mod.rs index 384f318..9241c45 100644 --- a/serde_dhall/src/options/mod.rs +++ b/serde_dhall/src/options/mod.rs @@ -1,2 +1,36 @@ +use crate::{SimpleType, StaticType}; + pub(crate) mod de; pub(crate) mod ser; + +#[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 TypeAnnot: Copy {} +pub trait HasAnnot { + fn get_annot(a: A) -> Option; +} + +impl TypeAnnot for NoAnnot {} +impl TypeAnnot for ManualAnnot<'_> {} +impl TypeAnnot for StaticAnnot {} + +impl HasAnnot for T { + fn get_annot(_: NoAnnot) -> Option { + None + } +} +impl HasAnnot> for T { + fn get_annot(a: ManualAnnot<'_>) -> Option { + Some(a.0.clone()) + } +} +impl HasAnnot for T { + fn get_annot(_: StaticAnnot) -> Option { + Some(T::static_type()) + } +} diff --git a/serde_dhall/src/options/ser.rs b/serde_dhall/src/options/ser.rs index 026dd21..ea5d16a 100644 --- a/serde_dhall/src/options/ser.rs +++ b/serde_dhall/src/options/ser.rs @@ -1,25 +1,5 @@ -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 { - fn get_annot(a: &A) -> SimpleType; -} -impl<'ty, T> RequiredAnnot> for T { - fn get_annot(a: &ManualAnnot<'ty>) -> SimpleType { - a.0.clone() - } -} -impl RequiredAnnot for T { - fn get_annot(_: &StaticAnnot) -> SimpleType { - T::static_type() - } -} +use crate::options::{HasAnnot, ManualAnnot, NoAnnot, StaticAnnot, TypeAnnot}; +use crate::{Result, SimpleType, ToDhall}; #[derive(Debug, Clone)] pub struct Serializer<'a, T, A> { @@ -46,12 +26,15 @@ impl<'a, T> Serializer<'a, T, NoAnnot> { } } -impl<'a, T, A> Serializer<'a, T, A> { +impl<'a, T, A> Serializer<'a, T, A> +where + A: TypeAnnot, +{ pub fn to_string(&self) -> Result where - T: ToDhall + RequiredAnnot, + T: ToDhall + HasAnnot, { - let val = self.data.to_dhall(&T::get_annot(&self.annot))?; + let val = self.data.to_dhall(T::get_annot(self.annot).as_ref())?; Ok(val.to_string()) } } diff --git a/serde_dhall/src/serialize.rs b/serde_dhall/src/serialize.rs index dd3e426..21b1931 100644 --- a/serde_dhall/src/serialize.rs +++ b/serde_dhall/src/serialize.rs @@ -11,7 +11,7 @@ pub trait Sealed {} pub trait ToDhall: Sealed { #[doc(hidden)] - fn to_dhall(&self, ty: &SimpleType) -> Result; + fn to_dhall(&self, ty: Option<&SimpleType>) -> Result; } impl Sealed for T where T: ser::Serialize {} @@ -20,7 +20,7 @@ impl ToDhall for T where T: ser::Serialize, { - fn to_dhall(&self, ty: &SimpleType) -> Result { + fn to_dhall(&self, ty: Option<&SimpleType>) -> Result { let sval: SimpleValue = self.serialize(Serializer)?; sval.into_value(ty) } @@ -310,3 +310,15 @@ impl ser::SerializeStruct for StructSerializer { Ok(Record(self.0)) } } + +impl serde::ser::Serialize for SimpleValue { + fn serialize( + &self, + serializer: S, + ) -> std::result::Result + where + S: serde::ser::Serializer, + { + todo!() + } +} diff --git a/serde_dhall/src/value.rs b/serde_dhall/src/value.rs index ca05658..50c12bd 100644 --- a/serde_dhall/src/value.rs +++ b/serde_dhall/src/value.rs @@ -293,87 +293,103 @@ impl SimpleValue { }) } - fn to_hir(&self, ty: &SimpleType) -> Result { + // Converts this to `Hir`, using the optional type annotation. Without the type, things like + // empty lists and unions will fail to convert. + fn to_hir(&self, ty: Option<&SimpleType>) -> Result { use SimpleType as T; use SimpleValue as V; let hir = |k| Hir::new(HirKind::Expr(k), Span::Artificial); - let type_error = |msg| Error(ErrorKind::Serialize(msg)); + let type_error = || { + Error(ErrorKind::Serialize(format!( + "expected a value of type {}, found {:?}", + ty.unwrap().to_hir().to_expr(Default::default()), + self + ))) + }; + let type_missing = || { + Error(ErrorKind::Serialize(format!( + "cannot serialize value without a type annotation: {:?}", + self + ))) + }; let kind = match (self, ty) { - (V::Num(num @ NumKind::Bool(_)), T::Bool) - | (V::Num(num @ NumKind::Natural(_)), T::Natural) - | (V::Num(num @ NumKind::Integer(_)), T::Integer) - | (V::Num(num @ NumKind::Double(_)), T::Double) => { - ExprKind::Num(num.clone()) + (V::Num(num @ NumKind::Bool(_)), Some(T::Bool)) + | (V::Num(num @ NumKind::Natural(_)), Some(T::Natural)) + | (V::Num(num @ NumKind::Integer(_)), Some(T::Integer)) + | (V::Num(num @ NumKind::Double(_)), Some(T::Double)) + | (V::Num(num), None) => ExprKind::Num(num.clone()), + (V::Text(v), Some(T::Text)) | (V::Text(v), None) => { + ExprKind::TextLit(v.clone().into()) } - (V::Text(v), T::Text) => ExprKind::TextLit(v.clone().into()), - (V::Optional(None), T::Optional(t)) => ExprKind::Op(OpKind::App( - hir(ExprKind::Builtin(Builtin::OptionalNone)), - t.to_hir(), - )), - (V::Optional(Some(v)), T::Optional(t)) => { - ExprKind::SomeLit(v.to_hir(t)?) + + (V::Optional(None), None) => return Err(type_missing()), + (V::Optional(None), Some(T::Optional(t))) => { + ExprKind::Op(OpKind::App( + hir(ExprKind::Builtin(Builtin::OptionalNone)), + t.to_hir(), + )) + } + (V::Optional(Some(v)), None) => ExprKind::SomeLit(v.to_hir(None)?), + (V::Optional(Some(v)), Some(T::Optional(t))) => { + ExprKind::SomeLit(v.to_hir(Some(t))?) } - (V::List(v), T::List(t)) if v.is_empty() => { + + (V::List(v), None) if v.is_empty() => return Err(type_missing()), + (V::List(v), Some(T::List(t))) if v.is_empty() => { ExprKind::EmptyListLit(hir(ExprKind::Op(OpKind::App( hir(ExprKind::Builtin(Builtin::List)), t.to_hir(), )))) } - (V::List(v), T::List(t)) => ExprKind::NEListLit( - v.iter().map(|v| v.to_hir(t)).collect::>()?, + (V::List(v), None) => ExprKind::NEListLit( + v.iter().map(|v| v.to_hir(None)).collect::>()?, + ), + (V::List(v), Some(T::List(t))) => ExprKind::NEListLit( + v.iter().map(|v| v.to_hir(Some(t))).collect::>()?, + ), + + (V::Record(v), None) => ExprKind::RecordLit( + v.iter() + .map(|(k, v)| Ok((k.clone().into(), v.to_hir(None)?))) + .collect::>()?, ), - (V::Record(v), T::Record(t)) => ExprKind::RecordLit( + (V::Record(v), Some(T::Record(t))) => ExprKind::RecordLit( v.iter() .map(|(k, v)| match t.get(k) { - Some(t) => Ok((k.clone().into(), v.to_hir(t)?)), - None => Err(type_error(format!( - "expected a value of type {}, found {:?}", - ty.to_hir().to_expr(Default::default()), - self - ))), + Some(t) => Ok((k.clone().into(), v.to_hir(Some(t))?)), + None => Err(type_error()), }) .collect::>()?, ), - (V::Union(variant, Some(v)), T::Union(t)) => match t.get(variant) { - Some(Some(variant_t)) => ExprKind::Op(OpKind::App( - hir(ExprKind::Op(OpKind::Field( - ty.to_hir(), - variant.clone().into(), - ))), - v.to_hir(variant_t)?, - )), - _ => { - return Err(type_error(format!( - "expected a value of type {}, found {:?}", - ty.to_hir().to_expr(Default::default()), - self - ))) + + (V::Union(..), None) => return Err(type_missing()), + (V::Union(variant, Some(v)), Some(T::Union(t))) => { + match t.get(variant) { + Some(Some(variant_t)) => ExprKind::Op(OpKind::App( + hir(ExprKind::Op(OpKind::Field( + ty.unwrap().to_hir(), + variant.clone().into(), + ))), + v.to_hir(Some(variant_t))?, + )), + _ => return Err(type_error()), } - }, - (V::Union(variant, None), T::Union(t)) => match t.get(variant) { - Some(None) => ExprKind::Op(OpKind::Field( - ty.to_hir(), - variant.clone().into(), - )), - _ => { - return Err(type_error(format!( - "expected a value of type {}, found {:?}", - ty.to_hir().to_expr(Default::default()), - self - ))) + } + (V::Union(variant, None), Some(T::Union(t))) => { + match t.get(variant) { + Some(None) => ExprKind::Op(OpKind::Field( + ty.unwrap().to_hir(), + variant.clone().into(), + )), + _ => return Err(type_error()), } - }, - _ => { - return Err(type_error(format!( - "expected a value of type {}, found {:?}", - ty.to_hir().to_expr(Default::default()), - self - ))) } + + (_, Some(_)) => return Err(type_error()), }; Ok(hir(kind)) } - pub(crate) fn into_value(self, ty: &SimpleType) -> Result { + pub(crate) fn into_value(self, ty: Option<&SimpleType>) -> Result { Ok(Value { hir: self.to_hir(ty)?, as_simple_val: Some(self), @@ -467,7 +483,6 @@ impl SimpleType { impl crate::deserialize::Sealed for Value {} impl crate::deserialize::Sealed for SimpleType {} impl crate::serialize::Sealed for Value {} -impl crate::serialize::Sealed for SimpleValue {} impl FromDhall for Value { fn from_dhall(v: &Value) -> Result { @@ -485,15 +500,10 @@ impl FromDhall for SimpleType { } } impl ToDhall for Value { - fn to_dhall(&self, _ty: &SimpleType) -> Result { + fn to_dhall(&self, _ty: Option<&SimpleType>) -> Result { Ok(self.clone()) } } -impl ToDhall for SimpleValue { - fn to_dhall(&self, ty: &SimpleType) -> Result { - self.clone().into_value(ty) - } -} impl Eq for Value {} impl PartialEq for Value { diff --git a/serde_dhall/tests/de.rs b/serde_dhall/tests/de.rs index 0b43de2..f49bee4 100644 --- a/serde_dhall/tests/de.rs +++ b/serde_dhall/tests/de.rs @@ -1,5 +1,5 @@ use serde::Deserialize; -use serde_dhall::{from_str, FromDhall, NumKind, SimpleValue}; +use serde_dhall::{from_str, FromDhall}; #[test] fn test_de_untyped() { @@ -57,37 +57,5 @@ fn test_de_untyped() { assert!(from_str("List/length [True, 42]").parse::().is_err()); } -#[test] -fn test_de_simplevalue() { - let bool_true = SimpleValue::Num(NumKind::Bool(true)); - // https://github.com/Nadrieril/dhall-rust/issues/184 - assert_eq!( - from_str("[ True ]").parse::>().unwrap(), - vec![bool_true.clone()] - ); - - assert_eq!( - from_str("< Foo >.Foo").parse::().unwrap(), - SimpleValue::Union("Foo".into(), None) - ); - assert_eq!( - from_str("< Foo: Bool >.Foo True") - .parse::() - .unwrap(), - SimpleValue::Union("Foo".into(), Some(Box::new(bool_true.clone()))) - ); - - #[derive(Debug, PartialEq, Eq, Deserialize)] - struct Foo { - foo: SimpleValue, - } - assert_eq!( - from_str("{ foo = True }").parse::().unwrap(), - Foo { - foo: bool_true.clone() - }, - ); -} - // TODO: test various builder configurations // In particular test cloning and reusing builder diff --git a/serde_dhall/tests/serde.rs b/serde_dhall/tests/serde.rs index 39f2f79..ce25380 100644 --- a/serde_dhall/tests/serde.rs +++ b/serde_dhall/tests/serde.rs @@ -75,9 +75,13 @@ mod serde { #[test] fn optional() { - assert_serde::>("None Natural", None); - assert_serde::>("None Text", None); + assert_serde("None Natural", None::); + assert_serde("None Text", None::); assert_serde("Some 1", Some(1u64)); + assert_eq!( + serialize(&None::).to_string().map_err(|e| e.to_string()), + Err("cannot serialize value without a type annotation: Optional(None)".to_string()) + ); } #[test] diff --git a/serde_dhall/tests/simple_value.rs b/serde_dhall/tests/simple_value.rs new file mode 100644 index 0000000..d2792d4 --- /dev/null +++ b/serde_dhall/tests/simple_value.rs @@ -0,0 +1,57 @@ +mod simple_value { + use serde::{Deserialize, Serialize}; + use serde_dhall::{ + from_str, serialize, FromDhall, NumKind, SimpleValue, ToDhall, + }; + + fn assert_de(s: &str, x: T) + where + T: FromDhall + PartialEq + std::fmt::Debug, + { + assert_eq!(from_str(s).parse::().map_err(|e| e.to_string()), Ok(x)); + } + fn assert_ser(s: &str, x: T) + where + T: ToDhall + PartialEq + std::fmt::Debug, + { + assert_eq!( + serialize(&x).to_string().map_err(|e| e.to_string()), + Ok(s.to_string()) + ); + } + fn assert_serde(s: &str, x: T) + where + T: ToDhall + FromDhall + PartialEq + std::fmt::Debug + Clone, + { + assert_de(s, x.clone()); + assert_ser(s, x); + } + + #[test] + fn test_serde() { + let bool_true = SimpleValue::Num(NumKind::Bool(true)); + // https://github.com/Nadrieril/dhall-rust/issues/184 + // assert_serde("[ True ]", vec![bool_true.clone()]); + assert_de("< Foo >.Foo", SimpleValue::Union("Foo".into(), None)); + assert_de( + "< Foo: Bool >.Foo True", + SimpleValue::Union("Foo".into(), Some(Box::new(bool_true.clone()))), + ); + + // assert_eq!( + // serialize(&SimpleValue::Optional(None)).to_string().map_err(|e| e.to_string()), + // Err("cannot serialize value without a type annotation: Optional(None)".to_string()) + // ); + + // #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] + // struct Foo { + // foo: SimpleValue, + // } + // assert_serde( + // "{ foo = True }", + // Foo { + // foo: bool_true.clone(), + // }, + // ); + } +} -- cgit v1.2.3 From 27377f045a2449c86ea3b680d168d2224c90e5e3 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 28 Oct 2020 21:59:14 +0000 Subject: Add missing documentation --- serde_dhall/src/lib.rs | 114 ++++++++++++++++++++++--- serde_dhall/src/options/ser.rs | 184 +++++++++++++++++++++++++++++++++++++++++ serde_dhall/src/serialize.rs | 28 +++++++ 3 files changed, 316 insertions(+), 10 deletions(-) (limited to 'serde_dhall') diff --git a/serde_dhall/src/lib.rs b/serde_dhall/src/lib.rs index e236e37..387278b 100644 --- a/serde_dhall/src/lib.rs +++ b/serde_dhall/src/lib.rs @@ -12,13 +12,12 @@ //! YAML. It uses the [Serde][serde] serialization library to provide drop-in support for Dhall //! for any datatype that supports serde (and that's a lot of them !). //! -//! This library is limited to deserializing (reading) Dhall values; serializing (writing) -//! values to Dhall is not supported. -//! //! # Basic usage //! -//! The main entrypoint of this library is the [`from_str`](fn.from_str.html) function. It reads a string -//! containing a Dhall expression and deserializes it into any serde-compatible type. +//! ## Deserialization (reading) +//! +//! The entrypoint for deserialization is the [`from_str`](fn.from_str.html) function. It reads a +//! string containing a Dhall expression and deserializes it into any serde-compatible type. //! //! This could mean a common Rust type like `HashMap`: //! @@ -65,16 +64,69 @@ //! # } //! ``` //! +//! ## Serialization (writing) +//! +//! The entrypoint for serialization is the [`serialize`](fn.serialize.html) function. It takes a +//! serde-compatible type value and serializes it to a string containing a Dhall expression. +//! +//! This could mean a common Rust type like `HashMap`: +//! +//! ```rust +//! # fn main() -> serde_dhall::Result<()> { +//! use std::collections::HashMap; +//! +//! let mut map = HashMap::new(); +//! map.insert("x".to_string(), 1u64); +//! map.insert("y".to_string(), 2u64); +//! +//! let string = serde_dhall::serialize(&map).to_string()?; +//! assert_eq!( +//! string, +//! "{ x = 1, y = 2 }".to_string(), +//! ); +//! # Ok(()) +//! # } +//! ``` +//! +//! or a custom datatype, using serde's `derive` mechanism: +//! +//! ```rust +//! # fn main() -> serde_dhall::Result<()> { +//! use serde::Serialize; +//! +//! #[derive(Serialize)] +//! struct Point { +//! x: u64, +//! y: u64, +//! } +//! +//! let data = Point { x: 1, y: 2 }; +//! let string = serde_dhall::serialize(&data).to_string()?; +//! assert_eq!( +//! string, +//! "{ x = 1, y = 2 }".to_string(), +//! ); +//! # Ok(()) +//! # } +//! ``` +//! +//! Beware that in order to serialize empty options, empty lists or enums correctly, you will need +//! to provide a type annotation! +//! //! # Replacing `serde_json` or `serde_yaml` //! //! If you used to consume JSON or YAML, you only need to replace [`serde_json::from_str`] or //! [`serde_yaml::from_str`] with [`serde_dhall::from_str(…).parse()`](fn.from_str.html). +//! If you used to produce JSON or YAML, you only need to replace [`serde_json::to_string`] or +//! [`serde_yaml::to_string`] with [`serde_dhall::serialize(…).to_string()`](fn.serialize.html). //! //! [`serde_json::from_str`]: https://docs.serde.rs/serde_json/fn.from_str.html //! [`serde_yaml::from_str`]: https://docs.serde.rs/serde_yaml/fn.from_str.html +//! [`serde_json::to_string`]: https://docs.serde.rs/serde_json/fn.to_string.html +//! [`serde_yaml::to_string`]: https://docs.serde.rs/serde_yaml/fn.to_string.html //! //! -//! # Additional Dhall typechecking +//! # Additional type annotations //! //! When deserializing, normal type checking is done to ensure that the returned value is a valid //! Dhall value. However types are @@ -82,11 +134,16 @@ //! matches a given Dhall type. That way, a type error will be caught on the Dhall side, and have //! pretty and explicit errors that point to the source file. //! -//! There are two ways to typecheck a Dhall value in this way: you can provide the type manually or -//! you can let Rust infer it for you. +//! It is also possible to provide a type annotation when serializing. This is useful in particular +//! for types like `HashMap` or [`SimpleValue`] that do not have a fixed type as Dhall values. +//! +//! Moreover, some values (namely empty options, empty lists, and enums) _require_ a type annotation +//! in order to be converted to Dhall, because the resulting Dhall value will contain the type +//! explicitly. //! -//! To let Rust infer the appropriate Dhall type, use the [StaticType](trait.StaticType.html) -//! trait. +//! There are two ways to provide a type in this way: you can provide it manually or you can let +//! Rust infer it for you. To let Rust infer the appropriate Dhall type, use the +//! [StaticType](trait.StaticType.html) trait. //! //! ```rust //! # fn main() -> serde_dhall::Result<()> { @@ -121,6 +178,28 @@ //! # } //! ``` //! +//! ``` +//! # fn main() -> serde_dhall::Result<()> { +//! use serde::Serialize; +//! use serde_dhall::{serialize, StaticType}; +//! +//! #[derive(Serialize, StaticType)] +//! enum MyOption { +//! MyNone, +//! MySome(u64), +//! } +//! +//! let data = MyOption::MySome(0); +//! let string = serialize(&data) +//! .static_type_annotation() +//! .to_string()?; +//! // The resulting Dhall string depends on the type annotation; it could not have been +//! // printed without it. +//! assert_eq!(string, "< MyNone | MySome: Natural >.MySome 0".to_string()); +//! # Ok(()) +//! # } +//! ``` +//! //! To provide a type manually, you need a [`SimpleType`](enum.SimpleType.html) value. You //! can parse it from some Dhall text like you would parse any other value. //! @@ -151,12 +230,27 @@ //! # } //! ``` //! +//! ``` +//! # fn main() -> serde_dhall::Result<()> { +//! use serde_dhall::{serialize, from_str, SimpleValue}; +//! +//! let ty = from_str("< A | B: Bool >").parse()?; +//! let data = SimpleValue::Union("A".to_string(), None); +//! let string = serialize(&data) +//! .type_annotation(&ty) +//! .to_string()?; +//! assert_eq!(string, "< A | B: Bool >.A".to_string()); +//! # Ok(()) +//! # } +//! ``` +//! //! # Controlling deserialization //! //! If you need more control over the process of reading Dhall values, e.g. disabling //! imports, see the [`Deserializer`] methods. //! //! [`Deserializer`]: struct.Deserializer.html +//! [`SimpleValue`]: enum.SimpleValue.html //! [dhall]: https://dhall-lang.org/ //! [serde]: https://docs.serde.rs/serde/ //! [serde::Deserialize]: https://docs.serde.rs/serde/trait.Deserialize.html diff --git a/serde_dhall/src/options/ser.rs b/serde_dhall/src/options/ser.rs index ea5d16a..3332735 100644 --- a/serde_dhall/src/options/ser.rs +++ b/serde_dhall/src/options/ser.rs @@ -1,6 +1,49 @@ use crate::options::{HasAnnot, ManualAnnot, NoAnnot, StaticAnnot, TypeAnnot}; use crate::{Result, SimpleType, ToDhall}; +/// Controls how a Dhall value is written. +/// +/// This builder exposes the ability to configure how a value is serialized, and to set type +/// annotations. +/// +/// When using [`Serializer`], you'll create it with [`serialize`], then chain calls to methods to +/// set each option, then call [`to_string`]. This will give you a [`Result`] containing +/// the input serialized to Dhall. +/// +/// Note that if you do not provide a type annotation, some values may not be convertible to Dhall, +/// like empty lists or enums. +/// +/// [`Serializer`]: struct.Serializer.html +/// [`serialize`]: fn.serialize.html +/// [`to_string`]: struct.Serializer.html#method.to_string +/// [`Result`]: type.Result.html +/// +/// # Examples +/// +/// Serializing without a type annotation: +/// +/// ```rust +/// # fn main() -> serde_dhall::Result<()> { +/// use serde_dhall::serialize; +/// +/// let string = serialize(&1i64).to_string()?; +/// assert_eq!(string, "+1".to_string()); +/// # Ok(()) +/// # } +/// ``` +/// +/// Serializing with an automatic type annotation: +/// +/// ```rust +/// # fn main() -> serde_dhall::Result<()> { +/// use serde_dhall::serialize; +/// +/// let data: Option = None; +/// let string = serialize(&data).static_type_annotation().to_string()?; +/// assert_eq!(string, "None Natural".to_string()); +/// # Ok(()) +/// # } +/// ``` #[derive(Debug, Clone)] pub struct Serializer<'a, T, A> { data: &'a T, @@ -8,6 +51,40 @@ pub struct Serializer<'a, T, A> { } impl<'a, T> Serializer<'a, T, NoAnnot> { + /// Provides a type to the serialization process. The provided value will be checked against + /// that type, and the type will be used when Dhall needs it, like for empty lists or for + /// unions. + /// + /// In many cases the Dhall type that corresponds to a Rust type can be inferred automatically. + /// See the [`StaticType`] trait and the [`static_type_annotation`] method for that. + /// + /// # Example + /// + /// ``` + /// # fn main() -> serde_dhall::Result<()> { + /// use serde_dhall::{serialize, from_str, SimpleType, SimpleValue}; + /// + /// let ty = from_str("< A | B: Bool >").parse()?; + /// let data = SimpleValue::Union("A".to_string(), None); + /// let string = serialize(&data) + /// .type_annotation(&ty) + /// .to_string()?; + /// assert_eq!(string, "< A | B: Bool >.A".to_string()); + /// + /// // Invalid data fails the type validation; serialization would have succeeded otherwise. + /// let ty = SimpleType::Integer; + /// assert!( + /// serialize(&Some(0u64)) + /// .type_annotation(&ty) + /// .to_string() + /// .is_err() + /// ); + /// # Ok(()) + /// # } + /// ``` + /// + /// [`static_type_annotation`]: struct.Serializer.html#method.static_type_annotation + /// [`StaticType`]: trait.StaticType.html pub fn type_annotation<'ty>( self, ty: &'ty SimpleType, @@ -18,6 +95,37 @@ impl<'a, T> Serializer<'a, T, NoAnnot> { } } + /// Uses the type of `T` in the serialization process. This will be used when Dhall needs it, + /// like for empty lists or for unions. + /// + /// `T` must implement the [`StaticType`] trait. If it doesn't, you can use [`type_annotation`] + /// + /// # Example + /// + /// ``` + /// # fn main() -> serde_dhall::Result<()> { + /// use serde::Serialize; + /// use serde_dhall::{serialize, StaticType}; + /// + /// #[derive(Serialize, StaticType)] + /// enum MyOption { + /// MyNone, + /// MySome(u64), + /// } + /// + /// let data = MyOption::MySome(0); + /// let string = serialize(&data) + /// .static_type_annotation() + /// .to_string()?; + /// // The resulting Dhall string depends on the type annotation; it could not have been + /// // printed without it. + /// assert_eq!(string, "< MyNone | MySome: Natural >.MySome 0".to_string()); + /// # Ok(()) + /// # } + /// ``` + /// + /// [`type_annotation`]: struct.Serializer.html#method.type_annotation + /// [`StaticType`]: trait.StaticType.html pub fn static_type_annotation(self) -> Serializer<'a, T, StaticAnnot> { Serializer { annot: StaticAnnot, @@ -30,6 +138,27 @@ impl<'a, T, A> Serializer<'a, T, A> where A: TypeAnnot, { + /// Prints the chosen value with the options provided. + /// + /// If you enabled static annotations, `T` is required to implement [`StaticType`]. + /// + /// Note that if you do not provide a type annotation, some values may not be convertible to + /// Dhall, like empty lists or enums. + /// + /// + /// # Example + /// + /// ```rust + /// # fn main() -> serde_dhall::Result<()> { + /// use serde_dhall::serialize; + /// + /// let string = serialize(&1i64).static_type_annotation().to_string()?; + /// assert_eq!(string, "+1".to_string()); + /// # Ok(()) + /// # } + /// ``` + /// + /// [`StaticType`]: trait.StaticType.html pub fn to_string(&self) -> Result where T: ToDhall + HasAnnot, @@ -39,6 +168,61 @@ where } } +/// Serialize a value to a string of Dhall text. +/// +/// This returns a [`Serializer`] object. Call the [`to_string`] method to get the serialized +/// value, or use other [`Serializer`] methods to control the serialization process. +/// +/// In order to process certain values (like unions or empty lists) correctly, it is necessary to +/// add a type annotation (with [`static_type_annotation`] or [`type_annotation`]). +/// +/// # Examples +/// +/// ```rust +/// # fn main() -> serde_dhall::Result<()> { +/// use serde::Serialize; +/// use serde_dhall::{serialize, StaticType}; +/// +/// #[derive(Serialize)] +/// struct Point { +/// x: u64, +/// y: u64, +/// } +/// +/// +/// let data = Point { x: 0, y: 0 }; +/// let string = serialize(&data).to_string()?; +/// assert_eq!(string, "{ x = 0, y = 0 }"); +/// # Ok(()) +/// # } +/// ``` +/// +/// ```rust +/// # fn main() -> serde_dhall::Result<()> { +/// use serde::Serialize; +/// use serde_dhall::{serialize, StaticType}; +/// +/// #[derive(Serialize, StaticType)] +/// enum MyOption { +/// MyNone, +/// MySome(u64), +/// } +/// +/// let data = MyOption::MySome(0); +/// let string = serialize(&data) +/// .static_type_annotation() +/// .to_string()?; +/// // The resulting Dhall string depends on the type annotation; it could not have been +/// // printed without it. +/// assert_eq!(string, "< MyNone | MySome: Natural >.MySome 0".to_string()); +/// # Ok(()) +/// # } +/// ``` +/// +/// [`Serializer`]: struct.Serializer.html +/// [`type_annotation`]: struct.Serializer.html#method.type_annotation +/// [`static_type_annotation`]: struct.Serializer.html#method.static_type_annotation +/// [`to_string`]: struct.Serializer.html#method.to_string pub fn serialize<'a, T>(data: &'a T) -> Serializer<'a, T, NoAnnot> where T: ToDhall, diff --git a/serde_dhall/src/serialize.rs b/serde_dhall/src/serialize.rs index 21b1931..632dace 100644 --- a/serde_dhall/src/serialize.rs +++ b/serde_dhall/src/serialize.rs @@ -9,6 +9,34 @@ use SimpleValue::*; pub trait Sealed {} +/// A data structure that can be serialized from a Dhall expression. +/// +/// This is automatically implemented for any type that [serde] can serialize. +/// In fact, this trait cannot be implemented manually. To implement it for your type, +/// use serde's derive mechanism. +/// +/// # Example +/// +/// ```rust +/// # fn main() -> serde_dhall::Result<()> { +/// use serde::Serialize; +/// +/// // Use serde's derive +/// #[derive(Serialize)] +/// struct Point { +/// x: u64, +/// y: u64, +/// } +/// +/// // Convert a Point to a Dhall string. +/// let point = Point { x: 0, y: 0 }; +/// let point_str = serde_dhall::serialize(&point).to_string()?; +/// assert_eq!(point_str, "{ x = 0, y = 0 }".to_string()); +/// # Ok(()) +/// # } +/// ``` +/// +/// [serde]: https://serde.rs pub trait ToDhall: Sealed { #[doc(hidden)] fn to_dhall(&self, ty: Option<&SimpleType>) -> Result; -- cgit v1.2.3 From 48bb9c03b0ace9efa33315e06c47a868e5c4ed36 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 28 Oct 2020 22:43:34 +0000 Subject: Move table of type correspondances --- serde_dhall/src/static_type.rs | 26 ++++---------------------- serde_dhall/src/value.rs | 23 ++++++++++++++++++++++- 2 files changed, 26 insertions(+), 23 deletions(-) (limited to 'serde_dhall') diff --git a/serde_dhall/src/static_type.rs b/serde_dhall/src/static_type.rs index 7e6c27c..586cd4d 100644 --- a/serde_dhall/src/static_type.rs +++ b/serde_dhall/src/static_type.rs @@ -10,6 +10,10 @@ use crate::SimpleType; /// corresponds to them. For example, `HashMap` could correspond to multiple different /// Dhall types, e.g. `{ foo: Natural, bar: Natural }` and `{ baz: Natural }`. /// +/// See also [the table of type correspondances]. +/// +/// [the table of type correspondances]: enum.SimpleType.html#type-correspondence +/// /// # Example /// /// ```rust @@ -29,28 +33,6 @@ use crate::SimpleType; /// # Ok(()) /// # } /// ``` -/// -/// # Type correspondence -/// -/// The following Dhall types correspond to the following Rust types: -/// -/// Dhall | Rust -/// -------|------ -/// `Bool` | `bool` -/// `Natural` | `u64`, `u32`, ... -/// `Integer` | `i64`, `i32`, ... -/// `Double` | `f64`, `f32`, ... -/// `Text` | `String` -/// `List T` | `Vec` -/// `Optional T` | `Option` -/// `{ x: T, y: U }` | structs -/// `{ _1: T, _2: U }` | `(T, U)`, structs -/// `{ x: T, y: T }` | `HashMap`, structs -/// `< x: T \| y: U >` | enums -/// `Prelude.Map.Type Text T` | `HashMap`, structs -/// `T -> U` | unsupported -/// `Prelude.JSON.Type` | unsupported -/// `Prelude.Map.Type T U` | unsupported pub trait StaticType { /// Return the Dhall type that represents this type. /// diff --git a/serde_dhall/src/value.rs b/serde_dhall/src/value.rs index 50c12bd..b8ad199 100644 --- a/serde_dhall/src/value.rs +++ b/serde_dhall/src/value.rs @@ -124,6 +124,28 @@ pub enum SimpleValue { /// [`StaticType`]: trait.StaticType.html /// [`Deserializer::static_type_annotation`]: options/struct.Deserializer.html#method.static_type_annotation /// +/// # Type correspondence +/// +/// The following Dhall types correspond to the following Rust types: +/// +/// Dhall | Rust +/// -------|------ +/// `Bool` | `bool` +/// `Natural` | `u64`, `u32`, ... +/// `Integer` | `i64`, `i32`, ... +/// `Double` | `f64`, `f32`, ... +/// `Text` | `String` +/// `List T` | `Vec` +/// `Optional T` | `Option` +/// `{ x: T, y: U }` | structs +/// `{ _1: T, _2: U }` | `(T, U)`, structs +/// `{ x: T, y: T }` | `HashMap`, structs +/// `< x: T \| y: U >` | enums +/// `Prelude.Map.Type Text T` | `HashMap`, structs +/// `T -> U` | unsupported +/// `Prelude.JSON.Type` | unsupported +/// `Prelude.Map.Type T U` | unsupported +/// /// # Examples /// /// ```rust @@ -159,7 +181,6 @@ pub enum SimpleValue { /// # Ok(()) /// # } /// ``` -/// #[derive(Debug, Clone, PartialEq, Eq)] pub enum SimpleType { /// Corresponds to the Dhall type `Bool` -- cgit v1.2.3 From 93ed3cf67c49bf7016b8b1780d873cfdffcb84c5 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 28 Oct 2020 23:21:19 +0000 Subject: Implement SimpleValue serialization --- serde_dhall/src/serialize.rs | 46 ++++++++++++++++++++++++++++++++++++++- serde_dhall/tests/simple_value.rs | 31 +++++++++++++------------- 2 files changed, 61 insertions(+), 16 deletions(-) (limited to 'serde_dhall') diff --git a/serde_dhall/src/serialize.rs b/serde_dhall/src/serialize.rs index 632dace..286c04d 100644 --- a/serde_dhall/src/serialize.rs +++ b/serde_dhall/src/serialize.rs @@ -347,6 +347,50 @@ impl serde::ser::Serialize for SimpleValue { where S: serde::ser::Serializer, { - todo!() + use serde::ser::{SerializeMap, SerializeSeq}; + use NumKind::*; + use SimpleValue::*; + + match self { + Num(Bool(x)) => serializer.serialize_bool(*x), + Num(Natural(x)) => serializer.serialize_u64(*x), + Num(Integer(x)) => serializer.serialize_i64(*x), + Num(Double(x)) => serializer.serialize_f64((*x).into()), + Text(x) => serializer.serialize_str(x), + List(xs) => { + let mut seq = serializer.serialize_seq(Some(xs.len()))?; + for x in xs { + seq.serialize_element(x)?; + } + seq.end() + } + Optional(None) => serializer.serialize_none(), + Optional(Some(x)) => serializer.serialize_some(x), + Record(m) => { + let mut map = serializer.serialize_map(Some(m.len()))?; + for (k, v) in m { + map.serialize_entry(k, v)?; + } + map.end() + } + // serde's enum support is yet again really limited. We can't avoid a memleak here :(. + Union(field_name, None) => { + let field_name: Box = field_name.clone().into(); + serializer.serialize_unit_variant( + "SimpleValue", + 0, + Box::leak(field_name), + ) + } + Union(field_name, Some(x)) => { + let field_name: Box = field_name.clone().into(); + serializer.serialize_newtype_variant( + "SimpleValue", + 0, + Box::leak(field_name), + x, + ) + } + } } } diff --git a/serde_dhall/tests/simple_value.rs b/serde_dhall/tests/simple_value.rs index d2792d4..3bd9d64 100644 --- a/serde_dhall/tests/simple_value.rs +++ b/serde_dhall/tests/simple_value.rs @@ -31,27 +31,28 @@ mod simple_value { fn test_serde() { let bool_true = SimpleValue::Num(NumKind::Bool(true)); // https://github.com/Nadrieril/dhall-rust/issues/184 - // assert_serde("[ True ]", vec![bool_true.clone()]); + assert_serde("[True]", vec![bool_true.clone()]); + assert_de("< Foo >.Foo", SimpleValue::Union("Foo".into(), None)); assert_de( "< Foo: Bool >.Foo True", SimpleValue::Union("Foo".into(), Some(Box::new(bool_true.clone()))), ); - // assert_eq!( - // serialize(&SimpleValue::Optional(None)).to_string().map_err(|e| e.to_string()), - // Err("cannot serialize value without a type annotation: Optional(None)".to_string()) - // ); + assert_eq!( + serialize(&SimpleValue::Union("Foo".into(), None)).to_string().map_err(|e| e.to_string()), + Err("cannot serialize value without a type annotation: Union(\"Foo\", None)".to_string()) + ); - // #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] - // struct Foo { - // foo: SimpleValue, - // } - // assert_serde( - // "{ foo = True }", - // Foo { - // foo: bool_true.clone(), - // }, - // ); + #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] + struct Foo { + foo: SimpleValue, + } + assert_serde( + "{ foo = True }", + Foo { + foo: bool_true.clone(), + }, + ); } } -- cgit v1.2.3 From 1a53f10c6ed1ef65621b39c40e05da79161047cc Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 28 Oct 2020 23:26:22 +0000 Subject: Move some tests --- serde_dhall/tests/de.rs | 61 ---------------------------------------------- serde_dhall/tests/serde.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 61 deletions(-) delete mode 100644 serde_dhall/tests/de.rs (limited to 'serde_dhall') diff --git a/serde_dhall/tests/de.rs b/serde_dhall/tests/de.rs deleted file mode 100644 index f49bee4..0000000 --- a/serde_dhall/tests/de.rs +++ /dev/null @@ -1,61 +0,0 @@ -use serde::Deserialize; -use serde_dhall::{from_str, FromDhall}; - -#[test] -fn test_de_untyped() { - use std::collections::BTreeMap; - use std::collections::HashMap; - - fn parse(s: &str) -> T { - from_str(s).parse().unwrap() - } - - // Test tuples on record of wrong type - assert_eq!( - parse::<(u64, String, i64)>(r#"{ y = "foo", x = 1, z = +42 }"#), - (1, "foo".to_owned(), 42) - ); - - let mut expected_map = HashMap::new(); - expected_map.insert("x".to_string(), 1); - expected_map.insert("y".to_string(), 2); - assert_eq!( - parse::>("{ x = 1, y = 2 }"), - expected_map - ); - assert_eq!( - parse::>("toMap { x = 1, y = 2 }"), - expected_map - ); - - let mut expected_map = HashMap::new(); - expected_map.insert("if".to_string(), 1); - expected_map.insert("FOO_BAR".to_string(), 2); - expected_map.insert("baz-kux".to_string(), 3); - assert_eq!( - parse::>("{ `if` = 1, FOO_BAR = 2, baz-kux = 3 }"), - expected_map - ); - - let mut expected_map = BTreeMap::new(); - expected_map.insert("x".to_string(), 1); - expected_map.insert("y".to_string(), 2); - assert_eq!( - parse::>("{ x = 1, y = 2 }"), - expected_map - ); - - #[derive(Debug, PartialEq, Eq, Deserialize)] - struct Foo { - x: u64, - y: Option, - } - // Omit optional field - assert_eq!(parse::("{ x = 1 }"), Foo { x: 1, y: None }); - - // https://github.com/Nadrieril/dhall-rust/issues/155 - assert!(from_str("List/length [True, 42]").parse::().is_err()); -} - -// TODO: test various builder configurations -// In particular test cloning and reusing builder diff --git a/serde_dhall/tests/serde.rs b/serde_dhall/tests/serde.rs index ce25380..03bcc58 100644 --- a/serde_dhall/tests/serde.rs +++ b/serde_dhall/tests/serde.rs @@ -146,4 +146,63 @@ mod serde { .parse::() .is_err()); } + + #[test] + fn test_de_untyped() { + use std::collections::BTreeMap; + use std::collections::HashMap; + + fn parse(s: &str) -> T { + from_str(s).parse().unwrap() + } + + // Test tuples on record of wrong type. Fields are just taken in alphabetic order. + assert_eq!( + parse::<(u64, String, i64)>(r#"{ y = "foo", x = 1, z = +42 }"#), + (1, "foo".to_owned(), 42) + ); + + let mut expected_map = HashMap::new(); + expected_map.insert("x".to_string(), 1); + expected_map.insert("y".to_string(), 2); + assert_eq!( + parse::>("{ x = 1, y = 2 }"), + expected_map + ); + assert_eq!( + parse::>("toMap { x = 1, y = 2 }"), + expected_map + ); + + let mut expected_map = HashMap::new(); + expected_map.insert("if".to_string(), 1); + expected_map.insert("FOO_BAR".to_string(), 2); + expected_map.insert("baz-kux".to_string(), 3); + assert_eq!( + parse::>("{ `if` = 1, FOO_BAR = 2, baz-kux = 3 }"), + expected_map + ); + + let mut expected_map = BTreeMap::new(); + expected_map.insert("x".to_string(), 1); + expected_map.insert("y".to_string(), 2); + assert_eq!( + parse::>("{ x = 1, y = 2 }"), + expected_map + ); + + #[derive(Debug, PartialEq, Eq, Deserialize)] + struct Foo { + x: u64, + y: Option, + } + // Omit optional field + assert_eq!(parse::("{ x = 1 }"), Foo { x: 1, y: None }); + + // https://github.com/Nadrieril/dhall-rust/issues/155 + assert!(from_str("List/length [True, 42]").parse::().is_err()); + } + + // TODO: test various builder configurations + // In particular test cloning and reusing builder } -- cgit v1.2.3 From af16e9699799f8cfbd228e2832e1d5df3653116b Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 28 Oct 2020 23:31:57 +0000 Subject: Fix clippy and formatting --- serde_dhall/src/options/ser.rs | 2 +- serde_dhall/tests/serde.rs | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'serde_dhall') diff --git a/serde_dhall/src/options/ser.rs b/serde_dhall/src/options/ser.rs index 3332735..d74beb0 100644 --- a/serde_dhall/src/options/ser.rs +++ b/serde_dhall/src/options/ser.rs @@ -223,7 +223,7 @@ where /// [`type_annotation`]: struct.Serializer.html#method.type_annotation /// [`static_type_annotation`]: struct.Serializer.html#method.static_type_annotation /// [`to_string`]: struct.Serializer.html#method.to_string -pub fn serialize<'a, T>(data: &'a T) -> Serializer<'a, T, NoAnnot> +pub fn serialize(data: &T) -> Serializer<'_, T, NoAnnot> where T: ToDhall, { diff --git a/serde_dhall/tests/serde.rs b/serde_dhall/tests/serde.rs index 03bcc58..4c184e7 100644 --- a/serde_dhall/tests/serde.rs +++ b/serde_dhall/tests/serde.rs @@ -179,7 +179,9 @@ mod serde { expected_map.insert("FOO_BAR".to_string(), 2); expected_map.insert("baz-kux".to_string(), 3); assert_eq!( - parse::>("{ `if` = 1, FOO_BAR = 2, baz-kux = 3 }"), + parse::>( + "{ `if` = 1, FOO_BAR = 2, baz-kux = 3 }" + ), expected_map ); -- cgit v1.2.3