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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use std::fmt::Debug;

use async_trait::async_trait;
use byteorder_async::{BigEndian};
use futures::{AsyncRead, AsyncSeek};
use byteorder_async::ReaderToByteOrder;
use crate::bytes_write::FlagTrait;

use crate::error::MP4Error;

#[async_trait]
pub trait Mp4Readable: Sized {
    async fn read<R: ReadMp4>(reader: &mut R) -> Result<Self, MP4Error>;
}

#[async_trait]
pub trait Mp4VersionedReadable<F: FlagTrait>: Sized {
    async fn versioned_read<R: ReadMp4>(version: u8, flags: F, reader: &mut R) -> Result<Self, MP4Error>;
}

#[async_trait]
impl<F: FlagTrait, T: Mp4Readable> Mp4VersionedReadable<F> for  T {
    async fn versioned_read<R: ReadMp4>(_: u8, _: F, reader: &mut R) -> Result<Self, MP4Error> {
        T::read(reader).await
    }
}

#[async_trait]
impl Mp4Readable for u8 {
    async fn read<R: ReadMp4>(reader: &mut R) -> Result<Self, MP4Error> {
        Ok(reader.byte_order().read_u8().await?)
    }
}

#[async_trait]
impl Mp4Readable for u16 {
    async fn read<R: ReadMp4>(reader: &mut R) -> Result<Self, MP4Error> {
        Ok(reader.byte_order().read_u16::<BigEndian>().await?)
    }
}
#[async_trait]
impl Mp4Readable for u32 {
    async fn read<R: ReadMp4>(reader: &mut R) -> Result<Self, MP4Error> {
        Ok(reader.byte_order().read_u32::<BigEndian>().await?)
    }
}
#[async_trait]
impl Mp4Readable for u64 {
    async fn read<R: ReadMp4>(reader: &mut R) -> Result<Self, MP4Error> {
        Ok(reader.byte_order().read_u64::<BigEndian>().await?)
    }
}

#[async_trait]
impl Mp4Readable for i8 {
    async fn read<R: ReadMp4>(reader: &mut R) -> Result<Self, MP4Error> {
        Ok(reader.byte_order().read_i8().await?)
    }
}

#[async_trait]
impl Mp4Readable for i16 {
    async fn read<R: ReadMp4>(reader: &mut R) -> Result<Self, MP4Error> {
        Ok(reader.byte_order().read_i16::<BigEndian>().await?)
    }
}
#[async_trait]
impl Mp4Readable for i32 {
    async fn read<R: ReadMp4>(reader: &mut R) -> Result<Self, MP4Error> {
        Ok(reader.byte_order().read_i32::<BigEndian>().await?)
    }
}
#[async_trait]
impl Mp4Readable for i64 {
    async fn read<R: ReadMp4>(reader: &mut R) -> Result<Self, MP4Error> {
        Ok(reader.byte_order().read_i64::<BigEndian>().await?)
    }
}

#[async_trait]
impl<const N: usize, T: Mp4Readable + Send + Sync + Debug> Mp4Readable for [T; N] {
    async fn read<R: ReadMp4>(reader: &mut R) -> Result<Self, MP4Error> {
        let mut out = vec![];
        for _ in 0..N {
            out.push(T::read(reader).await?);
        }
        Ok(out.try_into().unwrap())
    }
}


#[async_trait]
pub trait ReadMp4: AsyncRead + AsyncSeek + Unpin + Send + Sync + Sized {

    #[inline]
    async fn read_u24(&mut self) -> Result<u32, MP4Error> {
        Ok(self.byte_order().read_u24::<BigEndian>().await?)
    }

    #[inline]
    async fn read_i24(&mut self) -> Result<i32, MP4Error> {
        Ok(self.byte_order().read_i24::<BigEndian>().await?)
    }

    async fn read<T: Mp4Readable>(&mut self) -> Result<T, MP4Error> {
        T::read(self).await
    }

    async fn versioned_read<T: Mp4VersionedReadable<F>, F: FlagTrait>(&mut self, version: u8, flags: F) -> Result<T, MP4Error> { T::versioned_read(version, flags, self).await }
}

impl<T: AsyncRead + AsyncSeek + Unpin + Send + Sync> ReadMp4 for T {}