Struct serde_dhall::Deserializer [−][src]
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<T>
where T
is a deserializable type of your choice.
Examples
Reading from a file:
use serde_dhall::from_file; let data = from_file("foo.dhall").parse::<u64>()?;
Reading from a file and checking the value against a provided type:
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::<HashMap<String, u64>>()?;
Implementations
impl<'a> Deserializer<'a, NoAnnot>
[src]
pub fn type_annotation<'ty>(
self,
ty: &'ty SimpleType
) -> Deserializer<'a, ManualAnnot<'ty>>
[src]
self,
ty: &'ty SimpleType
) -> Deserializer<'a, ManualAnnot<'ty>>
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
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::<SimpleType>()?; // Parse some Dhall data. let data = "{ x = 1, y = 1 + 1 }"; let point = from_str(data) .type_annotation(&ty) .parse::<HashMap<String, u64>>()?; 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::<HashMap<String, u64>>() .is_err() );
pub fn static_type_annotation(self) -> Deserializer<'a, StaticAnnot>
[src]
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
use serde::Deserialize; use serde_dhall::StaticType; #[derive(Deserialize, StaticType)] struct Point { x: u64, y: Option<u64>, } // 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::<Point>()?; 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::<Point>() .is_err() );
impl<'a, A> Deserializer<'a, A>
[src]
pub fn imports(self, imports: bool) -> Self
[src]
Sets whether to enable imports.
By default, imports are enabled.
Example
use serde::Deserialize; use serde_dhall::SimpleType; let data = "12 + ./other_file.dhall : Natural"; assert!( serde_dhall::from_str(data) .imports(false) .parse::<u64>() .is_err() );
pub fn parse<T>(&self) -> Result<T> where
A: TypeAnnot,
T: FromDhall + HasAnnot<A>,
[src]
A: TypeAnnot,
T: FromDhall + HasAnnot<A>,
Parses the chosen dhall value with the options provided.
If you enabled static annotations, T
is required to implement StaticType
.
Example
let data = serde_dhall::from_str("6 * 7").parse::<u64>()?; assert_eq!(data, 42);
Trait Implementations
impl<'a, A: Clone> Clone for Deserializer<'a, A>
[src]
fn clone(&self) -> Deserializer<'a, A>
[src]
pub fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl<'a, A: Debug> Debug for Deserializer<'a, A>
[src]
Auto Trait Implementations
impl<'a, A> RefUnwindSafe for Deserializer<'a, A> where
A: RefUnwindSafe,
[src]
A: RefUnwindSafe,
impl<'a, A> Send for Deserializer<'a, A> where
A: Send,
[src]
A: Send,
impl<'a, A> Sync for Deserializer<'a, A> where
A: Sync,
[src]
A: Sync,
impl<'a, A> Unpin for Deserializer<'a, A> where
A: Unpin,
[src]
A: Unpin,
impl<'a, A> UnwindSafe for Deserializer<'a, A> where
A: UnwindSafe,
[src]
A: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,