[][src]Struct serde_dhall::Deserializer

pub struct Deserializer<'a, A> { /* fields omitted */ }

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, usize>>()?;

Implementations

impl<'a> Deserializer<'a, NoAnnot>[src]

pub fn type_annotation<'ty>(
    self,
    ty: &'ty SimpleType
) -> Deserializer<'a, ManualAnnot<'ty>>
[src]

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, usize>>()?;
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, usize>>()
        .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
    T: FromDhall + HasAnnot<A>, 
[src]

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]

impl<'a, A: Debug> Debug for Deserializer<'a, A>[src]

Auto Trait Implementations

impl<'a, A> RefUnwindSafe for Deserializer<'a, A> where
    A: RefUnwindSafe

impl<'a, A> Send for Deserializer<'a, A> where
    A: Send

impl<'a, A> Sync for Deserializer<'a, A> where
    A: Sync

impl<'a, A> Unpin for Deserializer<'a, A> where
    A: Unpin

impl<'a, A> UnwindSafe for Deserializer<'a, A> where
    A: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.