]> git.lizzy.rs Git - mt_net.git/blob - src/to_clt/inv.rs
Implement NodeMeta and add Inventory stub
[mt_net.git] / src / to_clt / inv.rs
1 use super::*;
2 use mt_ser::{DeserializeError, SerializeError};
3 use std::io::{Read, Write};
4
5 #[mt_derive(to = "clt", custom)]
6 pub struct Inventory; // TODO
7
8 #[cfg(feature = "server")]
9 impl MtSerialize for Inventory {
10     fn mt_serialize<C: MtCfg>(&self, writer: &mut impl Write) -> Result<(), SerializeError> {
11         "EndInventory\n".mt_serialize::<()>(writer)
12     }
13 }
14
15 fn read_line(reader: &mut impl Read) -> Result<String, DeserializeError> {
16     let utf8 = mt_ser::mt_deserialize_seq::<(), u8>(reader)?
17         .map_while(|x| match x {
18             Ok(0x0A) => None,
19             x => Some(x),
20         })
21         .try_collect::<Vec<_>>()?;
22
23     String::from_utf8(utf8)
24         .map_err(|e| DeserializeError::Other(format!("Invalid UTF-8: {e}").into()))
25 }
26
27 #[cfg(feature = "client")]
28 impl MtDeserialize for Inventory {
29     fn mt_deserialize<C: MtCfg>(reader: &mut impl Read) -> Result<Self, DeserializeError> {
30         loop {
31             match read_line(reader)?.as_str() {
32                 "EndInventory" => return Ok(Self),
33                 _ => {}
34             }
35         }
36     }
37 }