From 854531112d86b5ac60bb37ed71f0d63e9ecc84da Mon Sep 17 00:00:00 2001 From: Greg Heartsfield Date: Tue, 23 Nov 2021 11:32:19 -0600 Subject: [PATCH] Wrap serde json errors --- src/error.rs | 8 ++++++++ src/event.rs | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/src/error.rs b/src/error.rs index c50a3b5..28fcec5 100644 --- a/src/error.rs +++ b/src/error.rs @@ -15,4 +15,12 @@ pub enum Error { ReqParseFailed, #[error("parsing JSON->Close failed")] CloseParseFailed, + #[error("JSON parsing failed")] + JsonParseFailed(serde_json::Error), +} + +impl From for Error { + fn from(r: serde_json::Error) -> Self { + Error::JsonParseFailed(r) + } } diff --git a/src/event.rs b/src/event.rs index c8bf399..5e21fbd 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,3 +1,4 @@ +use crate::error::{Error, Result}; use serde::{Deserialize, Deserializer, Serialize}; //use serde_json::json; //use serde_json::Result; @@ -48,6 +49,13 @@ where // Roundtrip from JSON-string to Event, and back to string. // Perform validation on an Event to ensure the id and signature are correct. +impl Event { + pub fn parse(json: &str) -> Result { + let e: Event = serde_json::from_str(json)?; + Err(Error::EventParseFailed) + } +} + #[cfg(test)] mod tests { use crate::event::Event;