From 1a0929b52af57d5963dd9da9e5cf85ffbed3a8f7 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 13 Mar 2020 20:53:23 +0000 Subject: Reorganize serde modules --- serde_dhall/src/error.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 serde_dhall/src/error.rs (limited to 'serde_dhall/src/error.rs') diff --git a/serde_dhall/src/error.rs b/serde_dhall/src/error.rs new file mode 100644 index 0000000..91a0b94 --- /dev/null +++ b/serde_dhall/src/error.rs @@ -0,0 +1,30 @@ +use dhall::error::Error as DhallError; + +pub type Result = std::result::Result; + +#[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(msg: T) -> Self + where + T: std::fmt::Display, + { + Error::Deserialize(msg.to_string()) + } +} -- cgit v1.3.1 From 002d7c2a74647312a821598ac3d9f5521296873d Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 22 Mar 2020 21:25:44 +0000 Subject: Add a bunch of TODOs --- serde_dhall/src/deserialize.rs | 3 ++- serde_dhall/src/error.rs | 2 ++ serde_dhall/src/lib.rs | 5 ++--- serde_dhall/src/options.rs | 9 +++++++++ serde_dhall/src/shortcuts.rs | 4 ++++ serde_dhall/src/simple.rs | 2 ++ serde_dhall/src/static_type.rs | 1 + serde_dhall/src/value.rs | 3 +++ 8 files changed, 25 insertions(+), 4 deletions(-) (limited to 'serde_dhall/src/error.rs') diff --git a/serde_dhall/src/deserialize.rs b/serde_dhall/src/deserialize.rs index 75a08a9..cccadad 100644 --- a/serde_dhall/src/deserialize.rs +++ b/serde_dhall/src/deserialize.rs @@ -16,9 +16,10 @@ pub trait Sealed {} /// can deserialize. /// /// This trait cannot be implemented manually. +/// +/// TODO pub trait Deserialize: Sealed + Sized { #[doc(hidden)] - /// See [serde_dhall::from_str][crate::from_str] fn from_dhall(v: &Value) -> Result; } diff --git a/serde_dhall/src/error.rs b/serde_dhall/src/error.rs index 91a0b94..23d1b02 100644 --- a/serde_dhall/src/error.rs +++ b/serde_dhall/src/error.rs @@ -1,9 +1,11 @@ use dhall::error::Error as DhallError; +/// TODO pub type Result = std::result::Result; #[derive(Debug)] #[non_exhaustive] +/// TODO pub enum Error { Dhall(DhallError), Deserialize(String), diff --git a/serde_dhall/src/lib.rs b/serde_dhall/src/lib.rs index 5ee1cf6..e59255c 100644 --- a/serde_dhall/src/lib.rs +++ b/serde_dhall/src/lib.rs @@ -1,6 +1,5 @@ #![doc(html_root_url = "https://docs.rs/serde_dhall/0.4.0")] -// #![warn(missing_docs)] -// #![warn(missing_doc_code_examples)] +// TODO #![warn(missing_docs)] #![warn(missing_doc_code_examples)] //! [Dhall][dhall] is a programmable configuration language that provides a non-repetitive //! alternative to JSON and YAML. //! @@ -173,7 +172,7 @@ #[cfg(doctest)] doc_comment::doctest!("../../README.md"); -/// Finer-grained control over deserializing Dhall +/// Finer-grained control over deserializing Dhall values pub mod options; /// Serde-compatible Dhall values and their type pub mod simple; diff --git a/serde_dhall/src/options.rs b/serde_dhall/src/options.rs index 542804b..19b8587 100644 --- a/serde_dhall/src/options.rs +++ b/serde_dhall/src/options.rs @@ -48,6 +48,7 @@ enum Source<'a> { /// # Ok(()) /// # } /// ``` +/// /// TODO #[derive(Debug, Clone)] pub struct Options<'a, T> { source: Source<'a>, @@ -69,9 +70,11 @@ impl<'a, T> Options<'a, T> { target_type: std::marker::PhantomData, } } + /// TODO fn from_str(s: &'a str) -> Self { Self::default_with_source(Source::Str(s)) } + /// TODO fn from_file>(path: P) -> Self { Self::default_with_source(Source::File(path.as_ref().to_owned())) } @@ -79,6 +82,7 @@ impl<'a, T> Options<'a, T> { // Self::default_with_source(Source::Url(url)) // } + /// TODO pub fn imports(&mut self, imports: bool) -> &mut Self { self.allow_imports = imports; self @@ -90,10 +94,12 @@ impl<'a, T> Options<'a, T> { // } // self // } + // /// TODO pub fn type_annotation(&mut self, ty: &SimpleType) -> &mut Self { self.annot = Some(ty.clone()); self } + /// TODO pub fn static_type_annotation(&mut self) -> &mut Self where T: StaticType, @@ -118,6 +124,7 @@ impl<'a, T> Options<'a, T> { }; Ok(Value::from_nir(typed.normalize().as_nir())) } + /// TODO pub fn parse(&self) -> Result where T: Deserialize, @@ -127,9 +134,11 @@ impl<'a, T> Options<'a, T> { } } +/// TODO pub fn from_str<'a, T>(s: &'a str) -> Options<'a, T> { Options::from_str(s) } +/// TODO pub fn from_file<'a, T, P: AsRef>(path: P) -> Options<'a, T> { Options::from_file(path) } diff --git a/serde_dhall/src/shortcuts.rs b/serde_dhall/src/shortcuts.rs index 1fb1032..ddb738c 100644 --- a/serde_dhall/src/shortcuts.rs +++ b/serde_dhall/src/shortcuts.rs @@ -79,6 +79,8 @@ where /// # Ok(()) /// # } /// ``` +/// +/// TODO pub fn from_str_manual_type(s: &str, ty: &SimpleType) -> Result where T: Deserialize, @@ -92,6 +94,8 @@ where /// Like [from_str], but this additionally checks that /// the type of the provided expression matches the output type `T`. The [StaticType] trait /// captures Rust types that are valid Dhall types. +/// +/// TODO pub fn from_str_static_type(s: &str) -> Result where T: Deserialize + StaticType, diff --git a/serde_dhall/src/simple.rs b/serde_dhall/src/simple.rs index 95642bd..3d77853 100644 --- a/serde_dhall/src/simple.rs +++ b/serde_dhall/src/simple.rs @@ -6,6 +6,7 @@ use dhall::syntax::{Builtin, ExprKind, NumKind, Span}; use crate::{Deserialize, Error, Result, Sealed}; /// A simple value of the kind that can be encoded/decoded with serde +/// TODO #[derive(Debug, Clone, PartialEq, Eq)] pub struct Value { kind: Box, @@ -24,6 +25,7 @@ pub enum ValKind { } /// The type of a `simple::Value`. +/// TODO #[derive(Debug, Clone, PartialEq, Eq)] pub struct Type { kind: Box, diff --git a/serde_dhall/src/static_type.rs b/serde_dhall/src/static_type.rs index 79418d2..3fdff39 100644 --- a/serde_dhall/src/static_type.rs +++ b/serde_dhall/src/static_type.rs @@ -11,6 +11,7 @@ use crate::simple::{TyKind, Type}; /// For this reason, something like `HashMap` cannot implement /// [StaticType] because each different value would /// have a different Dhall record type. +/// TODO pub trait StaticType { fn static_type() -> Type; } diff --git a/serde_dhall/src/value.rs b/serde_dhall/src/value.rs index a119028..2557efe 100644 --- a/serde_dhall/src/value.rs +++ b/serde_dhall/src/value.rs @@ -5,6 +5,7 @@ use crate::simple::{Type as SimpleType, Value as SimpleValue}; use crate::{Deserialize, Error, Sealed}; /// An arbitrary Dhall value. +/// TODO #[derive(Debug, Clone)] pub struct Value { /// Invariant: in normal form @@ -25,10 +26,12 @@ impl Value { } /// Converts a Value into a SimpleValue. + /// TODO pub fn to_simple_value(&self) -> Option { self.as_simple_val.clone() } /// Converts a Value into a SimpleType. + /// TODO pub fn to_simple_type(&self) -> Option { self.as_simple_ty.clone() } -- cgit v1.3.1 From f9848b54fe2e64901042fba66fb471999f415ff1 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 24 Mar 2020 18:53:25 +0000 Subject: Hide serde Error internals --- serde_dhall/src/deserialize.rs | 6 +++--- serde_dhall/src/error.rs | 24 ++++++++++++++++-------- serde_dhall/src/lib.rs | 1 + serde_dhall/src/options.rs | 4 ++-- serde_dhall/src/value.rs | 10 +++++----- 5 files changed, 27 insertions(+), 18 deletions(-) (limited to 'serde_dhall/src/error.rs') diff --git a/serde_dhall/src/deserialize.rs b/serde_dhall/src/deserialize.rs index ab5580e..7bb0051 100644 --- a/serde_dhall/src/deserialize.rs +++ b/serde_dhall/src/deserialize.rs @@ -6,7 +6,7 @@ use std::borrow::Cow; use dhall::syntax::NumKind; use crate::value::SimpleValue; -use crate::{Error, Result, Value}; +use crate::{Error, ErrorKind, Result, Value}; pub trait Sealed {} @@ -33,10 +33,10 @@ where { fn from_dhall(v: &Value) -> Result { let sval = v.to_simple_value().ok_or_else(|| { - Error::Deserialize(format!( + Error(ErrorKind::Deserialize(format!( "this cannot be deserialized into the serde data model: {}", v - )) + ))) })?; T::deserialize(Deserializer(Cow::Owned(sval))) } diff --git a/serde_dhall/src/error.rs b/serde_dhall/src/error.rs index 23d1b02..896e8b9 100644 --- a/serde_dhall/src/error.rs +++ b/serde_dhall/src/error.rs @@ -1,21 +1,29 @@ use dhall::error::Error as DhallError; -/// TODO +/// Alias for a `Result` with the error type `serde_dhall::Error`. pub type Result = std::result::Result; +/// Errors that can occur when deserializing Dhall data. #[derive(Debug)] -#[non_exhaustive] -/// TODO -pub enum Error { +pub struct Error(pub(crate) ErrorKind); + +#[derive(Debug)] +pub(crate) enum ErrorKind { Dhall(DhallError), Deserialize(String), } +impl From for Error { + fn from(kind: ErrorKind) -> Error { + Error(kind) + } +} + 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), + match &self.0 { + ErrorKind::Dhall(err) => write!(f, "{}", err), + ErrorKind::Deserialize(err) => write!(f, "{}", err), } } } @@ -27,6 +35,6 @@ impl serde::de::Error for Error { where T: std::fmt::Display, { - Error::Deserialize(msg.to_string()) + ErrorKind::Deserialize(msg.to_string()).into() } } diff --git a/serde_dhall/src/lib.rs b/serde_dhall/src/lib.rs index 4cb25e5..fe1f4b2 100644 --- a/serde_dhall/src/lib.rs +++ b/serde_dhall/src/lib.rs @@ -188,6 +188,7 @@ pub use dhall_proc_macros::StaticType; pub use deserialize::Deserialize; pub(crate) use deserialize::Sealed; +pub(crate) use error::ErrorKind; pub use error::{Error, Result}; pub use shortcuts::{ from_file, from_file_manual_type, from_file_static_type, from_str, diff --git a/serde_dhall/src/options.rs b/serde_dhall/src/options.rs index f468cf0..f260572 100644 --- a/serde_dhall/src/options.rs +++ b/serde_dhall/src/options.rs @@ -3,7 +3,7 @@ use std::path::{Path, PathBuf}; use dhall::Parsed; use crate::SimpleType; -use crate::{Deserialize, Error, Result, StaticType, Value}; +use crate::{Deserialize, Error, ErrorKind, Result, StaticType, Value}; #[derive(Debug, Clone)] enum Source<'a> { @@ -130,7 +130,7 @@ impl<'a, T> Options<'a, T> { where T: Deserialize, { - let val = self._parse().map_err(Error::Dhall)?; + let val = self._parse().map_err(ErrorKind::Dhall).map_err(Error)?; T::from_dhall(&val) } } diff --git a/serde_dhall/src/value.rs b/serde_dhall/src/value.rs index bdc914f..3543f4d 100644 --- a/serde_dhall/src/value.rs +++ b/serde_dhall/src/value.rs @@ -3,7 +3,7 @@ use std::collections::{BTreeMap, HashMap}; use dhall::semantics::{Hir, HirKind, Nir, NirKind}; use dhall::syntax::{Builtin, Expr, ExprKind, NumKind, Span}; -use crate::{Deserialize, Error, Result, Sealed}; +use crate::{Deserialize, Error, ErrorKind, Result, Sealed}; #[doc(hidden)] /// An arbitrary Dhall value. @@ -254,20 +254,20 @@ impl Deserialize for Value { impl Deserialize for SimpleValue { fn from_dhall(v: &Value) -> Result { v.to_simple_value().ok_or_else(|| { - Error::Deserialize(format!( + Error(ErrorKind::Deserialize(format!( "this cannot be deserialized into a simple type: {}", v - )) + ))) }) } } impl Deserialize for SimpleType { fn from_dhall(v: &Value) -> Result { v.to_simple_type().ok_or_else(|| { - Error::Deserialize(format!( + Error(ErrorKind::Deserialize(format!( "this cannot be deserialized into a simple type: {}", v - )) + ))) }) } } -- cgit v1.3.1