phoenix_channel_runtime/
payload.rs1use serde_json::Value;
2use thiserror::Error;
3
4pub trait EventRoute {
6 const EVENT: &'static str;
8 type Output: serde::de::DeserializeOwned;
10}
11
12#[derive(Clone, Debug, PartialEq)]
14pub enum Payload {
15 Json(Value),
17 Binary(Vec<u8>),
19 Reply {
21 status: String,
23 response: Box<Payload>,
25 },
26}
27
28impl Payload {
29 pub fn as_json(&self) -> Option<&Value> {
31 match self {
32 Self::Json(value) => Some(value),
33 Self::Binary(_) | Self::Reply { .. } => None,
34 }
35 }
36
37 pub fn as_binary(&self) -> Option<&[u8]> {
39 match self {
40 Self::Binary(value) => Some(value),
41 Self::Json(_) | Self::Reply { .. } => None,
42 }
43 }
44
45 pub fn into_json(self) -> Result<Value, Self> {
49 match self {
50 Self::Json(value) => Ok(value),
51 other => Err(other),
52 }
53 }
54
55 pub fn into_binary(self) -> Result<Vec<u8>, Self> {
59 match self {
60 Self::Binary(value) => Ok(value),
61 other => Err(other),
62 }
63 }
64
65 pub fn deserialize<T: serde::de::DeserializeOwned>(&self) -> Result<T, PayloadError> {
67 let value = self.as_json().ok_or(PayloadError::ExpectedJson)?;
68 serde_json::from_value(value.clone()).map_err(PayloadError::Deserialize)
69 }
70}
71
72impl From<Value> for Payload {
73 fn from(value: Value) -> Self {
74 Self::Json(value)
75 }
76}
77
78impl From<Vec<u8>> for Payload {
79 fn from(value: Vec<u8>) -> Self {
80 Self::Binary(value)
81 }
82}
83
84impl PartialEq<Value> for Payload {
85 fn eq(&self, other: &Value) -> bool {
86 self.as_json() == Some(other)
87 }
88}
89
90#[derive(Debug, Error)]
92pub enum PayloadError {
93 #[error("expected a JSON payload, received binary data")]
95 ExpectedJson,
96 #[error("failed to deserialize a JSON payload: {0}")]
98 Deserialize(#[source] serde_json::Error),
99}