From e1a30c6f248c0c17c97598290a0d94993dbb0325 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 10 Apr 2019 19:16:11 +0200 Subject: Add SimpleType and SimpeStaticType. Derive the latter --- dhall/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'dhall/src/lib.rs') diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs index 5c5a641..3bfc46f 100644 --- a/dhall/src/lib.rs +++ b/dhall/src/lib.rs @@ -21,6 +21,7 @@ mod imports; mod normalize; mod traits; mod typecheck; +pub use crate::traits::SimpleStaticType; pub use crate::traits::StaticType; -pub use dhall_generator::StaticType; +pub use dhall_generator::SimpleStaticType; pub mod expr; -- cgit v1.2.3 From ff12918696181f1b0f2b8272944044e27c89e319 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 10 Apr 2019 19:18:25 +0200 Subject: Add a new Deserialize trait for reading dhall values --- dhall/src/lib.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'dhall/src/lib.rs') diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs index 3bfc46f..b49d1c1 100644 --- a/dhall/src/lib.rs +++ b/dhall/src/lib.rs @@ -21,7 +21,21 @@ mod imports; mod normalize; mod traits; mod typecheck; +pub use crate::traits::Deserialize; pub use crate::traits::SimpleStaticType; pub use crate::traits::StaticType; pub use dhall_generator::SimpleStaticType; pub mod expr; + +pub fn from_str<'a, T: Deserialize<'a>>( + s: &'a str, + ty: Option<&crate::expr::Type>, +) -> crate::traits::Result { + T::from_str(s, ty) +} + +pub fn from_str_auto_type<'a, T: Deserialize<'a> + StaticType>( + s: &'a str, +) -> crate::traits::Result { + from_str(s, Some(&::get_static_type())) +} -- cgit v1.2.3 From 3473b504645a0e1986f32b93b9cae93d5659c861 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 10 Apr 2019 19:25:13 +0200 Subject: Prepare for serde support --- dhall/src/lib.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'dhall/src/lib.rs') diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs index b49d1c1..2d44115 100644 --- a/dhall/src/lib.rs +++ b/dhall/src/lib.rs @@ -26,6 +26,7 @@ pub use crate::traits::SimpleStaticType; pub use crate::traits::StaticType; pub use dhall_generator::SimpleStaticType; pub mod expr; +pub mod serde; pub fn from_str<'a, T: Deserialize<'a>>( s: &'a str, -- cgit v1.2.3 From 838f6c0a25d4024ee5f32ddde915fdd2f759018d Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 10 Apr 2019 19:25:55 +0200 Subject: Write main lib doc --- dhall/src/lib.rs | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) (limited to 'dhall/src/lib.rs') diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs index 2d44115..ababc5d 100644 --- a/dhall/src/lib.rs +++ b/dhall/src/lib.rs @@ -9,6 +9,112 @@ clippy::many_single_char_names )] +//! [Dhall][dhall] is a programmable configuration language that provides a non-repetitive +//! alternative to JSON and YAML. +//! +//! You can think of Dhall as: JSON/YAML + types + imports + functions +//! +//! For a description of the dhall language, examples, tutorials, and more, see the [language +//! website][dhall]. +//! +//! This crate provides support for consuming dhall files the same way you would consume JSON or +//! YAML. It uses the [Serde][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 [SimpleStaticType] for it as well. This gives you access to +//! a dhall representation of your datatype that can be outputted to users, and +//! allows easy type-safe deserializing. +//! +//! ```edition2018 +//! use serde::Deserialize; +//! use dhall::SimpleStaticType; +//! +//! #[derive(Debug, Deserialize, SimpleStaticType)] +//! struct Point { +//! x: usize, +//! y: usize, +//! } +//! +//! fn main() { +//! // Some dhall data +//! // ./Point can be populated using Point::get_type().to_string() +//! let data = "{ x = 1, y = 1 + 1 } : ./Point"; +//! +//! // Convert the dhall string to a Point. +//! let point: Point = +//! dhall::from_str_auto_type(&data) +//! .expect("An error ocurred !"); +//! +//! // Prints "point = Point { x: 1, y: 2 }" +//! println!("point = {:?}", point); +//! } +//! ``` +//! +//! ### 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 [dhall::from_str][from_str]. +//! More generally, if the [SimpleStaticType] derive doesn't suit your needs, you can still +//! deserialize any valid dhall file that serde can handle. +//! +//! [serde_json::from_str]: https://docs.serde.rs/serde_json/de/fn.from_str.html +//! [serde_yaml::from_str]: https://docs.serde.rs/serde_yaml/fn.from_str.html +//! +//! ```edition2018 +//! use std::collections::BTreeMap; +//! +//! let mut map = BTreeMap::new(); +//! map.insert("x".to_string(), 1); +//! map.insert("y".to_string(), 2); +//! +//! // Some dhall data +//! let data = "{ x = 1, y = 1 + 1 } : { x: Natural, y: Natural }"; +//! +//! // Deserialize it to a Rust type. +//! let deserialized_map: BTreeMap = +//! dhall::from_str(&data, None) +//! .expect("Failed reading the data !"); +//! assert_eq!(map, deserialized_map); +//! ``` +//! +//! You can of course specify a dhall type that the input should match. +//! +//! ```edition2018 +//! use std::collections::BTreeMap; +//! +//! let mut map = BTreeMap::new(); +//! map.insert("x".to_string(), 1); +//! map.insert("y".to_string(), 2); +//! +//! // Some dhall data +//! let point_data = "{ x = 1, y = 1 + 1 }"; +//! let point_type_data = "{ x: Natural, y: Natural }"; +//! +//! // Construct a type +//! let point_type = +//! dhall::from_str(point_type_data, None) +//! .expect("Could not parse the Point type"); +//! +//! // Deserialize it to a Rust type. +//! let deserialized_map: BTreeMap = +//! dhall::from_str(&point_data, Some(&point_type)) +//! .expect("Failed reading the data !"); +//! assert_eq!(map, deserialized_map); +//! ``` +//! +//! [dhall]: https://dhall-lang.org/ +//! [serde]: https://docs.serde.rs/serde/ +//! [serde::Deserialize]: https://docs.serde.rs/serde/trait.Deserialize.html + #[cfg(test)] #[macro_use] mod tests; -- cgit v1.2.3 From 730d99adf2d4a7f222a71d687ea942545a7038fd Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 10 Apr 2019 19:55:50 +0200 Subject: Add error submodule --- dhall/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'dhall/src/lib.rs') diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs index ababc5d..c218aeb 100644 --- a/dhall/src/lib.rs +++ b/dhall/src/lib.rs @@ -131,18 +131,19 @@ pub use crate::traits::Deserialize; pub use crate::traits::SimpleStaticType; pub use crate::traits::StaticType; pub use dhall_generator::SimpleStaticType; +pub mod error; pub mod expr; pub mod serde; pub fn from_str<'a, T: Deserialize<'a>>( s: &'a str, ty: Option<&crate::expr::Type>, -) -> crate::traits::Result { +) -> crate::error::Result { T::from_str(s, ty) } pub fn from_str_auto_type<'a, T: Deserialize<'a> + StaticType>( s: &'a str, -) -> crate::traits::Result { +) -> crate::error::Result { from_str(s, Some(&::get_static_type())) } -- cgit v1.2.3 From 82d62c4d7d423817a4fd9d6294d27d18d60bcd22 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Thu, 11 Apr 2019 13:11:52 +0200 Subject: Add basic deserialization support Closes #13 --- dhall/src/lib.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'dhall/src/lib.rs') diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs index c218aeb..8af5af9 100644 --- a/dhall/src/lib.rs +++ b/dhall/src/lib.rs @@ -39,14 +39,13 @@ //! //! #[derive(Debug, Deserialize, SimpleStaticType)] //! struct Point { -//! x: usize, -//! y: usize, +//! x: u64, +//! y: u64, //! } //! //! fn main() { //! // Some dhall data -//! // ./Point can be populated using Point::get_type().to_string() -//! let data = "{ x = 1, y = 1 + 1 } : ./Point"; +//! let data = "{ x = 1, y = 1 + 1 }"; //! //! // Convert the dhall string to a Point. //! let point: Point = -- cgit v1.2.3