summaryrefslogtreecommitdiff
path: root/serde_dhall/src/error.rs
diff options
context:
space:
mode:
authorNadrieril2020-04-05 17:57:07 +0100
committerGitHub2020-04-05 17:57:07 +0100
commit7e977f282fb6a0eff0ef45738b9b5c98dc4c6fee (patch)
treead4249609707fd8720a44469152105c2f6a67c79 /serde_dhall/src/error.rs
parent5a5aa49e64197899006751db72e404f4b2292d4e (diff)
parent820214615547101f8f2b5de209b5189968bddfee (diff)
Merge pull request #154 from Nadrieril/cleanup-api
Rewrite serde_dhall API
Diffstat (limited to '')
-rw-r--r--serde_dhall/src/error.rs40
1 files changed, 40 insertions, 0 deletions
diff --git a/serde_dhall/src/error.rs b/serde_dhall/src/error.rs
new file mode 100644
index 0000000..896e8b9
--- /dev/null
+++ b/serde_dhall/src/error.rs
@@ -0,0 +1,40 @@
+use dhall::error::Error as DhallError;
+
+/// Alias for a `Result` with the error type `serde_dhall::Error`.
+pub type Result<T> = std::result::Result<T, Error>;
+
+/// Errors that can occur when deserializing Dhall data.
+#[derive(Debug)]
+pub struct Error(pub(crate) ErrorKind);
+
+#[derive(Debug)]
+pub(crate) enum ErrorKind {
+ Dhall(DhallError),
+ Deserialize(String),
+}
+
+impl From<ErrorKind> 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.0 {
+ ErrorKind::Dhall(err) => write!(f, "{}", err),
+ ErrorKind::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,
+ {
+ ErrorKind::Deserialize(msg.to_string()).into()
+ }
+}