Skip to main content

watermelon_proto/
subscription_id.rs

1use core::fmt::{self, Display};
2
3use crate::util::{self, ParseUintError};
4
5#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
6pub struct SubscriptionId(u64);
7
8impl SubscriptionId {
9    pub const MIN: Self = SubscriptionId(1);
10    pub const MAX: Self = SubscriptionId(u64::MAX);
11
12    /// Converts a slice of ASCII bytes to a `SubscriptionId`.
13    ///
14    /// # Errors
15    ///
16    /// It returns an error if the bytes do not contain a valid numeric value.
17    pub fn from_ascii_bytes(buf: &[u8]) -> Result<Self, ParseUintError> {
18        util::parse_u64(buf).map(Self)
19    }
20}
21
22impl From<u64> for SubscriptionId {
23    fn from(value: u64) -> Self {
24        Self(value)
25    }
26}
27
28impl From<SubscriptionId> for u64 {
29    fn from(value: SubscriptionId) -> Self {
30        value.0
31    }
32}
33
34impl Display for SubscriptionId {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        Display::fmt(&self.0, f)
37    }
38}