use crate::*;
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Desc {
pub country_indicator: u16,
pub language_indicator: u16,
pub text: String,
}
impl Atom for Desc {
const KIND: FourCC = FourCC::new(b"desc");
fn decode_body<B: Buf>(buf: &mut B) -> Result<Self> {
let data = super::data::decode_text(buf)?;
Ok(Desc {
country_indicator: data.country_indicator,
language_indicator: data.language_indicator,
text: data.text,
})
}
fn encode_body<B: BufMut>(&self, buf: &mut B) -> Result<()> {
super::data::encode_text(
self.country_indicator,
self.language_indicator,
&self.text,
buf,
)
}
}
#[cfg(test)]
mod tests {
use super::*;
const ENCODED_DESC_LONG: &[u8] = &[
0x00, 0x00, 0x00, 0x1E, b'd', b'e', b's', b'c', 0x00, 0x00, 0x00, 0x16, b'd', b'a', b't', b'a', 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, b'a', b' ', b'd', b'e', b's', b'c',
];
#[test]
fn test_desc_long_style() {
let decoded = Desc::decode(&mut &ENCODED_DESC_LONG[..]).unwrap();
assert_eq!(decoded.country_indicator, 0);
assert_eq!(decoded.language_indicator, 0);
assert_eq!(decoded.text, "a desc");
let mut out = Vec::new();
decoded.encode(&mut out).unwrap();
assert_eq!(out, ENCODED_DESC_LONG);
}
#[test]
fn test_desc_short_style() {
let buf = [
0x00, 0x00, 0x00, 0x16, b'd', b'e', b's', b'c', 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, b'a', b' ', b'd', b'e', b's', b'c',
];
let decoded = Desc::decode(&mut &buf[..]).unwrap();
assert_eq!(decoded.country_indicator, 0);
assert_eq!(decoded.language_indicator, 0);
assert_eq!(decoded.text, "a desc");
}
}