1use async_std::prelude::*;
2use async_std::{io, task};
3use simple_message_channels::{Message, Reader, Writer};
4
5fn main() {
6 task::block_on(async move {
7 match echo().await {
8 Err(e) => eprintln!("Error: {:?}", e),
9 Ok(()) => eprintln!("Ok."),
10 }
11 });
12}
13
14async fn echo() -> Result<(), io::Error> {
15 let stdin = io::stdin().lock().await;
16 let stdout = io::stdout().lock().await;
17 let mut reader = Reader::new(stdin);
18 let mut writer = Writer::new(stdout);
19 while let Some(msg) = reader.next().await {
20 let msg = msg?;
21 let resp = Message {
22 channel: msg.channel,
23 typ: msg.typ + 1,
24 message: to_upper(&msg.message),
25 };
26 writer.send(resp).await?;
27 }
28 Ok(())
29}
30
31fn to_upper(bytes: &[u8]) -> Vec<u8> {
32 let text = String::from_utf8(bytes.to_vec()).unwrap();
33 text.to_uppercase().as_bytes().to_vec()
34}