sustenet_shared/
macros.rs

1/// This calls loop and select! in a single macro.
2#[macro_export]
3macro_rules! lselect {
4    ($($select_body:tt)*) => {
5        loop {
6            tokio::select! {
7                $($select_body)*
8            }
9        }
10    };
11}
12
13/// This reads a u8 for the length and then calls read_exact
14/// to read the bytes then converts it to a string.
15/// 
16/// This is used in a loop to continue if an error occurs.
17#[macro_export]
18macro_rules! lread_string {
19    ($reader:expr, $error:expr, $name:expr) => {
20        {
21        let len = match $reader.read_u8().await {
22            Ok(len) => len,
23            Err(e) => {
24                $error(format!("Failed to read the {} len. {:?}", $name, e).as_str());
25                continue;
26            }
27        } as usize;
28
29        let mut val = vec![0u8; len];
30        match $reader.read_exact(&mut val).await {
31            Ok(_) => (),
32            Err(e) => {
33                $error(format!("Failed to read the {}. {:?}", $name, e).as_str());
34                continue;
35            }
36        };
37
38        match String::from_utf8(val) {
39            Ok(val) => val,
40            Err(e) => {
41                $error(format!("Failed to convert the {} to a String. {:?}", $name, e).as_str());
42                continue;
43            }
44        }
45        }
46    };
47}