use super::ws_protocol::{client, schema};
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct ClientChannelId(u32);
impl ClientChannelId {
pub fn new(id: u32) -> Self {
Self(id)
}
}
impl From<ClientChannelId> for u32 {
fn from(id: ClientChannelId) -> u32 {
id.0
}
}
impl From<ClientChannelId> for u64 {
fn from(id: ClientChannelId) -> u64 {
id.0.into()
}
}
impl std::fmt::Display for ClientChannelId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ClientChannel {
pub id: ClientChannelId,
pub topic: String,
pub encoding: String,
pub schema_name: String,
pub schema_encoding: Option<String>,
pub schema: Option<Vec<u8>>,
}
impl TryFrom<client::advertise::Channel<'_>> for ClientChannel {
type Error = schema::DecodeError;
fn try_from(ch: client::advertise::Channel) -> Result<Self, Self::Error> {
let schema = match ch.decode_schema() {
Ok(schema) => Some(schema),
Err(schema::DecodeError::MissingSchema) => None,
Err(e) => return Err(e),
};
Ok(Self {
id: ClientChannelId::new(ch.id),
topic: ch.topic.to_string(),
encoding: ch.encoding.to_string(),
schema_name: ch.schema_name.to_string(),
schema_encoding: ch.schema_encoding.map(|s| s.to_string()),
schema,
})
}
}