1use serde::{Deserialize, Serialize, de::DeserializeOwned};
2#[cfg(feature = "wasm")]
3use wasm_bindgen::prelude::*;
4
5use crate::{
6 endpoint::{Endpoint, Source},
7 serde_utils,
8};
9
10#[derive(Clone, Debug, Deserialize, Serialize)]
11#[cfg_attr(test, derive(PartialEq))]
12#[cfg_attr(feature = "wasm", wasm_bindgen)]
13pub struct OutgoingMessage {
15 #[cfg_attr(feature = "wasm", wasm_bindgen(getter_with_clone))]
17 pub payload: Vec<u8>,
18 #[cfg_attr(feature = "wasm", wasm_bindgen(getter_with_clone))]
20 pub destination: Endpoint,
21 #[cfg_attr(feature = "wasm", wasm_bindgen(getter_with_clone))]
23 pub topic: Option<String>,
24}
25
26#[derive(Clone, Debug, Deserialize, Serialize)]
27#[cfg_attr(test, derive(PartialEq))]
28#[cfg_attr(feature = "wasm", wasm_bindgen)]
29pub struct IncomingMessage {
31 #[cfg_attr(feature = "wasm", wasm_bindgen(getter_with_clone))]
33 pub payload: Vec<u8>,
34 #[cfg_attr(feature = "wasm", wasm_bindgen(getter_with_clone))]
36 pub destination: Endpoint,
37 #[cfg_attr(feature = "wasm", wasm_bindgen(getter_with_clone))]
39 pub source: Source,
40 #[cfg_attr(feature = "wasm", wasm_bindgen(getter_with_clone))]
42 pub topic: Option<String>,
43}
44
45#[derive(Clone, Debug, Serialize, Deserialize)]
47#[cfg_attr(test, derive(PartialEq))]
48pub struct TypedOutgoingMessage<Payload> {
49 pub payload: Payload,
51 pub destination: Endpoint,
53}
54
55impl<Payload> TryFrom<OutgoingMessage> for TypedOutgoingMessage<Payload>
56where
57 Payload: DeserializeOwned,
58{
59 type Error = serde_utils::SerializeError;
60
61 fn try_from(value: OutgoingMessage) -> Result<Self, Self::Error> {
62 Ok(Self {
63 payload: serde_utils::from_slice(&value.payload)?,
64 destination: value.destination,
65 })
66 }
67}
68
69impl<Payload> TryFrom<TypedOutgoingMessage<Payload>> for OutgoingMessage
70where
71 Payload: Serialize + PayloadTypeName,
72{
73 type Error = serde_utils::DeserializeError;
74
75 fn try_from(value: TypedOutgoingMessage<Payload>) -> Result<Self, Self::Error> {
76 Ok(Self {
77 payload: serde_utils::to_vec(&value.payload)?,
78 destination: value.destination,
79 topic: Some(Payload::PAYLOAD_TYPE_NAME.to_owned()),
80 })
81 }
82}
83
84#[derive(Clone, Debug)]
86#[cfg_attr(test, derive(PartialEq))]
87pub struct TypedIncomingMessage<Payload: PayloadTypeName> {
88 pub payload: Payload,
90 pub destination: Endpoint,
92 pub source: Source,
94}
95
96pub trait PayloadTypeName {
98 const PAYLOAD_TYPE_NAME: &'static str;
100}
101
102impl<Payload> TryFrom<IncomingMessage> for TypedIncomingMessage<Payload>
103where
104 Payload: DeserializeOwned + PayloadTypeName,
105{
106 type Error = serde_utils::DeserializeError;
107
108 fn try_from(value: IncomingMessage) -> Result<Self, Self::Error> {
109 Ok(Self {
110 payload: serde_utils::from_slice(&value.payload)?,
111 destination: value.destination,
112 source: value.source,
113 })
114 }
115}
116
117impl<Payload> TryFrom<TypedIncomingMessage<Payload>> for IncomingMessage
118where
119 Payload: Serialize + PayloadTypeName,
120{
121 type Error = serde_utils::SerializeError;
122
123 fn try_from(value: TypedIncomingMessage<Payload>) -> Result<Self, Self::Error> {
124 Ok(Self {
125 payload: serde_utils::to_vec(&value.payload)?,
126 destination: value.destination,
127 source: value.source,
128 topic: Some(Payload::PAYLOAD_TYPE_NAME.to_owned()),
129 })
130 }
131}