Skip to main content

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

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