palladium_runtime/
remote_reply.rs1use std::fmt;
2use std::marker::PhantomData;
3
4use palladium_actor::fnv1a_64;
5use serde::de::DeserializeOwned;
6use serde::{Deserialize, Deserializer, Serialize, Serializer};
7
8pub(crate) const REMOTE_REPLY_ERROR_TYPE_TAG: u64 =
9 fnv1a_64("palladium_runtime.remote.RemoteReplyError");
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
12pub(crate) enum RemoteReplyErrorKind {
13 SerializationFailed,
14}
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
17pub(crate) struct RemoteReplyErrorEnvelope {
18 pub kind: RemoteReplyErrorKind,
19}
20
21pub struct JsonReply<T> {
22 json: String,
23 _marker: PhantomData<fn() -> T>,
24}
25
26impl<T> JsonReply<T> {
27 pub fn from_json(json: impl Into<String>) -> Self {
28 Self {
29 json: json.into(),
30 _marker: PhantomData,
31 }
32 }
33
34 pub fn as_json(&self) -> &str {
35 &self.json
36 }
37
38 pub fn into_json(self) -> String {
39 self.json
40 }
41}
42
43impl<T> JsonReply<T>
44where
45 T: Serialize,
46{
47 pub fn from_value(value: &T) -> serde_json::Result<Self> {
48 serde_json::to_string(value).map(Self::from_json)
49 }
50}
51
52impl<T> JsonReply<T>
53where
54 T: DeserializeOwned,
55{
56 pub fn decode(&self) -> serde_json::Result<T> {
57 serde_json::from_str(&self.json)
58 }
59}
60
61impl<T> Clone for JsonReply<T> {
62 fn clone(&self) -> Self {
63 Self::from_json(self.json.clone())
64 }
65}
66
67impl<T> PartialEq for JsonReply<T> {
68 fn eq(&self, other: &Self) -> bool {
69 self.json == other.json
70 }
71}
72
73impl<T> Eq for JsonReply<T> {}
74
75impl<T> fmt::Debug for JsonReply<T> {
76 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77 f.debug_struct("JsonReply")
78 .field("json", &self.json)
79 .finish()
80 }
81}
82
83impl<T> Serialize for JsonReply<T> {
84 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
85 where
86 S: Serializer,
87 {
88 self.json.serialize(serializer)
89 }
90}
91
92impl<'de, T> Deserialize<'de> for JsonReply<T> {
93 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
94 where
95 D: Deserializer<'de>,
96 {
97 String::deserialize(deserializer).map(Self::from_json)
98 }
99}
100
101#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
102pub struct BytesReply(Vec<u8>);
103
104impl BytesReply {
105 pub fn from_bytes(bytes: impl Into<Vec<u8>>) -> Self {
106 Self(bytes.into())
107 }
108
109 pub fn as_bytes(&self) -> &[u8] {
110 &self.0
111 }
112
113 pub fn into_bytes(self) -> Vec<u8> {
114 self.0
115 }
116}