summaryrefslogtreecommitdiff
path: root/serde_dhall
diff options
context:
space:
mode:
authorNadrieril2020-11-20 21:35:58 +0000
committerNadrieril2020-11-20 21:36:34 +0000
commit0415bc0ce299dfc707b7f8dc2ee26ceb57bce84e (patch)
treebacc3cc0fec11daae2175e161a530c31248e71fa /serde_dhall
parentfe9b732d61cfa347fc220887ca27a8439745c82a (diff)
Expose binary parsing in the API
Diffstat (limited to 'serde_dhall')
-rw-r--r--serde_dhall/src/lib.rs2
-rw-r--r--serde_dhall/src/options/de.rs41
-rw-r--r--serde_dhall/tests/serde.rs21
3 files changed, 63 insertions, 1 deletions
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<P: AsRef<Path>>(path: P) -> Self {
Self::default_with_source(Source::File(path.as_ref().to_owned()))
}
+ fn from_binary_file<P: AsRef<Path>>(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>>(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>>(
+ 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::<bool>().is_err());
}
+ #[test]
+ fn test_file() {
+ assert_eq!(
+ serde_dhall::from_file(
+ "../dhall-lang/tests/parser/success/unit/BoolLitTrueA.dhall"
+ )
+ .static_type_annotation()
+ .parse::<bool>()
+ .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::<bool>()
+ .map_err(|e| e.to_string()),
+ Ok(true)
+ );
+ }
// TODO: test various builder configurations
// In particular test cloning and reusing builder
}