diff --git a/lib/src/dbs/notification.rs b/lib/src/dbs/notification.rs index 916e9343..dd6d8b92 100644 --- a/lib/src/dbs/notification.rs +++ b/lib/src/dbs/notification.rs @@ -1,17 +1,34 @@ use crate::sql::{Object, Value}; -use serde::{Deserialize, Serialize}; -use std::fmt; -use std::fmt::Debug; +use serde::{ser::SerializeStruct, Deserialize, Serialize}; +use std::fmt::{self, Debug, Display}; use uuid::Uuid; #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "UPPERCASE")] +pub enum Action { + Create, + Update, + Delete, +} + +impl Display for Action { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + Action::Create => write!(f, "CREATE"), + Action::Update => write!(f, "UPDATE"), + Action::Delete => write!(f, "DELETE"), + } + } +} + +#[derive(Clone, Debug, PartialEq, Deserialize)] pub struct Notification { pub id: Uuid, pub action: Action, pub result: Value, } -impl fmt::Display for Notification { +impl Display for Notification { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let obj: Object = map! { "id".to_string() => self.id.to_string().into(), @@ -23,20 +40,15 @@ impl fmt::Display for Notification { } } -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] -#[serde(rename_all = "UPPERCASE")] -pub enum Action { - Create, - Update, - Delete, -} - -impl fmt::Display for Action { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - Action::Create => write!(f, "CREATE"), - Action::Update => write!(f, "UPDATE"), - Action::Delete => write!(f, "DELETE"), - } +impl Serialize for Notification { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + let mut val = serializer.serialize_struct("Notification", 3)?; + val.serialize_field("id", &self.id.to_string())?; + val.serialize_field("action", &self.action)?; + val.serialize_field("result", &self.result)?; + val.end() } }