1use std::string::FromUtf8Error;
2
3use tubes::prelude::*;
4
5pub struct Msg(pub String);
6
7impl From<String> for Msg {
8 fn from(value: String) -> Self {
9 Self(value)
10 }
11}
12
13impl TryFrom<&[u8]> for Msg {
14 type Error = FromUtf8Error;
15
16 fn try_from(value: &[u8]) -> std::result::Result<Self, Self::Error> {
17 Ok(Msg(String::from_utf8(value.to_vec())?))
18 }
19}
20
21impl TryFrom<Msg> for Vec<u8> {
22 type Error = ();
23
24 fn try_from(value: Msg) -> std::result::Result<Self, Self::Error> {
25 Ok(value.0.into())
26 }
27}
28
29fn main() {
30 let mut s = Session::new_client("127.0.0.1:5000".into());
31 s.start().unwrap();
32
33 loop {
34 if let Ok(Some(m)) = s.read()
35 && let MessageData::Broadcast { data, .. } = m
36 {
37 let data: Msg = data.as_slice().try_into().unwrap();
38 println!("Message: {}", data.0)
39 }
40 }
41}