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