[][src]Crate serde_dhall

Dhall is a programmable configuration language that provides a non-repetitive alternative to JSON and YAML.

You can think of Dhall as: JSON + types + imports + functions

For a description of the Dhall language, examples, tutorials, and more, see the language website.

This crate provides support for consuming Dhall files the same way you would consume JSON or YAML. It uses the Serde serialization library to provide drop-in support for Dhall for any datatype that supports serde (and that's a lot of them !).

This library is limited to deserializing (reading) Dhall values; serializing (writing) values to Dhall is not supported for now.

Examples

Custom datatype

If you have a custom datatype for which you derived serde::Deserialize, chances are you will be able to derive StaticType for it as well. This allows easy type-safe deserializing.

This code runs with edition 2018
use serde::Deserialize;
use serde_dhall::{de::Error, StaticType};

#[derive(Debug, Deserialize, StaticType)]
struct Point {
    x: u64,
    y: u64,
}

fn main() -> Result<(), Error> {
    // Some Dhall data
    let data = "{ x = 1, y = 1 + 1 }";

    // Convert the Dhall string to a Point.
    let point: Point = serde_dhall::from_str_auto_type(data)?;
    assert_eq!(point.x, 1);
    assert_eq!(point.y, 2);

    // Invalid data fails the type validation
    let invalid_data = "{ x = 1, z = 0.3 }";
    assert!(serde_dhall::from_str_auto_type::<Point>(invalid_data).is_err());

    Ok(())
}

Loosely typed

If you used to consume JSON or YAML in a loosely typed way, you can continue to do so with Dhall. You only need to replace serde_json::from_str or serde_yaml::from_str with serde_dhall::from_str. More generally, if the StaticType derive doesn't suit your needs, you can still deserialize any valid Dhall file that serde can handle.

This code runs with edition 2018
use std::collections::BTreeMap;

// Some Dhall data
let data = "{ x = 1, y = 1 + 1 } : { x: Natural, y: Natural }";

// Deserialize it to a Rust type.
let deserialized_map: BTreeMap<String, usize> = serde_dhall::from_str(data)?;

let mut expected_map = BTreeMap::new();
expected_map.insert("x".to_string(), 1);
expected_map.insert("y".to_string(), 2);

assert_eq!(deserialized_map, expected_map);

You can alternatively specify a Dhall type that the input should match.

This code runs with edition 2018
use std::collections::BTreeMap;

// Parse a Dhall type
let point_type_str = "{ x: Natural, y: Natural }";
let point_type = serde_dhall::from_str(point_type_str)?;

// Some Dhall data
let point_data = "{ x = 1, y = 1 + 1 }";

// Deserialize the data to a Rust type. This ensures that
// the data matches the point type.
let deserialized_map: BTreeMap<String, usize> =
        serde_dhall::from_str_check_type(point_data, &point_type)?;

let mut expected_map = BTreeMap::new();
expected_map.insert("x".to_string(), 1);
expected_map.insert("y".to_string(), 2);

assert_eq!(deserialized_map, expected_map);

Modules

de

Deserialize Dhall data to a Rust data structure.

value

Structs

Value

A Dhall value

Traits

StaticType

A Rust type that can be represented as a Dhall type.

Functions

from_str

Deserialize an instance of type T from a string of Dhall text.

from_str_auto_type

Deserialize an instance of type T from a string of Dhall text, additionally checking that it matches the type of T.

from_str_check_type

Deserialize an instance of type T from a string of Dhall text, additionally checking that it matches the supplied type.