[−][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.
Basic usage
The main entrypoint of this library is the from_str
function. It reads a string
containing a Dhall expression and deserializes it into any serde-compatible type.
This could mean a common Rust type like HashMap
:
use std::collections::HashMap; // Some Dhall data let data = "{ x = 1, y = 1 + 1 } : { x: Natural, y: Natural }"; // Deserialize it to a Rust type. let deserialized_map: HashMap<String, usize> = serde_dhall::from_str(data).parse()?; let mut expected_map = HashMap::new(); expected_map.insert("x".to_string(), 1); expected_map.insert("y".to_string(), 2); assert_eq!(deserialized_map, expected_map);
or a custom datatype, using serde's derive
mechanism:
use serde::Deserialize; #[derive(Deserialize)] struct Point { x: u64, y: u64, } // Some Dhall data let data = "{ x = 1, y = 1 + 1 } : { x: Natural, y: Natural }"; // Convert the Dhall string to a Point. let point: Point = serde_dhall::from_str(data).parse()?; assert_eq!(point.x, 1); assert_eq!(point.y, 2);
Replacing serde_json
or serde_yaml
If you used to consume JSON or YAML, you only need to replace serde_json::from_str
or
serde_yaml::from_str
with serde_dhall::from_str(…).parse()
.
Additional Dhall typechecking
When deserializing, normal type checking is done to ensure that the returned value is a valid Dhall value. However types are first-class in Dhall, and this library allows you to additionally check that the input data matches a given Dhall type. That way, a type error will be caught on the Dhall side, and have pretty and explicit errors that point to the source file.
There are two ways to typecheck a Dhall value in this way: you can provide the type manually or you can let Rust infer it for you.
To let Rust infer the appropriate Dhall type, use the StaticType trait.
use serde::Deserialize; use serde_dhall::StaticType; #[derive(Deserialize, StaticType)] struct Point { x: u64, y: u64, } // Some Dhall data let data = "{ x = 1, y = 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, 2); // Invalid data fails the type validation let invalid_data = "{ x = 1, z = 0.3 }"; assert!( serde_dhall::from_str(invalid_data) .static_type_annotation() .parse::<Point>() .is_err() );
To provide a type manually, you need a SimpleType
value. You
can parse it from some Dhall text like you would parse any other value.
use serde_dhall::SimpleType; use std::collections::HashMap; // Parse a Dhall type let point_type_str = "{ x: Natural, y: Natural }"; let point_type = serde_dhall::from_str(point_type_str).parse::<SimpleType>()?; // Some Dhall data let point_data = "{ x = 1, y = 1 + 1 }"; // Deserialize the data to a Rust type. This checks that // the data matches the provided type. let deserialized_map = serde_dhall::from_str(point_data) .type_annotation(&point_type) .parse::<HashMap<String, usize>>()?; let mut expected_map = HashMap::new(); expected_map.insert("x".to_string(), 1); expected_map.insert("y".to_string(), 2); assert_eq!(deserialized_map, expected_map);
Controlling deserialization
If you need more control over the process of reading Dhall values, e.g. disabling
imports, see the Deserializer
methods.
Structs
Deserializer | Controls how a Dhall value is read. |
Error | Errors that can occur when deserializing Dhall data. |
Enums
SimpleType | The type of a value that can be decoded by |
Traits
FromDhall | A data structure that can be deserialized from a Dhall expression. |
StaticType | A Rust type that can be represented as a Dhall type. |
Functions
from_file | Deserialize a value from a Dhall file. |
from_str | Deserialize a value from a string of Dhall text. |
Type Definitions
Result | Alias for a |