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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use std::io::Write;
use std::ops::BitOr;
use byteorder::{BigEndian, WriteBytesExt};
use crate::error::MP4Error;

pub trait FlagTrait: Copy + Default + BitOr<Output=Self> + Into<u32> + From<u32> + Send + Sync + 'static {}
impl<T: Copy + Default + BitOr<Output=Self> + Into<u32> + From<u32> + Send + Sync + 'static> FlagTrait for T {}

pub trait Mp4Writable {
    fn byte_size(&self) -> usize;
    fn write<W: WriteMp4>(&self, writer: &mut W) -> Result<usize, MP4Error>;
}

pub trait Mp4VersionedWritable<F: FlagTrait> {
    fn required_version(&self) -> u8 {0}
    fn required_flags(&self) -> F {F::default()}
    fn versioned_byte_size(&self, version: u8, flags: F) -> usize;
    fn versioned_write<W: WriteMp4>(&self, version: u8, flags: F, writer: &mut W) -> Result<usize, MP4Error>;
}

impl<F: FlagTrait, T: Mp4Writable + Send + Sync> Mp4VersionedWritable<F> for  T {
    fn versioned_byte_size(&self, _: u8, _: F) -> usize {
        self.byte_size()
    }
    fn versioned_write<W: WriteMp4>(&self, _: u8, _: F, writer: &mut W) -> Result<usize, MP4Error> {
        self.write(writer)
    }
}

impl Mp4Writable for u8 {
    fn byte_size(&self) -> usize {
        1
    }

    fn write<W: WriteMp4>(&self, writer: &mut W) -> Result<usize, MP4Error> {
        writer.write_u8(*self)?;
        Ok(self.byte_size())
    }
}

impl Mp4Writable for u16 {
    fn byte_size(&self) -> usize {
        2
    }
    fn write<W: WriteMp4 + ?Sized>(&self, writer: &mut W) -> Result<usize, MP4Error> {
        writer.write_u16::<BigEndian>(*self)?;
        Ok(self.byte_size())
    }
}

impl Mp4Writable for u32 {
    fn byte_size(&self) -> usize {
        4
    }
    fn write<W: WriteMp4>(&self, writer: &mut W) -> Result<usize, MP4Error> {
        writer.write_u32::<BigEndian>(*self)?;
        Ok(self.byte_size())
    }
}

impl Mp4Writable for u64 {
    fn byte_size(&self) -> usize {
        8
    }
    fn write<W: WriteMp4>(&self, writer: &mut W) -> Result<usize, MP4Error> {
        writer.write_u64::<BigEndian>(*self)?;
        Ok(self.byte_size())
    }
}

impl Mp4Writable for i8 {
    fn byte_size(&self) -> usize {
        1
    }
    fn write<W: WriteMp4>(&self, writer: &mut W) -> Result<usize, MP4Error> {
        writer.write_i8(*self)?;
        Ok(self.byte_size())
    }
}

impl Mp4Writable for i16 {
    fn byte_size(&self) -> usize {
        2
    }
    fn write<W: WriteMp4>(&self, writer: &mut W) -> Result<usize, MP4Error> {
        writer.write_i16::<BigEndian>(*self)?;
        Ok(self.byte_size())
    }
}

impl Mp4Writable for i32 {
    fn byte_size(&self) -> usize {
        4
    }
    fn write<W: WriteMp4>(&self, writer: &mut W) -> Result<usize, MP4Error> {
        writer.write_i32::<BigEndian>(*self)?;
        Ok(self.byte_size())
    }
}

impl Mp4Writable for i64 {
    fn byte_size(&self) -> usize {
        8
    }
    fn write<W: WriteMp4>(&self, writer: &mut W) -> Result<usize, MP4Error> {
        writer.write_i64::<BigEndian>(*self)?;
        Ok(self.byte_size())
    }
}

impl<T: Mp4Writable + Send + Sync> Mp4Writable for [T] {
    fn byte_size(&self) -> usize {
        self.iter().map(Mp4Writable::byte_size).sum()
    }

    fn write<W: WriteMp4>(&self, writer: &mut W) -> Result<usize, MP4Error> {
        let mut count = 0;
        for elem in self {
            count += elem.write(writer)?;
        }
        Ok(count)
    }
}

impl<T: Mp4Writable  + Send + Sync, const N: usize> Mp4Writable for [T; N] {
    fn byte_size(&self) -> usize {
        self.as_slice().byte_size()
    }
    fn write<W: WriteMp4>(&self, writer: &mut W) -> Result<usize, MP4Error> {
        self.as_slice().write(writer)
    }
}

impl <T: Mp4Writable + Send + Sync> Mp4Writable for Option<T> {
    fn byte_size(&self) -> usize {
        self.iter().map(Mp4Writable::byte_size).sum()
    }
    fn write<W: WriteMp4>(&self, writer: &mut W) -> Result<usize, MP4Error> {
        if let Some(elem) = self {
            elem.write(writer)
        } else {
            Ok(0)
        }
    }
}

impl <T: Mp4Writable + Send + Sync> Mp4Writable for Vec<T> {
    fn byte_size(&self) -> usize {
        self.as_slice().byte_size()
    }
    fn write<W: WriteMp4>(&self, writer: &mut W) -> Result<usize, MP4Error> {
        self.as_slice().write(writer)
    }
}

pub trait WriteMp4: Write + Sized {

    #[inline]
    fn write_u24(&mut self, n: u32) -> Result<usize, MP4Error> {
       WriteBytesExt::write_u24::<BigEndian>(self, n)?;
        Ok(3)
    }

    #[inline]
    fn write_i24(&mut self, n: i32) -> Result<usize, MP4Error> {
        WriteBytesExt::write_i24::<BigEndian>(self, n)?;
        Ok(3)
    }
}

impl<T: Write> WriteMp4 for T {}