use serde::Serialize; use crate::{ object::{AnyObject, GiteratedObject}, operation::{AnyOperation, GiteratedOperation}, }; use std::fmt::Debug; #[derive(Serialize)] #[serde(bound(deserialize = "O: GiteratedObject, V: GiteratedOperation"))] pub struct GiteratedMessage> { #[serde(with = "string")] pub object: O, pub operation: String, pub payload: V, } #[allow(unused)] mod string { use std::fmt::Display; use std::str::FromStr; use serde::{de, Deserialize, Deserializer, Serializer}; pub fn serialize(value: &T, serializer: S) -> Result where T: Display, S: Serializer, { serializer.collect_str(value) } pub fn deserialize<'de, T, D>(deserializer: D) -> Result where T: FromStr, T::Err: Display, D: Deserializer<'de>, { String::deserialize(deserializer)? .parse() .map_err(de::Error::custom) } } impl GiteratedMessage { pub fn try_into>( &self, ) -> Result, ()> { let object = O::from_object_str(&self.object.0).map_err(|_| ())?; let payload = serde_json::from_slice::(&self.payload.0).map_err(|_| ())?; Ok(GiteratedMessage { object, operation: self.operation.clone(), payload, }) } } impl + Debug, O: GiteratedObject + Debug> Debug for GiteratedMessage { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("GiteratedMessage") .field("object", &self.object) .field("operation", &self.operation) .field("payload", &self.payload) .finish() } }