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