Skip to main content

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

1// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2
3use crate::primitives::fixed::{get_i32, put_i32};
4use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
5use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
6use bytes::{Buf, BufMut};
7pub const MIN_VERSION: i16 = 0;
8pub const MAX_VERSION: i16 = 1;
9pub const FLEXIBLE_MIN: i16 = 1;
10#[inline]
11fn is_flexible(version: i16) -> bool {
12    version >= FLEXIBLE_MIN
13}
14#[derive(Debug, Clone, PartialEq, Eq, Default)]
15pub struct ResponseHeader {
16    pub correlation_id: i32,
17    pub unknown_tagged_fields: UnknownTaggedFields,
18}
19impl Encode for ResponseHeader {
20    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
21        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
22            return Err(ProtocolError::SchemaMismatch(
23                "ResponseHeader version out of range",
24            ));
25        }
26        let flex = is_flexible(version);
27        if version >= 0 {
28            put_i32(buf, self.correlation_id);
29        }
30        if flex {
31            let tagged = WriteTaggedFields::new();
32            tagged.write(buf, &self.unknown_tagged_fields);
33        }
34        Ok(())
35    }
36    fn encoded_len(&self, version: i16) -> usize {
37        let flex = is_flexible(version);
38        let mut n: usize = 0;
39        if version >= 0 {
40            n += 4;
41        }
42        if flex {
43            let known_pairs: Vec<(u32, usize)> = Vec::new();
44            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
45        }
46        n
47    }
48}
49impl Decode<'_> for ResponseHeader {
50    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
51        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
52            return Err(ProtocolError::SchemaMismatch(
53                "ResponseHeader version out of range",
54            ));
55        }
56        let flex = is_flexible(version);
57        let mut out = Self::default();
58        if version >= 0 {
59            out.correlation_id = get_i32(buf)?;
60        }
61        if flex {
62            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
63        }
64        Ok(out)
65    }
66}
67#[cfg(test)]
68impl ResponseHeader {
69    #[must_use]
70    pub fn populated(version: i16) -> Self {
71        let mut m = Self::default();
72        if version >= 0 {
73            m.correlation_id = 1i32;
74        }
75        m
76    }
77}
78/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
79/// Only includes fields valid for the given version.
80#[must_use]
81#[allow(unused_comparisons)]
82pub fn default_json(version: i16) -> ::serde_json::Value {
83    let mut obj = ::serde_json::Map::new();
84    obj.insert("correlationId".to_string(), ::serde_json::json!(0));
85    ::serde_json::Value::Object(obj)
86}