summaryrefslogtreecommitdiff
path: root/serde_dhall/src/options
diff options
context:
space:
mode:
authorNadrieril2020-11-20 21:35:58 +0000
committerNadrieril2020-11-20 21:36:34 +0000
commit0415bc0ce299dfc707b7f8dc2ee26ceb57bce84e (patch)
treebacc3cc0fec11daae2175e161a530c31248e71fa /serde_dhall/src/options
parentfe9b732d61cfa347fc220887ca27a8439745c82a (diff)
Expose binary parsing in the API
Diffstat (limited to 'serde_dhall/src/options')
-rw-r--r--serde_dhall/src/options/de.rs41
1 files changed, 41 insertions, 0 deletions
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)
// }