asteroid_mq_model/
topic.rs

1use std::{borrow::Borrow, collections::HashMap};
2
3use bytes::Bytes;
4use serde::{Deserialize, Serialize};
5use typeshare::typeshare;
6
7use crate::{endpoint::EndpointAddr, message::MessageStatusKind};
8
9#[derive(Debug, Clone, PartialEq, Eq, Hash)]
10#[typeshare(serialized_as = "String")]
11/// code are expect to be a valid utf8 string
12pub struct TopicCode(pub(crate) Bytes);
13impl TopicCode {
14    pub fn new<B: Into<String>>(code: B) -> Self {
15        Self(Bytes::from(code.into()))
16    }
17    pub const fn const_new(code: &'static str) -> Self {
18        Self(Bytes::from_static(code.as_bytes()))
19    }
20}
21
22impl From<&'_ str> for TopicCode {
23    fn from(val: &'_ str) -> Self {
24        TopicCode::new(val)
25    }
26}
27
28impl From<String> for TopicCode {
29    fn from(val: String) -> Self {
30        TopicCode::new(val)
31    }
32}
33
34impl From<&'_ [u8]> for TopicCode {
35    fn from(val: &'_ [u8]) -> Self {
36        TopicCode(Bytes::copy_from_slice(val))
37    }
38}
39
40impl From<Vec<u8>> for TopicCode {
41    fn from(val: Vec<u8>) -> Self {
42        TopicCode(Bytes::from(val))
43    }
44}
45
46impl Serialize for TopicCode {
47    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
48        let string = unsafe { std::str::from_utf8_unchecked(self.0.as_ref()) };
49        serializer.serialize_str(string)
50    }
51}
52
53impl<'de> Deserialize<'de> for TopicCode {
54    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
55        let string = String::deserialize(deserializer)?;
56        Ok(Self(Bytes::from(string)))
57    }
58}
59
60impl Borrow<[u8]> for TopicCode {
61    fn borrow(&self) -> &[u8] {
62        &self.0
63    }
64}
65
66impl std::fmt::Display for TopicCode {
67    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68        unsafe { f.write_str(std::str::from_utf8_unchecked(&self.0)) }
69    }
70}
71
72#[derive(Debug, Clone, Serialize, Deserialize)]
73#[cfg_attr(feature = "bincode", derive(bincode::Decode, bincode::Encode))]
74#[typeshare]
75pub struct WaitAckError {
76    pub status: HashMap<EndpointAddr, MessageStatusKind>,
77    pub exception: Option<WaitAckErrorException>,
78}
79
80#[derive(Debug, Clone, Serialize, Deserialize)]
81#[cfg_attr(feature = "bincode", derive(bincode::Decode, bincode::Encode))]
82#[typeshare]
83pub struct WaitAckSuccess {
84    pub status: HashMap<EndpointAddr, MessageStatusKind>,
85}
86
87impl WaitAckError {
88    pub fn exception(exception: WaitAckErrorException) -> Self {
89        Self {
90            status: HashMap::new(),
91            exception: Some(exception),
92        }
93    }
94}
95
96#[repr(u8)]
97#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
98#[cfg_attr(feature = "bincode", derive(bincode::Decode, bincode::Encode))]
99#[typeshare]
100pub enum WaitAckErrorException {
101    MessageDropped = 0,
102    Overflow = 1,
103    NoAvailableTarget = 2,
104    DurableMessageWithoutConfig = 3,
105    DurableMessageExpired = 4,
106    PayloadToLarge = 5,
107}
108
109pub enum AckWaitErrorKind {
110    Timeout,
111    Fail,
112}
113pub type WaitAckResult = Result<WaitAckSuccess, WaitAckError>;