use ostrya_gvariant::{GvDecode, GvEncode, GvType};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Be32(pub u32);
impl GvType for Be32 {
const SIGNATURE: &'static str = "u";
const ALIGNMENT: usize = 4;
const FIXED_SIZE: Option<usize> = Some(4);
}
impl GvEncode for Be32 {
fn encode(&self, out: &mut Vec<u8>) -> ostrya_gvariant::Result<()> {
out.extend_from_slice(&self.0.to_be_bytes());
Ok(())
}
}
impl<'a> GvDecode<'a> for Be32 {
fn decode(data: &'a [u8]) -> ostrya_gvariant::Result<Self> {
let arr: [u8; 4] = data
.try_into()
.map_err(|_| ostrya_gvariant::Error::NotNormal("u32 has the wrong size"))?;
Ok(Be32(u32::from_be_bytes(arr)))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Be64(pub u64);
impl GvType for Be64 {
const SIGNATURE: &'static str = "t";
const ALIGNMENT: usize = 8;
const FIXED_SIZE: Option<usize> = Some(8);
}
impl GvEncode for Be64 {
fn encode(&self, out: &mut Vec<u8>) -> ostrya_gvariant::Result<()> {
out.extend_from_slice(&self.0.to_be_bytes());
Ok(())
}
}
impl<'a> GvDecode<'a> for Be64 {
fn decode(data: &'a [u8]) -> ostrya_gvariant::Result<Self> {
let arr: [u8; 8] = data
.try_into()
.map_err(|_| ostrya_gvariant::Error::NotNormal("u64 has the wrong size"))?;
Ok(Be64(u64::from_be_bytes(arr)))
}
}
#[cfg(test)]
mod tests {
use super::*;
use ostrya_gvariant::encode_to_vec;
#[test]
fn be32_writes_big_endian_and_round_trips() {
let bytes = encode_to_vec(&Be32(0x0102_0304)).unwrap();
assert_eq!(bytes, [0x01, 0x02, 0x03, 0x04]);
assert_eq!(Be32::decode(&bytes).unwrap(), Be32(0x0102_0304));
}
#[test]
fn be64_writes_big_endian_and_round_trips() {
let bytes = encode_to_vec(&Be64(0x0102_0304_0506_0708)).unwrap();
assert_eq!(bytes, [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]);
assert_eq!(Be64::decode(&bytes).unwrap(), Be64(0x0102_0304_0506_0708));
}
#[test]
fn rejects_wrong_width() {
assert!(Be32::decode(&[0; 3]).is_err());
assert!(Be64::decode(&[0; 7]).is_err());
}
}