Design of the public API
Closed this issue · 3 comments
Nadrieril commented
The current focus of the Dhall project is "displacing YAML". I would like for dhall-rust
to go in this direction as well.
So the current objective is: what would be the simplest API we can imagine that would enable people to consume Dhall files as easily as YAML, but also profit from the power of Dhall ?
Currently, the API provides (more-or-less) a dhall::from_str(s)
function that can be used as a drop-in replacement for serde_yaml::from_str(s)
. That's a good start, but I think this is too minimal.
What kind of things should we include in the API ? What kind of code would you like to write using this crate ?
Nadrieril commented
Some ideas:
- enable/disable import resolution
- something like
dhall::from_str_with_type(s, type)
that checks that the parsed expression has the given type - I have written a macro that derives a Dhall type for a given Rust type (see e.g. this test). For example,
struct Foo { field1: u64, field2: Option<bool> }
becomes the Dhall type{ field1: Natural, field2: Optional Bool }
. How do we use this ?
Nadrieril commented
Some ideas of the kind of code we could want to write:
// A type
let point_type_str = "{ x: Natural, y: Natural }";
// Parse the type
let point_type = dhall::from_str(point_type_str)?;
// Some Dhall data
let point_str = "{ x = 1, y = 1 + 1 }";
// Deserialize it to a Rust type, checking against the provided type.
let deserialized_map: HashMap<String, u64> =
dhall::from_str_with_type(point_str, &point_type)?;
use serde::Deserialize;
#[derive(Debug, Deserialize, MagicDhallType)]
struct Point {
x: u64,
y: u64,
}
// Some dhall data
let point_str = "{ x = 1, y = 1 + 1 }";
// Convert the dhall string to a Point, checking against the inferred type.
let point: Point = dhall::from_str_with_magic_type(point_str)?;