pilota_build/codegen/thrift/
decode_helper.rs1use 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
29macro_rules! protocol_len {
30 ($m:ident) => {
31 paste! {
32 #[inline]
33 pub fn [<codegen_ $m>](&self, keep: bool) -> faststr::FastStr {
34 if self.is_async {
35 Default::default()
36 } else {
37 if keep {
38 format!("__pilota_offset += __protocol.{}();", stringify!($m)).into()
39 } else {
40 format!("__protocol.{}();", stringify!($m)).into()
41 }
42 }
43 }
44 }
45 };
46}
47
48impl DecodeHelper {
49 protocol_method!(read_i8);
50 protocol_method!(read_i16);
51 protocol_method!(read_i32);
52 protocol_method!(read_i64);
53 protocol_method!(read_double);
54 protocol_method!(read_bytes);
55 protocol_method!(read_bytes_vec);
56 protocol_method!(read_byte);
57 protocol_method!(read_string);
58 protocol_method!(read_faststr);
59 protocol_method!(read_list_begin);
60 protocol_method!(read_list_end);
61 protocol_method!(read_set_begin);
62 protocol_method!(read_set_end);
63 protocol_method!(read_map_begin);
64 protocol_method!(read_map_end);
65 protocol_method!(read_struct_begin);
66 protocol_method!(read_struct_end);
67 protocol_method!(read_field_begin);
68 protocol_method!(read_field_end);
69 protocol_method!(read_bool);
70 protocol_method!(read_uuid);
71
72 protocol_len!(field_end_len);
73 protocol_len!(field_stop_len);
74
75 pub fn codegen_skip_ttype(&self, tt: FastStr) -> String {
76 if self.is_async {
77 format!("__protocol.skip({tt}).await?")
78 } else {
79 format!("__protocol.skip({tt})?")
80 }
81 }
82
83 pub fn codegen_item_decode(&self, name: FastStr) -> FastStr {
84 if self.is_async {
85 format!("<{name} as ::pilota::thrift::Message>::decode_async(__protocol).await?",)
86 .into()
87 } else {
88 "::pilota::thrift::Message::decode(__protocol)?".into()
89 }
90 }
91
92 pub fn codegen_field_begin_len(&self, keep: bool) -> FastStr {
93 if self.is_async {
94 Default::default()
95 } else if keep {
96 "__pilota_offset += __protocol.field_begin_len(field_ident.field_type, field_ident.id);"
97 .into()
98 } else {
99 "__protocol.field_begin_len(field_ident.field_type, field_ident.id);".into()
100 }
101 }
102}