From 0415bc0ce299dfc707b7f8dc2ee26ceb57bce84e Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 20 Nov 2020 21:35:58 +0000 Subject: Expose binary parsing in the API --- serde_dhall/src/lib.rs | 2 +- serde_dhall/src/options/de.rs | 41 +++++++++++++++++++++++++++++++++++++++++ serde_dhall/tests/serde.rs | 21 +++++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) (limited to 'serde_dhall') diff --git a/serde_dhall/src/lib.rs b/serde_dhall/src/lib.rs index 9f8539f..2af445c 100644 --- a/serde_dhall/src/lib.rs +++ b/serde_dhall/src/lib.rs @@ -274,7 +274,7 @@ pub use dhall_proc_macros::StaticType; pub use deserialize::{from_simple_value, FromDhall}; pub(crate) use error::ErrorKind; pub use error::{Error, Result}; -pub use options::de::{from_file, from_str, Deserializer}; +pub use options::de::{from_binary_file, from_file, from_str, Deserializer}; pub use options::ser::{serialize, Serializer}; pub use serialize::ToDhall; pub use static_type::StaticType; diff --git a/serde_dhall/src/options/de.rs b/serde_dhall/src/options/de.rs index e4f3456..22fa7ba 100644 --- a/serde_dhall/src/options/de.rs +++ b/serde_dhall/src/options/de.rs @@ -10,6 +10,7 @@ use crate::{Error, ErrorKind, FromDhall, Result, Value}; enum Source<'a> { Str(&'a str), File(PathBuf), + BinaryFile(PathBuf), // Url(&'a str), } @@ -80,6 +81,9 @@ impl<'a> Deserializer<'a, NoAnnot> { fn from_file>(path: P) -> Self { Self::default_with_source(Source::File(path.as_ref().to_owned())) } + fn from_binary_file>(path: P) -> Self { + Self::default_with_source(Source::BinaryFile(path.as_ref().to_owned())) + } // fn from_url(url: &'a str) -> Self { // Self::default_with_source(Source::Url(url)) // } @@ -233,6 +237,7 @@ impl<'a, A> Deserializer<'a, A> { let parsed = match &self.source { Source::Str(s) => Parsed::parse_str(s)?, Source::File(p) => Parsed::parse_file(p.as_ref())?, + Source::BinaryFile(p) => Parsed::parse_binary_file(p.as_ref())?, }; let resolved = if self.allow_imports { parsed.resolve()? @@ -344,6 +349,42 @@ pub fn from_file<'a, P: AsRef>(path: P) -> Deserializer<'a, NoAnnot> { Deserializer::from_file(path) } +/// Deserialize a value from a CBOR-encoded Dhall binary file. The binary format is specified by +/// the Dhall standard specification and is mostly used for caching expressions. Using the format +/// is not recommended because errors won't have a file to refer to and thus will be hard to fix. +/// +/// 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_binary_file("foo.dhallb").parse()?; +/// # Ok(()) +/// # } +/// ``` +/// +/// [`Deserializer`]: struct.Deserializer.html +/// [`parse`]: struct.Deserializer.html#method.parse +pub fn from_binary_file<'a, P: AsRef>( + path: P, +) -> Deserializer<'a, NoAnnot> { + Deserializer::from_binary_file(path) +} + // pub fn from_url(url: &str) -> Deserializer<'_, NoAnnot> { // Deserializer::from_url(url) // } diff --git a/serde_dhall/tests/serde.rs b/serde_dhall/tests/serde.rs index 4c184e7..1181f72 100644 --- a/serde_dhall/tests/serde.rs +++ b/serde_dhall/tests/serde.rs @@ -205,6 +205,27 @@ mod serde { assert!(from_str("List/length [True, 42]").parse::().is_err()); } + #[test] + fn test_file() { + assert_eq!( + serde_dhall::from_file( + "../dhall-lang/tests/parser/success/unit/BoolLitTrueA.dhall" + ) + .static_type_annotation() + .parse::() + .map_err(|e| e.to_string()), + Ok(true) + ); + assert_eq!( + serde_dhall::from_binary_file( + "../dhall-lang/tests/parser/success/unit/BoolLitTrueB.dhallb" + ) + .static_type_annotation() + .parse::() + .map_err(|e| e.to_string()), + Ok(true) + ); + } // TODO: test various builder configurations // In particular test cloning and reusing builder } -- cgit v1.2.3