Skip to main content

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

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