use byteorder::{BigEndian, ReadBytesExt};
use serde::Serialize;
use std::io::{Cursor, Error as IoError, Read};
#[derive(Debug, Serialize, PartialEq, Eq)]
pub struct GroupMetadataKey {
pub group: String,
}
impl TryFrom<&[u8]> for GroupMetadataKey {
type Error = IoError;
fn try_from(buf: &[u8]) -> Result<Self, Self::Error> {
let mut reader = Cursor::new(buf);
let group = {
let length = i32::from(reader.read_i16::<BigEndian>()?);
if length < 0 {
return Err(IoError::new(
std::io::ErrorKind::InvalidData,
"Field is null",
));
} else if length > 0x7fff {
return Err(IoError::new(
std::io::ErrorKind::InvalidData,
"Length too long",
));
}
let mut buf =
vec![0u8; usize::try_from(length).expect("Cannot allocate buffer for the group")];
reader.read_exact(&mut buf)?;
String::from_utf8(buf)
.map_err(|_| IoError::new(std::io::ErrorKind::InvalidData, "Invalid UTF-8"))?
};
Ok(GroupMetadataKey { group })
}
}