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};

/// Information about an `ID3v2` frame that requires a language
///
/// See [`EncodedTextFrame`](crate::id3::v2::EncodedTextFrame)
#[derive(Clone, Debug, Eq)]
pub struct LanguageFrame {
	/// The encoding of the description and comment text
	pub encoding: TextEncoding,
	/// ISO-639-2 language code (3 bytes)
	pub language: [u8; 3],
	/// Unique content description
	pub description: String,
	/// The actual frame content
	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 {
	/// Convert a [`LanguageFrame`] to a byte vec
	///
	/// NOTE: This does not include a frame header
	///
	/// # Errors
	///
	/// * `language` is not exactly 3 bytes
	/// * `language` contains invalid characters `('a'..'z')`
	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)
	}
}