use super::ValidationError;
use std::fmt;
fn format_with_underscores(value: u32) -> String {
let s = value.to_string();
let bytes = s.as_bytes();
let len = bytes.len();
let mut result = String::new();
for (i, &byte) in bytes.iter().enumerate() {
if i > 0 && (len - i) % 3 == 0 {
result.push('_');
}
result.push(byte as char);
}
result
}
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct StreamID(u32);
impl StreamID {
pub fn new(value: u32) -> Result<Self, ValidationError> {
Ok(Self(value))
}
#[must_use]
pub const fn as_u32(self) -> u32 {
self.0
}
}
impl fmt::Display for StreamID {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "StreamID({})", format_with_underscores(self.0))
}
}
impl TryFrom<u32> for StreamID {
type Error = ValidationError;
fn try_from(value: u32) -> Result<Self, Self::Error> {
Self::new(value)
}
}
impl From<StreamID> for u32 {
fn from(stream_id: StreamID) -> Self {
stream_id.0
}
}