diff options
Diffstat (limited to '')
-rw-r--r-- | dhall/Cargo.toml | 1 | ||||
-rw-r--r-- | dhall/src/core/mod.rs | 8 | ||||
-rw-r--r-- | dhall/src/error/mod.rs | 10 | ||||
-rw-r--r-- | dhall/src/lib.rs | 112 | ||||
-rw-r--r-- | dhall_proc_macros/src/derive.rs | 14 | ||||
-rw-r--r-- | serde_dhall/src/lib.rs (renamed from dhall/src/api/mod.rs) | 123 | ||||
-rw-r--r-- | serde_dhall/src/serde.rs (renamed from dhall/src/api/serde.rs) | 20 | ||||
-rw-r--r-- | serde_dhall/src/static_type.rs (renamed from dhall/src/api/static_type.rs) | 2 | ||||
-rw-r--r-- | serde_dhall/tests/traits.rs (renamed from dhall/tests/traits.rs) | 6 |
9 files changed, 151 insertions, 145 deletions
diff --git a/dhall/Cargo.toml b/dhall/Cargo.toml index d08627d..aa52d58 100644 --- a/dhall/Cargo.toml +++ b/dhall/Cargo.toml @@ -14,7 +14,6 @@ serde = { version = "1.0", features = ["derive"] } serde_cbor = "0.9.0" improved_slice_patterns = { version = "2.0.0", path = "../improved_slice_patterns" } dhall_syntax = { path = "../dhall_syntax" } -dhall_proc_macros = { path = "../dhall_proc_macros" } [dev-dependencies] pretty_assertions = "0.6.1" diff --git a/dhall/src/core/mod.rs b/dhall/src/core/mod.rs index a202e72..0667df8 100644 --- a/dhall/src/core/mod.rs +++ b/dhall/src/core/mod.rs @@ -1,4 +1,4 @@ -pub(crate) mod context; -pub(crate) mod thunk; -pub(crate) mod value; -pub(crate) mod var; +pub mod context; +pub mod thunk; +pub mod value; +pub mod var; diff --git a/dhall/src/error/mod.rs b/dhall/src/error/mod.rs index 3c00017..ecf3510 100644 --- a/dhall/src/error/mod.rs +++ b/dhall/src/error/mod.rs @@ -192,3 +192,13 @@ impl From<TypeError> for Error { Error::Typecheck(err) } } + +impl serde::de::Error for Error { + fn custom<T>(msg: T) -> Self + where + T: std::fmt::Display, + { + Error::Deserialize(msg.to_string()) + } +} + diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs index ea29869..c360323 100644 --- a/dhall/src/lib.rs +++ b/dhall/src/lib.rs @@ -17,118 +17,10 @@ clippy::ptr_arg )] -//! [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][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 [StaticType][de::StaticType] 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::de::StaticType; -//! -//! #[derive(Debug, Deserialize, StaticType)] -//! struct Point { -//! x: u64, -//! y: u64, -//! } -//! -//! fn main() { -//! // Some dhall data -//! let data = "{ x = 1, y = 1 + 1 }"; -//! -//! // Convert the dhall string to a Point. -//! let point: Point = -//! dhall::de::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::de::from_str][de::from_str]. -//! More generally, if the [StaticType][de::StaticType] 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<String, usize> = -//! dhall::de::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::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::de::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; -pub(crate) mod api; -pub(crate) mod core; +pub mod core; pub mod error; -pub(crate) mod phase; - -pub use api::*; +pub mod phase; diff --git a/dhall_proc_macros/src/derive.rs b/dhall_proc_macros/src/derive.rs index 725cdfb..0ebfe7d 100644 --- a/dhall_proc_macros/src/derive.rs +++ b/dhall_proc_macros/src/derive.rs @@ -18,7 +18,7 @@ where T: quote::ToTokens, { quote!( - <#ty as ::dhall::de::StaticType>::static_type() + <#ty as ::serde_dhall::de::StaticType>::static_type() ) } @@ -53,7 +53,7 @@ fn derive_for_struct( let ty = static_type(ty); quote!( (#name.to_owned(), #ty) ) }); - Ok(quote! { ::dhall::de::Type::make_record_type( + Ok(quote! { ::serde_dhall::de::Type::make_record_type( vec![ #(#entries),* ].into_iter() ) }) } @@ -90,7 +90,7 @@ fn derive_for_enum( }) .collect::<Result<_, Error>>()?; - Ok(quote! { ::dhall::de::Type::make_union_type( + Ok(quote! { ::serde_dhall::de::Type::make_union_type( vec![ #(#entries),* ].into_iter() ) }) } @@ -134,7 +134,7 @@ pub fn derive_static_type_inner( let mut local_where_clause = orig_where_clause.clone(); local_where_clause .predicates - .push(parse_quote!(#ty: ::dhall::de::StaticType)); + .push(parse_quote!(#ty: ::serde_dhall::de::StaticType)); let phantoms = generics.params.iter().map(|param| match param { syn::GenericParam::Type(syn::TypeParam { ident, .. }) => { quote!(#ident) @@ -156,16 +156,16 @@ pub fn derive_static_type_inner( for ty in constraints.iter() { where_clause .predicates - .push(parse_quote!(#ty: ::dhall::de::StaticType)); + .push(parse_quote!(#ty: ::serde_dhall::de::StaticType)); } let ident = &input.ident; let tokens = quote! { - impl #impl_generics ::dhall::de::StaticType + impl #impl_generics ::serde_dhall::de::StaticType for #ident #ty_generics #where_clause { fn static_type() -> - ::dhall::de::Type { + ::serde_dhall::de::Type { #(#assertions)* #get_type } diff --git a/dhall/src/api/mod.rs b/serde_dhall/src/lib.rs index 188b6c0..1dbbf99 100644 --- a/dhall/src/api/mod.rs +++ b/serde_dhall/src/lib.rs @@ -1,3 +1,108 @@ +//! [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][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 [StaticType][de::StaticType] 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 serde_dhall::de::StaticType; +//! +//! #[derive(Debug, Deserialize, StaticType)] +//! struct Point { +//! x: u64, +//! y: u64, +//! } +//! +//! fn main() { +//! // Some dhall data +//! let data = "{ x = 1, y = 1 + 1 }"; +//! +//! // Convert the dhall string to a Point. +//! let point: Point = +//! serde_dhall::de::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 [serde_dhall::de::from_str][de::from_str]. +//! More generally, if the [StaticType][de::StaticType] 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<String, usize> = +//! serde_dhall::de::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 = +//! serde_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> = +//! serde_dhall::de::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 + mod serde; pub(crate) mod static_type; @@ -5,8 +110,8 @@ pub use value::Value; mod value { use super::Type; - use crate::error::Result; - use crate::phase::{NormalizedSubExpr, Parsed, Typed}; + use dhall::error::Result; + use dhall::phase::{NormalizedSubExpr, Parsed, Typed}; // A Dhall value pub struct Value(Typed); @@ -34,10 +139,10 @@ pub use typ::Type; mod typ { use dhall_syntax::Builtin; - use crate::core::thunk::{Thunk, TypeThunk}; - use crate::core::value::Value; - use crate::error::Result; - use crate::phase::{NormalizedSubExpr, Typed}; + use dhall::core::thunk::{Thunk, TypeThunk}; + use dhall::core::value::Value; + use dhall::error::Result; + use dhall::phase::{NormalizedSubExpr, Typed}; /// A Dhall expression representing a type. /// @@ -95,13 +200,13 @@ mod typ { pub(crate) fn to_expr(&self) -> NormalizedSubExpr { self.0.to_expr() } - pub(crate) fn to_type(&self) -> crate::phase::Type { + pub(crate) fn to_type(&self) -> dhall::phase::Type { self.0.to_type() } } impl crate::de::Deserialize for Type { - fn from_dhall(v: &crate::api::Value) -> Result<Self> { + fn from_dhall(v: &super::Value) -> Result<Self> { Ok(Type(v.to_typed())) } } @@ -111,7 +216,7 @@ mod typ { pub mod de { pub use super::static_type::StaticType; pub use super::{Type, Value}; - use crate::error::Result; + use dhall::error::Result; #[doc(hidden)] pub use dhall_proc_macros::StaticType; diff --git a/dhall/src/api/serde.rs b/serde_dhall/src/serde.rs index e1c8eef..3dad2d8 100644 --- a/dhall/src/api/serde.rs +++ b/serde_dhall/src/serde.rs @@ -1,5 +1,5 @@ -use crate::api::de::{Deserialize, Value}; -use crate::error::{Error, Result}; +use crate::de::{Deserialize, Value}; +use dhall::error::{Error, Result}; use dhall_syntax::{ExprF, SubExpr, X}; use std::borrow::Cow; @@ -14,14 +14,14 @@ where struct Deserializer<'a>(Cow<'a, SubExpr<X, X>>); -impl serde::de::Error for Error { - fn custom<T>(msg: T) -> Self - where - T: std::fmt::Display, - { - Error::Deserialize(msg.to_string()) - } -} +// impl serde::de::Error for Error { +// fn custom<T>(msg: T) -> Self +// where +// T: std::fmt::Display, +// { +// Error::Deserialize(msg.to_string()) +// } +// } impl<'de: 'a, 'a> serde::de::IntoDeserializer<'de, Error> for Deserializer<'a> { type Deserializer = Deserializer<'a>; diff --git a/dhall/src/api/static_type.rs b/serde_dhall/src/static_type.rs index 906bcef..13d5d70 100644 --- a/dhall/src/api/static_type.rs +++ b/serde_dhall/src/static_type.rs @@ -1,6 +1,6 @@ use dhall_syntax::{Builtin, Integer, Natural}; -use crate::api::Type; +use crate::Type; /// A Rust type that can be represented as a Dhall type. /// diff --git a/dhall/tests/traits.rs b/serde_dhall/tests/traits.rs index 0f75553..99f1109 100644 --- a/dhall/tests/traits.rs +++ b/serde_dhall/tests/traits.rs @@ -1,5 +1,5 @@ #![feature(proc_macro_hygiene)] -use dhall::de::{from_str, StaticType, Type}; +use serde_dhall::de::{from_str, StaticType, Type}; #[test] fn test_static_type() { @@ -15,14 +15,14 @@ fn test_static_type() { parse("{ _1: Bool, _2: List Text }") ); - #[derive(dhall::de::StaticType)] + #[derive(serde_dhall::de::StaticType)] #[allow(dead_code)] struct A { field1: bool, field2: Option<bool>, } assert_eq!( - <A as dhall::de::StaticType>::static_type(), + <A as serde_dhall::de::StaticType>::static_type(), parse("{ field1: Bool, field2: Optional Bool }") ); |