use crate::db::data::structural_field::FieldDecodeError;
fn decode_fixed_width_bytes<const N: usize>(
bytes: &[u8],
label: &'static str,
) -> Result<[u8; N], FieldDecodeError> {
bytes
.try_into()
.map_err(|_| FieldDecodeError::new(format!("structural binary: invalid {label} payload")))
}
pub(in crate::db::data::structural_field) const fn encode_i64_payload_bytes(value: i64) -> [u8; 8] {
value.to_be_bytes()
}
pub(in crate::db::data::structural_field) fn decode_i64_payload_bytes(
bytes: &[u8],
label: &'static str,
) -> Result<i64, FieldDecodeError> {
Ok(i64::from_be_bytes(decode_fixed_width_bytes(bytes, label)?))
}
pub(in crate::db::data::structural_field) const fn encode_u64_payload_bytes(value: u64) -> [u8; 8] {
value.to_be_bytes()
}
pub(in crate::db::data::structural_field) fn decode_u64_payload_bytes(
bytes: &[u8],
label: &'static str,
) -> Result<u64, FieldDecodeError> {
Ok(u64::from_be_bytes(decode_fixed_width_bytes(bytes, label)?))
}
pub(in crate::db::data::structural_field) const fn encode_f32_payload_bytes(value: f32) -> [u8; 4] {
value.to_bits().to_be_bytes()
}
pub(in crate::db::data::structural_field) const fn encode_f64_payload_bytes(value: f64) -> [u8; 8] {
value.to_bits().to_be_bytes()
}