Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
DefaultPrincipalData.borrowed.rs

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