Skip to main content

crabka_protocol/opt/rustwide/workdir/generated/
EnvelopeRequest.owned.rs

1// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2
3use crate::primitives::string_bytes::{
4    bytes_len, compact_bytes_len, compact_nullable_bytes_len, get_bytes_owned,
5    get_compact_bytes_owned, get_compact_nullable_bytes_owned, get_nullable_bytes_owned,
6    nullable_bytes_len, put_bytes, put_compact_bytes, put_compact_nullable_bytes,
7    put_nullable_bytes,
8};
9use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
10use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
11use bytes::{Buf, BufMut};
12
13pub const API_KEY: i16 = 58;
14pub const MIN_VERSION: i16 = 0;
15pub const MAX_VERSION: i16 = 0;
16pub const FLEXIBLE_MIN: i16 = 0;
17
18#[inline]
19fn is_flexible(version: i16) -> bool {
20    version >= FLEXIBLE_MIN
21}
22
23#[derive(Debug, Clone, PartialEq, Eq, Default)]
24pub struct EnvelopeRequest {
25    pub request_data: ::bytes::Bytes,
26    pub request_principal: Option<::bytes::Bytes>,
27    pub client_host_address: ::bytes::Bytes,
28    pub unknown_tagged_fields: UnknownTaggedFields,
29}
30impl Encode for EnvelopeRequest {
31    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
32        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
33            return Err(ProtocolError::UnsupportedVersion {
34                api_key: API_KEY,
35                version,
36            });
37        }
38        let flex = is_flexible(version);
39        if version >= 0 {
40            if flex {
41                put_compact_bytes(buf, &self.request_data);
42            } else {
43                put_bytes(buf, &self.request_data);
44            }
45        }
46        if version >= 0 {
47            if flex {
48                put_compact_nullable_bytes(buf, self.request_principal.as_deref());
49            } else {
50                put_nullable_bytes(buf, self.request_principal.as_deref());
51            }
52        }
53        if version >= 0 {
54            if flex {
55                put_compact_bytes(buf, &self.client_host_address);
56            } else {
57                put_bytes(buf, &self.client_host_address);
58            }
59        }
60        if flex {
61            let tagged = WriteTaggedFields::new();
62            tagged.write(buf, &self.unknown_tagged_fields);
63        }
64        Ok(())
65    }
66    fn encoded_len(&self, version: i16) -> usize {
67        let flex = is_flexible(version);
68        let mut n: usize = 0;
69        if version >= 0 {
70            n += if flex {
71                compact_bytes_len(&self.request_data)
72            } else {
73                bytes_len(&self.request_data)
74            };
75        }
76        if version >= 0 {
77            n += if flex {
78                compact_nullable_bytes_len(self.request_principal.as_deref())
79            } else {
80                nullable_bytes_len(self.request_principal.as_deref())
81            };
82        }
83        if version >= 0 {
84            n += if flex {
85                compact_bytes_len(&self.client_host_address)
86            } else {
87                bytes_len(&self.client_host_address)
88            };
89        }
90        if flex {
91            let known_pairs: Vec<(u32, usize)> = Vec::new();
92            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
93        }
94        n
95    }
96}
97impl Decode<'_> for EnvelopeRequest {
98    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
99        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
100            return Err(ProtocolError::UnsupportedVersion {
101                api_key: API_KEY,
102                version,
103            });
104        }
105        let flex = is_flexible(version);
106        let mut out = Self::default();
107        if version >= 0 {
108            out.request_data = if flex {
109                get_compact_bytes_owned(buf)?
110            } else {
111                get_bytes_owned(buf)?
112            };
113        }
114        if version >= 0 {
115            out.request_principal = if flex {
116                get_compact_nullable_bytes_owned(buf)?
117            } else {
118                get_nullable_bytes_owned(buf)?
119            };
120        }
121        if version >= 0 {
122            out.client_host_address = if flex {
123                get_compact_bytes_owned(buf)?
124            } else {
125                get_bytes_owned(buf)?
126            };
127        }
128        if flex {
129            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
130        }
131        Ok(out)
132    }
133}
134#[cfg(test)]
135impl EnvelopeRequest {
136    #[must_use]
137    pub fn populated(version: i16) -> Self {
138        let mut m = Self::default();
139        if version >= 0 {
140            m.request_data = ::bytes::Bytes::from_static(b"x");
141        }
142        if version >= 0 {
143            m.request_principal = Some(::bytes::Bytes::from_static(b"x"));
144        }
145        if version >= 0 {
146            m.client_host_address = ::bytes::Bytes::from_static(b"x");
147        }
148        m
149    }
150}
151
152/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
153/// Only includes fields valid for the given version.
154#[must_use]
155#[allow(unused_comparisons)]
156pub fn default_json(version: i16) -> ::serde_json::Value {
157    let mut obj = ::serde_json::Map::new();
158    obj.insert(
159        "requestData".to_string(),
160        ::serde_json::Value::String(String::new()),
161    );
162    obj.insert("requestPrincipal".to_string(), ::serde_json::Value::Null);
163    obj.insert(
164        "clientHostAddress".to_string(),
165        ::serde_json::Value::String(String::new()),
166    );
167    ::serde_json::Value::Object(obj)
168}
169
170impl crate::ProtocolRequest for EnvelopeRequest {
171    const API_KEY: i16 = API_KEY;
172    const MIN_VERSION: i16 = MIN_VERSION;
173    const MAX_VERSION: i16 = MAX_VERSION;
174    const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
175    type Response = super::envelope_response::EnvelopeResponse;
176}