use crate::primitives::string_bytes::{
compact_nullable_string_len, nullable_string_len, put_compact_nullable_string,
put_nullable_string,
};
use crate::tagged_fields::{
WriteTaggedFields, encode_to_bytes, read_tagged_fields, tagged_fields_len,
};
use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
use bytes::BufMut;
pub const MIN_VERSION: i16 = 0;
pub const MAX_VERSION: i16 = 0;
pub const FLEXIBLE_MIN: i16 = 0;
#[inline]
fn is_flexible(version: i16) -> bool {
version >= FLEXIBLE_MIN
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct BeginTransactionRecord {
pub name: Option<String>,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl BeginTransactionRecord {
pub fn to_owned(&self) -> crate::owned::begin_transaction_record::BeginTransactionRecord {
crate::owned::begin_transaction_record::BeginTransactionRecord {
name: self.name.clone(),
unknown_tagged_fields: self.unknown_tagged_fields.clone(),
}
}
}
impl Encode for BeginTransactionRecord {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
return Err(ProtocolError::SchemaMismatch(
"BeginTransactionRecord version out of range",
));
}
let flex = is_flexible(version);
if flex {
let mut tagged = WriteTaggedFields::new();
if !(self.name.is_none()) {
let payload = encode_to_bytes(
if flex {
compact_nullable_string_len(self.name.as_deref())
} else {
nullable_string_len(self.name.as_deref())
},
|b| {
if flex {
put_compact_nullable_string(b, self.name.as_deref());
} else {
put_nullable_string(b, self.name.as_deref());
}
Ok(())
},
);
tagged.add(0, payload);
}
tagged.write(buf, &self.unknown_tagged_fields);
}
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
let flex = is_flexible(version);
let mut n: usize = 0;
if flex {
let mut known_pairs: Vec<(u32, usize)> = Vec::new();
if !(self.name.is_none()) {
known_pairs.push((
0,
if flex {
compact_nullable_string_len(self.name.as_deref())
} else {
nullable_string_len(self.name.as_deref())
},
));
}
n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
}
n
}
}
impl<'de> DecodeBorrow<'de> for BeginTransactionRecord {
fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
return Err(ProtocolError::SchemaMismatch(
"BeginTransactionRecord version out of range",
));
}
let flex = is_flexible(version);
let mut out = Self::default();
if flex {
let mut tag_name = None;
out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| match tag {
0 => {
tag_name = Some({
let b: &mut &[u8] = payload;
if flex {
crate::primitives::string_bytes::get_compact_nullable_string_owned(b)?
} else {
crate::primitives::string_bytes::get_nullable_string_owned(b)?
}
});
Ok(true)
}
_ => Ok(false),
})?;
if let Some(v) = tag_name {
out.name = v;
}
}
Ok(out)
}
}
#[cfg(test)]
impl BeginTransactionRecord {
#[must_use]
pub fn populated(version: i16) -> Self {
let mut m = Self::default();
if version >= 0 {
m.name = Some("x".to_string());
}
m
}
}