1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use crate::error::{ID3v2Error, ID3v2ErrorKind, Result};
use crate::util::text::{encode_text, TextEncoding};
use std::hash::{Hash, Hasher};
#[derive(Clone, Debug, Eq)]
pub struct LanguageFrame {
pub encoding: TextEncoding,
pub language: [u8; 3],
pub description: String,
pub content: String,
}
impl PartialEq for LanguageFrame {
fn eq(&self, other: &Self) -> bool {
self.description == other.description
}
}
impl Hash for LanguageFrame {
fn hash<H: Hasher>(&self, state: &mut H) {
self.description.hash(state);
}
}
impl LanguageFrame {
pub fn as_bytes(&self) -> Result<Vec<u8>> {
let mut bytes = vec![self.encoding as u8];
if self.language.len() != 3 || self.language.iter().any(|c| !(b'a'..=b'z').contains(&c)) {
return Err(ID3v2Error::new(ID3v2ErrorKind::Other(
"Invalid frame language found (expected 3 ascii characters)",
))
.into());
}
bytes.extend(self.language);
bytes.extend(encode_text(&self.description, self.encoding, true).iter());
bytes.extend(encode_text(&self.content, self.encoding, false));
Ok(bytes)
}
}