summaryrefslogtreecommitdiff
path: root/serde_dhall/src
diff options
context:
space:
mode:
authorNadrieril2020-11-20 22:46:22 +0000
committerGitHub2020-11-20 22:46:22 +0000
commit4e6cab79e06a999d44604f14d3d7c7c4dad4e502 (patch)
treebacc3cc0fec11daae2175e161a530c31248e71fa /serde_dhall/src
parentfe9b732d61cfa347fc220887ca27a8439745c82a (diff)
parent0415bc0ce299dfc707b7f8dc2ee26ceb57bce84e (diff)
Merge pull request #201 from Nadrieril/parse-binary
Expose binary parsing in the API
Diffstat (limited to 'serde_dhall/src')
-rw-r--r--serde_dhall/src/lib.rs2
-rw-r--r--serde_dhall/src/options/de.rs41
2 files changed, 42 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)
// }