#![forbid(unsafe_code)]
use bytes::Bytes;
#[derive(Debug, Clone)]
pub struct Mp4Attachment {
pub attachment_type: [u8; 4],
pub data: Bytes,
pub filename: Option<String>,
}
impl Mp4Attachment {
#[must_use]
pub const fn new(attachment_type: [u8; 4], data: Bytes) -> Self {
Self {
attachment_type,
data,
filename: None,
}
}
#[must_use]
pub fn with_filename(mut self, filename: impl Into<String>) -> Self {
self.filename = Some(filename.into());
self
}
#[must_use]
pub fn size(&self) -> usize {
self.data.len()
}
#[must_use]
pub fn type_string(&self) -> String {
String::from_utf8_lossy(&self.attachment_type).into_owned()
}
}
pub struct Mp4AttachmentTypes;
impl Mp4AttachmentTypes {
pub const COVER: [u8; 4] = *b"covr";
pub const COPYRIGHT: [u8; 4] = *b"cprt";
pub const DESCRIPTION: [u8; 4] = *b"desc";
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_mp4_attachment() {
let data = Bytes::from_static(b"cover image");
let attachment =
Mp4Attachment::new(Mp4AttachmentTypes::COVER, data).with_filename("cover.jpg");
assert_eq!(attachment.attachment_type, Mp4AttachmentTypes::COVER);
assert_eq!(attachment.size(), 11);
assert_eq!(attachment.filename, Some("cover.jpg".into()));
assert_eq!(attachment.type_string(), "covr");
}
}