summaryrefslogtreecommitdiff
path: root/serde_dhall/src
diff options
context:
space:
mode:
Diffstat (limited to 'serde_dhall/src')
-rw-r--r--serde_dhall/src/lib.rs58
-rw-r--r--serde_dhall/src/serde.rs12
2 files changed, 51 insertions, 19 deletions
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 {