diff options
author | Nadrieril | 2019-08-13 15:11:31 +0200 |
---|---|---|
committer | Nadrieril | 2019-08-13 15:13:31 +0200 |
commit | 74bb40e88c71b80ab785f07fd19da22404ab6e95 (patch) | |
tree | fbc1a7a0b224ff30c908308972e3496492580610 | |
parent | 52f9ecfc4dac65d305fd920e8c7f748889a0804f (diff) |
Add new error type for serde_dhall
Diffstat (limited to '')
-rw-r--r-- | dhall/Cargo.toml | 2 | ||||
-rw-r--r-- | dhall/src/error/mod.rs | 12 | ||||
-rw-r--r-- | serde_dhall/src/lib.rs | 58 | ||||
-rw-r--r-- | serde_dhall/src/serde.rs | 12 |
4 files changed, 52 insertions, 32 deletions
diff --git a/dhall/Cargo.toml b/dhall/Cargo.toml index aa52d58..0a257e4 100644 --- a/dhall/Cargo.toml +++ b/dhall/Cargo.toml @@ -10,7 +10,7 @@ build = "build.rs" bytecount = "0.5.1" itertools = "0.8.0" term-painter = "0.2.3" -serde = { version = "1.0", features = ["derive"] } +serde = { version = "1.0" } serde_cbor = "0.9.0" improved_slice_patterns = { version = "2.0.0", path = "../improved_slice_patterns" } dhall_syntax = { path = "../dhall_syntax" } diff --git a/dhall/src/error/mod.rs b/dhall/src/error/mod.rs index ecf3510..3626d96 100644 --- a/dhall/src/error/mod.rs +++ b/dhall/src/error/mod.rs @@ -17,7 +17,6 @@ pub enum Error { Encode(EncodeError), Resolve(ImportError), Typecheck(TypeError), - Deserialize(String), } #[derive(Debug)] @@ -156,7 +155,6 @@ impl std::fmt::Display for Error { Error::Encode(err) => write!(f, "{:?}", err), Error::Resolve(err) => write!(f, "{:?}", err), Error::Typecheck(err) => write!(f, "{:?}", err), - Error::Deserialize(err) => write!(f, "{}", err), } } } @@ -192,13 +190,3 @@ 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/serde_dhall/src/lib.rs b/serde_dhall/src/lib.rs index 1dbbf99..f8f9ae9 100644 --- a/serde_dhall/src/lib.rs +++ b/serde_dhall/src/lib.rs @@ -1,3 +1,5 @@ +#![feature(non_exhaustive)] + //! [Dhall][dhall] is a programmable configuration language that provides a non-repetitive //! alternative to JSON and YAML. //! @@ -109,8 +111,8 @@ pub(crate) mod static_type; pub use value::Value; mod value { + use super::de::{Error, Result}; use super::Type; - use dhall::error::Result; use dhall::phase::{NormalizedSubExpr, Parsed, Typed}; // A Dhall value @@ -118,6 +120,12 @@ mod value { impl Value { pub fn from_str(s: &str, ty: Option<&Type>) -> Result<Self> { + Value::from_str_using_dhall_error_type(s, ty).map_err(Error::Dhall) + } + fn from_str_using_dhall_error_type( + s: &str, + ty: Option<&Type>, + ) -> dhall::error::Result<Self> { let resolved = Parsed::parse_str(s)?.resolve()?; let typed = match ty { None => resolved.typecheck()?, @@ -137,12 +145,12 @@ mod value { pub use typ::Type; mod typ { - use dhall_syntax::Builtin; - use dhall::core::thunk::{Thunk, TypeThunk}; use dhall::core::value::Value; - use dhall::error::Result; use dhall::phase::{NormalizedSubExpr, Typed}; + use dhall_syntax::Builtin; + + use super::de::Result; /// A Dhall expression representing a type. /// @@ -205,21 +213,55 @@ mod typ { } } - impl crate::de::Deserialize for Type { + impl super::de::Deserialize for Type { fn from_dhall(v: &super::Value) -> Result<Self> { Ok(Type(v.to_typed())) } } } +mod error { + use dhall::error::Error as DhallError; + + pub type Result<T> = std::result::Result<T, Error>; + + #[derive(Debug)] + #[non_exhaustive] + pub enum Error { + Dhall(DhallError), + Deserialize(String), + } + + impl std::fmt::Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match self { + Error::Dhall(err) => write!(f, "{}", err), + Error::Deserialize(err) => write!(f, "{}", err), + } + } + } + + impl std::error::Error for Error {} + + impl serde::de::Error for Error { + fn custom<T>(msg: T) -> Self + where + T: std::fmt::Display, + { + Error::Deserialize(msg.to_string()) + } + } +} + /// Deserialization of Dhall expressions into Rust pub mod de { - pub use super::static_type::StaticType; - pub use super::{Type, Value}; - use dhall::error::Result; #[doc(hidden)] pub use dhall_proc_macros::StaticType; + pub use super::error::{Error, Result}; + pub use super::static_type::StaticType; + pub use super::{Type, Value}; + /// A data structure that can be deserialized from a Dhall expression /// /// This is automatically implemented for any type that [serde][serde] diff --git a/serde_dhall/src/serde.rs b/serde_dhall/src/serde.rs index 3dad2d8..1f639e6 100644 --- a/serde_dhall/src/serde.rs +++ b/serde_dhall/src/serde.rs @@ -1,5 +1,4 @@ -use crate::de::{Deserialize, Value}; -use dhall::error::{Error, Result}; +use crate::de::{Deserialize, Error, Result, Value}; use dhall_syntax::{ExprF, SubExpr, X}; use std::borrow::Cow; @@ -14,15 +13,6 @@ 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<'de: 'a, 'a> serde::de::IntoDeserializer<'de, Error> for Deserializer<'a> { type Deserializer = Deserializer<'a>; fn into_deserializer(self) -> Self::Deserializer { |