Skip to main content

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

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