Skip to main content

crabka_protocol/opt/rustwide/workdir/generated/
BeginTransactionRecord.owned.rs

1// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2
3use crate::primitives::string_bytes::{
4    compact_nullable_string_len, get_compact_nullable_string_owned, get_nullable_string_owned,
5    nullable_string_len, put_compact_nullable_string, put_nullable_string,
6};
7use crate::tagged_fields::{
8    WriteTaggedFields, encode_to_bytes, read_tagged_fields, tagged_fields_len,
9};
10use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
11use bytes::{Buf, BufMut};
12pub const MIN_VERSION: i16 = 0;
13pub const MAX_VERSION: i16 = 0;
14pub const FLEXIBLE_MIN: i16 = 0;
15#[inline]
16fn is_flexible(version: i16) -> bool {
17    version >= FLEXIBLE_MIN
18}
19#[derive(Debug, Clone, PartialEq, Eq, Default)]
20pub struct BeginTransactionRecord {
21    pub name: Option<String>,
22    pub unknown_tagged_fields: UnknownTaggedFields,
23}
24impl Encode for BeginTransactionRecord {
25    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
26        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
27            return Err(ProtocolError::SchemaMismatch(
28                "BeginTransactionRecord version out of range",
29            ));
30        }
31        let flex = is_flexible(version);
32        if flex {
33            let mut tagged = WriteTaggedFields::new();
34            if !(self.name.is_none()) {
35                let payload = encode_to_bytes(
36                    if flex {
37                        compact_nullable_string_len(self.name.as_deref())
38                    } else {
39                        nullable_string_len(self.name.as_deref())
40                    },
41                    |b| {
42                        if flex {
43                            put_compact_nullable_string(b, self.name.as_deref());
44                        } else {
45                            put_nullable_string(b, self.name.as_deref());
46                        }
47                        Ok(())
48                    },
49                );
50                tagged.add(0, payload);
51            }
52            tagged.write(buf, &self.unknown_tagged_fields);
53        }
54        Ok(())
55    }
56    fn encoded_len(&self, version: i16) -> usize {
57        let flex = is_flexible(version);
58        let mut n: usize = 0;
59        if flex {
60            let mut known_pairs: Vec<(u32, usize)> = Vec::new();
61            if !(self.name.is_none()) {
62                known_pairs.push((
63                    0,
64                    if flex {
65                        compact_nullable_string_len(self.name.as_deref())
66                    } else {
67                        nullable_string_len(self.name.as_deref())
68                    },
69                ));
70            }
71            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
72        }
73        n
74    }
75}
76impl Decode<'_> for BeginTransactionRecord {
77    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
78        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
79            return Err(ProtocolError::SchemaMismatch(
80                "BeginTransactionRecord version out of range",
81            ));
82        }
83        let flex = is_flexible(version);
84        let mut out = Self::default();
85        if flex {
86            let mut tag_name = None;
87            out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| match tag {
88                0 => {
89                    tag_name = Some({
90                        let b: &mut &[u8] = payload;
91                        if flex {
92                            get_compact_nullable_string_owned(b)?
93                        } else {
94                            get_nullable_string_owned(b)?
95                        }
96                    });
97                    Ok(true)
98                }
99                _ => Ok(false),
100            })?;
101            if let Some(v) = tag_name {
102                out.name = v;
103            }
104        }
105        Ok(out)
106    }
107}
108#[cfg(test)]
109impl BeginTransactionRecord {
110    #[must_use]
111    pub fn populated(version: i16) -> Self {
112        let mut m = Self::default();
113        if version >= 0 {
114            m.name = Some("x".to_string());
115        }
116        m
117    }
118}
119/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
120/// Only includes fields valid for the given version.
121#[must_use]
122#[allow(unused_comparisons)]
123pub fn default_json(version: i16) -> ::serde_json::Value {
124    let mut obj = ::serde_json::Map::new();
125    obj.insert("name".to_string(), ::serde_json::Value::Null);
126    ::serde_json::Value::Object(obj)
127}