Skip to main content

pilota_build2/codegen/thrift/
decode_helper.rs

1use faststr::FastStr;
2use paste::paste;
3
4pub struct DecodeHelper {
5    pub is_async: bool,
6}
7
8impl DecodeHelper {
9    pub fn new(is_async: bool) -> Self {
10        Self { is_async }
11    }
12}
13
14macro_rules! protocol_method {
15    ($m:ident) => {
16        paste! {
17            #[inline]
18            pub fn [<codegen_ $m>](&self) -> faststr::FastStr {
19                if self.is_async {
20                    format!("protocol.{}().await?", stringify!($m)).into()
21                } else {
22                    format!("protocol.{}()?", stringify!($m)).into()
23                }
24            }
25        }
26    };
27}
28
29impl DecodeHelper {
30    protocol_method!(read_i8);
31    protocol_method!(read_i16);
32    protocol_method!(read_i32);
33    protocol_method!(read_i64);
34    protocol_method!(read_double);
35    protocol_method!(read_bytes);
36    protocol_method!(read_bytes_vec);
37    protocol_method!(read_byte);
38    protocol_method!(read_string);
39    protocol_method!(read_faststr);
40    protocol_method!(read_list_begin);
41    protocol_method!(read_list_end);
42    protocol_method!(read_set_begin);
43    protocol_method!(read_set_end);
44    protocol_method!(read_map_begin);
45    protocol_method!(read_map_end);
46    protocol_method!(read_struct_begin);
47    protocol_method!(read_struct_end);
48    protocol_method!(read_field_begin);
49    protocol_method!(read_field_end);
50    protocol_method!(read_bool);
51
52    pub fn codegen_skip_ttype(&self, tt: FastStr) -> String {
53        if self.is_async {
54            format!("protocol.skip({tt}).await?")
55        } else {
56            format!("protocol.skip({tt})?")
57        }
58    }
59
60    pub fn codegen_item_decode(&self) -> FastStr {
61        if self.is_async {
62            "::pilota::thrift::Message::decode_async(protocol).await?".into()
63        } else {
64            "::pilota::thrift::Message::decode(protocol)?".into()
65        }
66    }
67}