crabka_protocol/opt/rustwide/workdir/generated/
DelegationTokenRecord.owned.rs1use bytes::{Buf, BufMut};
4
5use crate::primitives::fixed::{get_i64, put_i64};
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 DelegationTokenRecord {
23 pub owner: String,
24 pub requester: String,
25 pub renewers: Vec<String>,
26 pub issue_timestamp: i64,
27 pub max_timestamp: i64,
28 pub expiration_timestamp: i64,
29 pub token_id: String,
30 pub unknown_tagged_fields: UnknownTaggedFields,
31}
32impl Encode for DelegationTokenRecord {
33 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
34 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
35 return Err(ProtocolError::SchemaMismatch(
36 "DelegationTokenRecord version out of range",
37 ));
38 }
39 let flex = is_flexible(version);
40 if version >= 0 {
41 if flex {
42 put_compact_string(buf, &self.owner);
43 } else {
44 put_string(buf, &self.owner);
45 }
46 }
47 if version >= 0 {
48 if flex {
49 put_compact_string(buf, &self.requester);
50 } else {
51 put_string(buf, &self.requester);
52 }
53 }
54 if version >= 0 {
55 {
56 crate::primitives::array::put_array_len(buf, (self.renewers).len(), flex);
57 for it in &self.renewers {
58 if flex {
59 put_compact_string(buf, it);
60 } else {
61 put_string(buf, it);
62 }
63 }
64 }
65 }
66 if version >= 0 {
67 put_i64(buf, self.issue_timestamp);
68 }
69 if version >= 0 {
70 put_i64(buf, self.max_timestamp);
71 }
72 if version >= 0 {
73 put_i64(buf, self.expiration_timestamp);
74 }
75 if version >= 0 {
76 if flex {
77 put_compact_string(buf, &self.token_id);
78 } else {
79 put_string(buf, &self.token_id);
80 }
81 }
82 if flex {
83 let tagged = WriteTaggedFields::new();
84 tagged.write(buf, &self.unknown_tagged_fields);
85 }
86 Ok(())
87 }
88 fn encoded_len(&self, version: i16) -> usize {
89 let flex = is_flexible(version);
90 let mut n: usize = 0;
91 if version >= 0 {
92 n += if flex {
93 compact_string_len(&self.owner)
94 } else {
95 string_len(&self.owner)
96 };
97 }
98 if version >= 0 {
99 n += if flex {
100 compact_string_len(&self.requester)
101 } else {
102 string_len(&self.requester)
103 };
104 }
105 if version >= 0 {
106 n += {
107 let prefix =
108 crate::primitives::array::array_len_prefix_len((self.renewers).len(), flex);
109 let body: usize = (self.renewers)
110 .iter()
111 .map(|it| {
112 if flex {
113 compact_string_len(it)
114 } else {
115 string_len(it)
116 }
117 })
118 .sum();
119 prefix + body
120 };
121 }
122 if version >= 0 {
123 n += 8;
124 }
125 if version >= 0 {
126 n += 8;
127 }
128 if version >= 0 {
129 n += 8;
130 }
131 if version >= 0 {
132 n += if flex {
133 compact_string_len(&self.token_id)
134 } else {
135 string_len(&self.token_id)
136 };
137 }
138 if flex {
139 let known_pairs: Vec<(u32, usize)> = Vec::new();
140 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
141 }
142 n
143 }
144}
145impl Decode<'_> for DelegationTokenRecord {
146 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
147 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
148 return Err(ProtocolError::SchemaMismatch(
149 "DelegationTokenRecord version out of range",
150 ));
151 }
152 let flex = is_flexible(version);
153 let mut out = Self::default();
154 if version >= 0 {
155 out.owner = if flex {
156 get_compact_string_owned(buf)?
157 } else {
158 get_string_owned(buf)?
159 };
160 }
161 if version >= 0 {
162 out.requester = if flex {
163 get_compact_string_owned(buf)?
164 } else {
165 get_string_owned(buf)?
166 };
167 }
168 if version >= 0 {
169 out.renewers = {
170 let n = crate::primitives::array::get_array_len(buf, flex)?;
171 let mut v = Vec::with_capacity(n);
172 for _ in 0..n {
173 v.push(if flex {
174 get_compact_string_owned(buf)?
175 } else {
176 get_string_owned(buf)?
177 });
178 }
179 v
180 };
181 }
182 if version >= 0 {
183 out.issue_timestamp = get_i64(buf)?;
184 }
185 if version >= 0 {
186 out.max_timestamp = get_i64(buf)?;
187 }
188 if version >= 0 {
189 out.expiration_timestamp = get_i64(buf)?;
190 }
191 if version >= 0 {
192 out.token_id = if flex {
193 get_compact_string_owned(buf)?
194 } else {
195 get_string_owned(buf)?
196 };
197 }
198 if flex {
199 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
200 }
201 Ok(out)
202 }
203}
204#[cfg(test)]
205impl DelegationTokenRecord {
206 #[must_use]
207 pub fn populated(version: i16) -> Self {
208 let mut m = Self::default();
209 if version >= 0 {
210 m.owner = "x".to_string();
211 }
212 if version >= 0 {
213 m.requester = "x".to_string();
214 }
215 if version >= 0 {
216 m.renewers = vec!["x".to_string()];
217 }
218 if version >= 0 {
219 m.issue_timestamp = 1i64;
220 }
221 if version >= 0 {
222 m.max_timestamp = 1i64;
223 }
224 if version >= 0 {
225 m.expiration_timestamp = 1i64;
226 }
227 if version >= 0 {
228 m.token_id = "x".to_string();
229 }
230 m
231 }
232}
233
234#[must_use]
237#[allow(unused_comparisons)]
238pub fn default_json(version: i16) -> ::serde_json::Value {
239 let mut obj = ::serde_json::Map::new();
240 obj.insert(
241 "owner".to_string(),
242 ::serde_json::Value::String(String::new()),
243 );
244 obj.insert(
245 "requester".to_string(),
246 ::serde_json::Value::String(String::new()),
247 );
248 obj.insert("renewers".to_string(), ::serde_json::Value::Array(vec![]));
249 obj.insert("issueTimestamp".to_string(), ::serde_json::json!(0));
250 obj.insert("maxTimestamp".to_string(), ::serde_json::json!(0));
251 obj.insert("expirationTimestamp".to_string(), ::serde_json::json!(0));
252 obj.insert(
253 "tokenId".to_string(),
254 ::serde_json::Value::String(String::new()),
255 );
256 ::serde_json::Value::Object(obj)
257}