Skip to main content

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

1// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2
3use bytes::{Buf, BufMut};
4
5use crate::primitives::fixed::{get_i16, put_i16};
6use crate::primitives::string_bytes::{
7    compact_string_len, get_compact_string_owned, get_string_owned, put_compact_string, put_string,
8    string_len,
9};
10use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
11use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
12pub const MIN_VERSION: i16 = 0;
13pub const MAX_VERSION: i16 = 0;
14pub const FLEXIBLE_MIN: i16 = 0;
15
16#[inline]
17fn is_flexible(version: i16) -> bool {
18    version >= FLEXIBLE_MIN
19}
20
21#[derive(Debug, Clone, PartialEq, Eq, Default)]
22pub struct FeatureLevelRecord {
23    pub name: String,
24    pub feature_level: i16,
25    pub unknown_tagged_fields: UnknownTaggedFields,
26}
27impl Encode for FeatureLevelRecord {
28    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
29        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
30            return Err(ProtocolError::SchemaMismatch(
31                "FeatureLevelRecord version out of range",
32            ));
33        }
34        let flex = is_flexible(version);
35        if version >= 0 {
36            if flex {
37                put_compact_string(buf, &self.name);
38            } else {
39                put_string(buf, &self.name);
40            }
41        }
42        if version >= 0 {
43            put_i16(buf, self.feature_level);
44        }
45        if flex {
46            let tagged = WriteTaggedFields::new();
47            tagged.write(buf, &self.unknown_tagged_fields);
48        }
49        Ok(())
50    }
51    fn encoded_len(&self, version: i16) -> usize {
52        let flex = is_flexible(version);
53        let mut n: usize = 0;
54        if version >= 0 {
55            n += if flex {
56                compact_string_len(&self.name)
57            } else {
58                string_len(&self.name)
59            };
60        }
61        if version >= 0 {
62            n += 2;
63        }
64        if flex {
65            let known_pairs: Vec<(u32, usize)> = Vec::new();
66            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
67        }
68        n
69    }
70}
71impl Decode<'_> for FeatureLevelRecord {
72    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
73        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
74            return Err(ProtocolError::SchemaMismatch(
75                "FeatureLevelRecord version out of range",
76            ));
77        }
78        let flex = is_flexible(version);
79        let mut out = Self::default();
80        if version >= 0 {
81            out.name = if flex {
82                get_compact_string_owned(buf)?
83            } else {
84                get_string_owned(buf)?
85            };
86        }
87        if version >= 0 {
88            out.feature_level = get_i16(buf)?;
89        }
90        if flex {
91            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
92        }
93        Ok(out)
94    }
95}
96#[cfg(test)]
97impl FeatureLevelRecord {
98    #[must_use]
99    pub fn populated(version: i16) -> Self {
100        let mut m = Self::default();
101        if version >= 0 {
102            m.name = "x".to_string();
103        }
104        if version >= 0 {
105            m.feature_level = 1i16;
106        }
107        m
108    }
109}
110
111/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
112/// Only includes fields valid for the given version.
113#[must_use]
114#[allow(unused_comparisons)]
115pub fn default_json(version: i16) -> ::serde_json::Value {
116    let mut obj = ::serde_json::Map::new();
117    obj.insert(
118        "name".to_string(),
119        ::serde_json::Value::String(String::new()),
120    );
121    obj.insert("featureLevel".to_string(), ::serde_json::json!(0));
122    ::serde_json::Value::Object(obj)
123}