Skip to main content

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

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