1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
extern crate proc_macro; mod api; mod ast; mod de; mod ser; mod util; use self::api::generate_request_traits; use self::api::parse_and_generate_api; use self::de::generate_decode_trait_impls; use self::de::generate_default_trait_impls; use self::ser::generate_encode_trait_impls; use proc_macro::TokenStream; use syn::parse_macro_input; /// Custom derive for decoding structure or enum from bytes using fluvio protocol format. /// This assumes all fields implement fluvio decode traits. /// /// # Examples /// /// ``` /// use std::io::Cursor; /// use fluvio_protocol::Decoder; /// use fluvio_protocol::derive::Decode; /// /// #[derive(Default, Decode)] /// pub struct SimpleRecord { /// val: u8 /// } /// /// let data = [ /// 0x04 /// ]; /// /// let record = SimpleRecord::decode_from(&mut Cursor::new(&data),0).expect("decode"); /// assert_eq!(record.val, 4); /// ``` /// /// /// Decode applies to either Struct of Enum. For enum, it implements `TryFrom` trait. /// Currently it only supports integer variants. /// /// So this works /// /// ``` /// # use fluvio_protocol::derive::Decode; /// # impl Default for ThreeChoice { fn default() -> Self { unimplemented!() } } /// #[derive(Decode)] /// pub enum ThreeChoice { /// First = 1, /// Second = 2, /// Third = 3 /// } /// ``` /// /// Also, enum without integer literal works as well /// /// ``` /// # use fluvio_protocol::derive::Decode; /// # impl Default for ThreeChoice { fn default() -> Self { unimplemented!() } } /// #[derive(Decode)] /// pub enum ThreeChoice { /// First, /// Second, /// Third /// } /// ``` /// /// In this case, 1 is decoded as First, 2 as Second, 3 as Third. /// /// Currently, mixing enum variants are not supported. /// /// /// Decode support container and field level attributes. /// Container level applies to struct. /// For field attributes /// * `#[varint]` force decode using varint format. /// * `#fluvio(min_version = <version>)]` decodes only if version is equal or greater than min_version /// * `#fluvio(max_version = <version>)]`decodes only if version is less or equal than max_version /// #[proc_macro_derive(Decode, attributes(varint, fluvio))] pub fn fluvio_decode(tokens: TokenStream) -> TokenStream { let input = parse_macro_input![tokens as ast::DeriveItem]; let expanded = generate_decode_trait_impls(&input); expanded.into() } /// Custom derive for encoding structure or enum to bytes using Kafka protocol format. /// This assumes all fields(or enum variants) implement kafka encode traits. /// /// # Examples /// /// ``` /// use fluvio_protocol::Encoder; /// use fluvio_protocol::derive::Encode; /// /// #[derive(Encode)] /// pub struct SimpleRecord { /// val: u8 /// } /// /// let mut data = vec![]; /// /// let record = SimpleRecord { val: 4 }; /// record.encode(&mut data,0); /// /// assert_eq!(data[0],4); /// ``` /// /// Encode applies to either Struct of Enum. /// /// Encode respects version attributes. See Decode derive. #[proc_macro_derive(Encode, attributes(varint, fluvio))] pub fn fluvio_encode(tokens: TokenStream) -> TokenStream { let input = parse_macro_input![tokens as ast::DeriveItem]; let expanded = generate_encode_trait_impls(&input); expanded.into() } #[proc_macro] pub fn fluvio_api(tokens: TokenStream) -> TokenStream { let inputs = parse_macro_input![tokens as syn::DeriveInput]; let expanded = parse_and_generate_api(&inputs); expanded.into() } /// Custom derive for implementing Request trait. /// This derives requires `fluvio` /// /// # Examples /// /// ``` /// use fluvio_protocol::derive::Decode; /// use fluvio_protocol::derive::Encode; /// use fluvio_protocol::api::Request; /// use fluvio_protocol::derive::RequestApi as Request; /// /// #[fluvio(default,api_min_version = 5, api_max_version = 6, api_key = 10, response = "SimpleResponse")] /// #[derive(Debug, Default, Encode, Decode, Request)] /// pub struct SimpleRequest { /// val: u8 /// } /// /// #[derive(Debug, Default, Encode, Decode)] /// #[fluvio(default)] /// pub struct SimpleResponse { /// pub value: i8, /// } /// ``` /// /// RequestApi derives respects following attributes in `fluvio` /// /// * `api_min_version`: min version that API supports. This is required /// * `api_max_version`: max version that API supports. This is optional. /// * `api_key`: API number. This is required /// * `response`: Response struct. This is required /// #[proc_macro_derive(RequestApi, attributes(varint, fluvio))] pub fn fluvio_request(tokens: TokenStream) -> TokenStream { let inputs = parse_macro_input![tokens as syn::DeriveInput]; let expanded = generate_request_traits(&inputs); expanded.into() } /// Custom derive for generating default structure /// /// Example: /// /// ``` /// use fluvio_protocol::derive::FluvioDefault; /// /// #[derive(FluvioDefault)] /// #[fluvio(default)] /// pub struct SimpleRecord { /// #[fluvio(default = "12")] /// val: u8 /// } /// /// let record = SimpleRecord::default(); /// assert_eq!(record.val, 12); /// ``` /// /// `default` assignment can be any Rust expression. #[proc_macro_derive(FluvioDefault, attributes(fluvio))] pub fn fluvio_default(tokens: TokenStream) -> TokenStream { let input = parse_macro_input![tokens as ast::DeriveItem]; let expanded = generate_default_trait_impls(&input); expanded.into() }