1use std::fmt::{Debug, Display, Formatter};
2use crate::bytes_read::{Mp4Readable, ReadMp4};
3use crate::error::MP4Error;
4use async_trait::async_trait;
5use crate::bytes_write::{Mp4Writable, WriteMp4};
6
7#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
8pub struct BoxId(pub [u8; 4]);
9
10impl Debug for BoxId {
11 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
12 Display::fmt(self, f)
13 }
14}
15
16impl Display for BoxId {
17 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
18 match std::str::from_utf8(&self.0) {
19 Ok(str) => Display::fmt(str, f),
20 Err(_) => Display::fmt(&self.0.escape_ascii(), f)
21 }
22 }
23}
24
25impl BoxId {
26 pub const fn size() -> usize {
27 4
28 }
29}
30
31#[async_trait]
32impl Mp4Readable for BoxId {
33 async fn read<R: ReadMp4>(reader: &mut R) -> Result<Self, MP4Error> {
34 Ok(Self(reader.read().await?))
35 }
36}
37
38impl Mp4Writable for BoxId {
39 fn byte_size(&self) -> usize {
40 self.0.byte_size()
41 }
42
43 fn write<W: WriteMp4>(&self, writer: &mut W) -> Result<usize, MP4Error> {
44 self.0.write(writer)
45 }
46}
47
48
49impl PartialEq<&[u8;4]> for BoxId {
50 fn eq(&self, other: &&[u8;4]) -> bool {
51 &self.0 == *other
52 }
53}
54
55impl From<[u8;4]> for BoxId {
56 fn from(id: [u8; 4]) -> Self {
57 Self(id)
58 }
59}
60
61impl From<&[u8;4]> for BoxId {
62 fn from(id: &[u8; 4]) -> Self {
63 Self(*id)
64 }
65}