use crate::attributes::AnnotationElement;
use crate::byte_reader::ByteReader;
use crate::error::Result;
use byteorder::{BigEndian, WriteBytesExt};
use std::fmt;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AnnotationValuePair {
pub name_index: u16,
pub value: AnnotationElement,
}
impl AnnotationValuePair {
pub fn from_bytes(bytes: &mut ByteReader<'_>) -> Result<AnnotationValuePair> {
let name_index = bytes.read_u16()?;
let value = AnnotationElement::from_bytes(bytes)?;
let annotation_value_pair = AnnotationValuePair { name_index, value };
Ok(annotation_value_pair)
}
pub fn to_bytes(&self, bytes: &mut Vec<u8>) -> Result<()> {
bytes.write_u16::<BigEndian>(self.name_index)?;
self.value.to_bytes(bytes)
}
}
impl fmt::Display for AnnotationValuePair {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "name_index: {}, value: {}", self.name_index, self.value)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_to_string() {
let annotation_value_pair = AnnotationValuePair {
name_index: 1,
value: AnnotationElement::Byte {
const_value_index: 42,
},
};
assert_eq!(
"name_index: 1, value: Byte { const_value_index: 42 }",
annotation_value_pair.to_string()
);
}
#[test]
fn test_serialization() -> Result<()> {
let annotation_value_pair = AnnotationValuePair {
name_index: 1,
value: AnnotationElement::Byte {
const_value_index: 42,
},
};
let expected_bytes = [0, 1, b'B', 0, 42];
let mut bytes = Vec::new();
annotation_value_pair.to_bytes(&mut bytes)?;
assert_eq!(expected_bytes, &bytes[..]);
let mut bytes = ByteReader::new(&expected_bytes);
assert_eq!(
annotation_value_pair,
AnnotationValuePair::from_bytes(&mut bytes)?
);
Ok(())
}
}