crabka_protocol/opt/rustwide/workdir/generated/
DefaultPrincipalData.borrowed.rs1use crate::primitives::fixed::{get_bool, put_bool};
3use crate::primitives::string_bytes::{
4 compact_string_len, put_compact_string, put_string, string_len,
5};
6use crate::primitives::string_bytes_borrowed::{get_compact_string_borrowed, get_string_borrowed};
7use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
8use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
9use bytes::BufMut;
10pub const MIN_VERSION: i16 = 0;
11pub const MAX_VERSION: i16 = 0;
12pub const FLEXIBLE_MIN: i16 = 0;
13#[inline]
14fn is_flexible(version: i16) -> bool {
15 version >= FLEXIBLE_MIN
16}
17#[derive(Debug, Clone, PartialEq, Eq, Default)]
18pub struct DefaultPrincipalData<'a> {
19 pub type_: &'a str,
20 pub name: &'a str,
21 pub token_authenticated: bool,
22 pub unknown_tagged_fields: UnknownTaggedFields,
23}
24impl DefaultPrincipalData<'_> {
25 pub fn to_owned(&self) -> crate::owned::default_principal_data::DefaultPrincipalData {
26 crate::owned::default_principal_data::DefaultPrincipalData {
27 type_: (self.type_).to_string(),
28 name: (self.name).to_string(),
29 token_authenticated: (self.token_authenticated),
30 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
31 }
32 }
33}
34impl Encode for DefaultPrincipalData<'_> {
35 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
36 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
37 return Err(ProtocolError::SchemaMismatch(
38 "DefaultPrincipalData version out of range",
39 ));
40 }
41 let flex = is_flexible(version);
42 if version >= 0 {
43 if flex {
44 put_compact_string(buf, self.type_);
45 } else {
46 put_string(buf, self.type_);
47 }
48 }
49 if version >= 0 {
50 if flex {
51 put_compact_string(buf, self.name);
52 } else {
53 put_string(buf, self.name);
54 }
55 }
56 if version >= 0 {
57 put_bool(buf, self.token_authenticated);
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 += if flex {
70 compact_string_len(self.type_)
71 } else {
72 string_len(self.type_)
73 };
74 }
75 if version >= 0 {
76 n += if flex {
77 compact_string_len(self.name)
78 } else {
79 string_len(self.name)
80 };
81 }
82 if version >= 0 {
83 n += 1;
84 }
85 if flex {
86 let known_pairs: Vec<(u32, usize)> = Vec::new();
87 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
88 }
89 n
90 }
91}
92impl<'de> DecodeBorrow<'de> for DefaultPrincipalData<'de> {
93 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
94 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
95 return Err(ProtocolError::SchemaMismatch(
96 "DefaultPrincipalData version out of range",
97 ));
98 }
99 let flex = is_flexible(version);
100 let mut out = Self::default();
101 if version >= 0 {
102 out.type_ = if flex {
103 get_compact_string_borrowed(buf)?
104 } else {
105 get_string_borrowed(buf)?
106 };
107 }
108 if version >= 0 {
109 out.name = if flex {
110 get_compact_string_borrowed(buf)?
111 } else {
112 get_string_borrowed(buf)?
113 };
114 }
115 if version >= 0 {
116 out.token_authenticated = get_bool(buf)?;
117 }
118 if flex {
119 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
120 }
121 Ok(out)
122 }
123}
124#[cfg(test)]
125impl DefaultPrincipalData<'_> {
126 #[must_use]
127 pub fn populated(version: i16) -> Self {
128 let mut m = Self::default();
129 if version >= 0 {
130 m.type_ = "x";
131 }
132 if version >= 0 {
133 m.name = "x";
134 }
135 if version >= 0 {
136 m.token_authenticated = true;
137 }
138 m
139 }
140}