fluvio_protocol/
lib.rs

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