use crate::byte_reader::ByteReader;
use crate::error::Result;
use byteorder::WriteBytesExt;
use std::fmt;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TargetPath {
pub type_path_kind: u8,
pub type_argument_index: u8,
}
impl TargetPath {
pub fn from_bytes(bytes: &mut ByteReader<'_>) -> Result<TargetPath> {
let type_path_kind = bytes.read_u8()?;
let type_argument_index = bytes.read_u8()?;
let target_path = TargetPath {
type_path_kind,
type_argument_index,
};
Ok(target_path)
}
pub fn to_bytes(&self, bytes: &mut Vec<u8>) -> Result<()> {
bytes.write_u8(self.type_path_kind)?;
bytes.write_u8(self.type_argument_index)?;
Ok(())
}
}
impl fmt::Display for TargetPath {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"TargetPath[type_path_kind={}, type_argument_index={}]",
self.type_path_kind, self.type_argument_index
)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_serialization() -> Result<()> {
let target_path = TargetPath {
type_path_kind: 1,
type_argument_index: 2,
};
let expected_value = [1, 2];
assert_eq!(
"TargetPath[type_path_kind=1, type_argument_index=2]",
target_path.to_string()
);
let mut bytes = Vec::new();
target_path.clone().to_bytes(&mut bytes)?;
assert_eq!(expected_value, &bytes[..]);
let mut bytes = ByteReader::new(&expected_value);
assert_eq!(target_path, TargetPath::from_bytes(&mut bytes)?);
Ok(())
}
}