Skip to main content

hanabi_core/protocol/response/
mod.rs

1mod control;
2mod key_value;
3mod pubsub;
4
5pub use control::ControlResponse;
6pub use key_value::KeyValueResponse;
7pub use pubsub::PubSubResponse;
8
9use crate::protocol::Status;
10use crate::protocol::error::ProtocolError;
11use bytes::Bytes;
12use tracing::info;
13
14/// Represents a response sent by the server.
15#[derive(Debug, Clone, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)]
16pub enum Response {
17    /// General response for successful operations that do not return a value.
18    Ok,
19
20    /// General response for failed operations.
21    Err(Status),
22
23    /// Specific response for control operations.
24    Control(ControlResponse),
25
26    /// Specific response for key-value store operations.
27    KeyValue(KeyValueResponse),
28
29    /// Specific response for Pub/Sub operations.
30    PubSub(PubSubResponse),
31}
32
33impl TryFrom<Response> for Vec<u8> {
34    type Error = ProtocolError;
35
36    fn try_from(value: Response) -> Result<Self, Self::Error> {
37        rmp_serde::to_vec(&value)
38            .inspect_err(|error| info!(?error, "Failed to serialize response"))
39            .map_err(|error| ProtocolError::Serialization(error.to_string()))
40    }
41}
42
43impl TryFrom<Response> for Bytes {
44    type Error = ProtocolError;
45
46    fn try_from(value: Response) -> Result<Self, Self::Error> {
47        rmp_serde::to_vec(&value)
48            .inspect_err(|error| info!(?error, "Failed to serialize response"))
49            .map_err(|error| ProtocolError::Serialization(error.to_string()))
50            .map(Bytes::from)
51    }
52}
53
54impl TryFrom<Vec<u8>> for Response {
55    type Error = ProtocolError;
56
57    fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
58        rmp_serde::from_slice(&value)
59            .inspect_err(|error| info!(?error, "Failed to deserialize response"))
60            .map_err(|error| ProtocolError::Serialization(error.to_string()))
61    }
62}
63
64impl TryFrom<&[u8]> for Response {
65    type Error = ProtocolError;
66
67    fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
68        rmp_serde::from_slice(value)
69            .inspect_err(|error| info!(?error, "Failed to deserialize response"))
70            .map_err(|error| ProtocolError::Serialization(error.to_string()))
71    }
72}
73
74impl TryFrom<Bytes> for Response {
75    type Error = ProtocolError;
76
77    fn try_from(value: Bytes) -> Result<Self, Self::Error> {
78        rmp_serde::from_slice(&value)
79            .inspect_err(|error| info!(?error, "Failed to deserialize response"))
80            .map_err(|error| ProtocolError::Serialization(error.to_string()))
81    }
82}