use std::string::FromUtf8Error;
use tubes::prelude::*;
pub struct Msg(pub String);
impl From<String> for Msg {
fn from(value: String) -> Self {
Self(value)
}
}
impl TryFrom<&[u8]> for Msg {
type Error = FromUtf8Error;
fn try_from(value: &[u8]) -> std::result::Result<Self, Self::Error> {
Ok(Msg(String::from_utf8(value.to_vec())?))
}
}
impl TryFrom<Msg> for Vec<u8> {
type Error = ();
fn try_from(value: Msg) -> std::result::Result<Self, Self::Error> {
Ok(value.0.into())
}
}
fn main() {
let mut s = Session::<0>::new_client("127.0.0.1:5000".into());
s.start().unwrap();
loop {
if let Ok(Some(m)) = s.read()
&& let MessageData::Broadcast { data, .. } = m
{
let data: Msg = data.as_slice().try_into().unwrap();
println!("Message: {}", data.0)
}
}
}