diff options
author | Nadrieril | 2019-04-14 23:52:34 +0200 |
---|---|---|
committer | Nadrieril | 2019-04-14 23:53:02 +0200 |
commit | 89667f976dc6ed006c9b983dd64301cf477b9008 (patch) | |
tree | 9652985c5378fa06e20d24406e7f64b37d909ae1 /dhall | |
parent | 4c840270cf69f9580b18c43736bd247cab74e896 (diff) |
Namespace deserialization functions in a `de` submodule
Closes #73
Diffstat (limited to 'dhall')
-rw-r--r-- | dhall/src/lib.rs | 84 | ||||
-rw-r--r-- | dhall/src/tests.rs | 2 | ||||
-rw-r--r-- | dhall/src/traits/deserialize.rs | 2 | ||||
-rw-r--r-- | dhall/tests/traits.rs | 6 |
4 files changed, 49 insertions, 45 deletions
diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs index 37686e9..883bf05 100644 --- a/dhall/src/lib.rs +++ b/dhall/src/lib.rs @@ -31,13 +31,13 @@ //! ### 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. +//! you will be able to derive [SimpleStaticType][de::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; +//! use dhall::de::SimpleStaticType; //! //! #[derive(Debug, Deserialize, SimpleStaticType)] //! struct Point { @@ -51,7 +51,7 @@ //! //! // Convert the dhall string to a Point. //! let point: Point = -//! dhall::from_str_auto_type(&data) +//! dhall::de::from_str_auto_type(&data) //! .expect("An error ocurred !"); //! //! // Prints "point = Point { x: 1, y: 2 }" @@ -63,9 +63,9 @@ //! //! 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. +//! with [dhall::de::from_str][de::from_str]. +//! More generally, if the [SimpleStaticType][de::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 @@ -82,7 +82,7 @@ //! //! // Deserialize it to a Rust type. //! let deserialized_map: BTreeMap<String, usize> = -//! dhall::from_str(&data, None) +//! dhall::de::from_str(&data, None) //! .expect("Failed reading the data !"); //! assert_eq!(map, deserialized_map); //! ``` @@ -102,12 +102,12 @@ //! //! // Construct a type //! let point_type = -//! dhall::from_str(point_type_data, None) +//! dhall::de::from_str(point_type_data, None) //! .expect("Could not parse the Point type"); //! //! // Deserialize it to a Rust type. //! let deserialized_map: BTreeMap<String, usize> = -//! dhall::from_str(&point_data, Some(&point_type)) +//! dhall::de::from_str(&point_data, Some(&point_type)) //! .expect("Failed reading the data !"); //! assert_eq!(map, deserialized_map); //! ``` @@ -128,39 +128,43 @@ mod imports; mod normalize; mod traits; mod typecheck; -pub use crate::traits::{Deserialize, SimpleStaticType, StaticType}; -#[doc(hidden)] -pub use dhall_generator::SimpleStaticType; /// When manipulating Dhall expressions goes wrong. pub mod error; pub mod expr; mod serde; -/// Deserialize an instance of type T from a string of Dhall text. -/// -/// This will recursively resolve all imports in the expression, and -/// typecheck it before deserialization. Relative imports will be resolved relative to the -/// provided file. More control over this process is not yet available -/// but will be in a coming version of this crate. -/// -/// If a type is provided, this additionally checks that the provided -/// expression has that type. -pub fn from_str<'a, T: Deserialize<'a>>( - s: &'a str, - ty: Option<&crate::expr::Type>, -) -> crate::error::Result<T> { - T::from_str(s, ty) -} +/// Deserialization of Dhall expressions into Rust +pub mod de { + pub use crate::traits::{Deserialize, SimpleStaticType, StaticType}; + #[doc(hidden)] + pub use dhall_generator::SimpleStaticType; + + /// Deserialize an instance of type T from a string of Dhall text. + /// + /// This will recursively resolve all imports in the expression, and + /// typecheck it before deserialization. Relative imports will be resolved relative to the + /// provided file. More control over this process is not yet available + /// but will be in a coming version of this crate. + /// + /// If a type is provided, this additionally checks that the provided + /// expression has that type. + pub fn from_str<'a, T: Deserialize<'a>>( + s: &'a str, + ty: Option<&crate::expr::Type>, + ) -> crate::error::Result<T> { + T::from_str(s, ty) + } -/// Deserialize an instance of type T from a string of Dhall text, -/// additionally checking that it matches the type of T. -/// -/// This will recursively resolve all imports in the expression, and -/// typecheck it before deserialization. Relative imports will be resolved relative to the -/// provided file. More control over this process is not yet available -/// but will be in a coming version of this crate. -pub fn from_str_auto_type<'a, T: Deserialize<'a> + StaticType>( - s: &'a str, -) -> crate::error::Result<T> { - from_str(s, Some(&<T as StaticType>::get_static_type())) + /// Deserialize an instance of type T from a string of Dhall text, + /// additionally checking that it matches the type of T. + /// + /// This will recursively resolve all imports in the expression, and + /// typecheck it before deserialization. Relative imports will be resolved relative to the + /// provided file. More control over this process is not yet available + /// but will be in a coming version of this crate. + pub fn from_str_auto_type<'a, T: Deserialize<'a> + StaticType>( + s: &'a str, + ) -> crate::error::Result<T> { + from_str(s, Some(&<T as StaticType>::get_static_type())) + } } diff --git a/dhall/src/tests.rs b/dhall/src/tests.rs index bb61776..f57f23f 100644 --- a/dhall/src/tests.rs +++ b/dhall/src/tests.rs @@ -112,7 +112,7 @@ pub fn run_test( // Round-trip pretty-printer let expr_string = expr.to_string(); - let expr: Parsed = crate::from_str(&expr_string, None)?; + let expr: Parsed = crate::de::from_str(&expr_string, None)?; assert_eq!(expr, expected); return Ok(()); diff --git a/dhall/src/traits/deserialize.rs b/dhall/src/traits/deserialize.rs index e2e0b05..cbe9aa3 100644 --- a/dhall/src/traits/deserialize.rs +++ b/dhall/src/traits/deserialize.rs @@ -8,7 +8,7 @@ use crate::expr::*; /// /// This trait cannot be implemented manually. pub trait Deserialize<'a>: Sized { - /// See [dhall::from_str][crate::from_str] + /// See [dhall::de::from_str][crate::de::from_str] fn from_str(s: &'a str, ty: Option<&Type>) -> Result<Self>; } diff --git a/dhall/tests/traits.rs b/dhall/tests/traits.rs index ef3e385..00e6509 100644 --- a/dhall/tests/traits.rs +++ b/dhall/tests/traits.rs @@ -1,5 +1,5 @@ #![feature(proc_macro_hygiene)] -use dhall::SimpleStaticType; +use dhall::de::SimpleStaticType; use dhall_core::{SubExpr, X}; use dhall_generator::dhall_expr; @@ -20,14 +20,14 @@ fn test_static_type() { mktype(dhall_expr!({ _1: Bool, _2: Optional Text })) ); - #[derive(dhall::SimpleStaticType)] + #[derive(dhall::de::SimpleStaticType)] #[allow(dead_code)] struct A { field1: bool, field2: Option<bool>, } assert_eq!( - <A as dhall::SimpleStaticType>::get_simple_static_type(), + <A as dhall::de::SimpleStaticType>::get_simple_static_type(), mktype(dhall_expr!({ field1: Bool, field2: Optional Bool })) ); |