@@ -13,6 +13,39 @@ pub struct IlstBox {
13
13
pub items : HashMap < MetadataKey , IlstItemBox > ,
14
14
}
15
15
16
+ impl IlstBox {
17
+ pub fn get_type ( & self ) -> BoxType {
18
+ BoxType :: IlstBox
19
+ }
20
+
21
+ pub fn get_size ( & self ) -> u64 {
22
+ let mut size = HEADER_SIZE ;
23
+ for item in self . items . values ( ) {
24
+ size += item. get_size ( ) ;
25
+ }
26
+ size
27
+ }
28
+ }
29
+
30
+ impl Mp4Box for IlstBox {
31
+ fn box_type ( & self ) -> BoxType {
32
+ self . get_type ( )
33
+ }
34
+
35
+ fn box_size ( & self ) -> u64 {
36
+ self . get_size ( )
37
+ }
38
+
39
+ fn to_json ( & self ) -> Result < String > {
40
+ Ok ( serde_json:: to_string ( & self ) . unwrap ( ) )
41
+ }
42
+
43
+ fn summary ( & self ) -> Result < String > {
44
+ let s = format ! ( "item_count={}" , self . items. len( ) ) ;
45
+ Ok ( s)
46
+ }
47
+ }
48
+
16
49
impl < R : Read + Seek > ReadBox < & mut R > for IlstBox {
17
50
fn read_box ( reader : & mut R , size : u64 ) -> Result < Self > {
18
51
let start = box_start ( reader) ?;
@@ -54,11 +87,36 @@ impl<R: Read + Seek> ReadBox<&mut R> for IlstBox {
54
87
}
55
88
}
56
89
90
+ impl < W : Write > WriteBox < & mut W > for IlstBox {
91
+ fn write_box ( & self , writer : & mut W ) -> Result < u64 > {
92
+ let size = self . box_size ( ) ;
93
+ BoxHeader :: new ( self . box_type ( ) , size) . write ( writer) ?;
94
+
95
+ for ( key, value) in & self . items {
96
+ let name = match key {
97
+ MetadataKey :: Title => BoxType :: NameBox ,
98
+ MetadataKey :: Year => BoxType :: DayBox ,
99
+ MetadataKey :: Poster => BoxType :: CovrBox ,
100
+ MetadataKey :: Summary => BoxType :: DescBox ,
101
+ } ;
102
+ BoxHeader :: new ( name, value. get_size ( ) ) . write ( writer) ?;
103
+ value. data . write_box ( writer) ?;
104
+ }
105
+ Ok ( size)
106
+ }
107
+ }
108
+
57
109
#[ derive( Debug , Clone , PartialEq , Eq , Default , Serialize ) ]
58
110
pub struct IlstItemBox {
59
111
pub data : DataBox ,
60
112
}
61
113
114
+ impl IlstItemBox {
115
+ fn get_size ( & self ) -> u64 {
116
+ HEADER_SIZE + self . data . box_size ( )
117
+ }
118
+ }
119
+
62
120
impl < R : Read + Seek > ReadBox < & mut R > for IlstItemBox {
63
121
fn read_box ( reader : & mut R , size : u64 ) -> Result < Self > {
64
122
let start = box_start ( reader) ?;
@@ -130,3 +188,56 @@ fn item_to_u32(item: &IlstItemBox) -> Option<u32> {
130
188
_ => None ,
131
189
}
132
190
}
191
+
192
+ #[ cfg( test) ]
193
+ mod tests {
194
+ use super :: * ;
195
+ use crate :: mp4box:: BoxHeader ;
196
+ use std:: io:: Cursor ;
197
+
198
+ #[ test]
199
+ fn test_ilst ( ) {
200
+ let src_year = IlstItemBox {
201
+ data : DataBox {
202
+ data_type : DataType :: Text ,
203
+ data : b"test_year" . to_vec ( ) ,
204
+ } ,
205
+ } ;
206
+ let src_box = IlstBox {
207
+ items : [
208
+ ( MetadataKey :: Title , IlstItemBox :: default ( ) ) ,
209
+ ( MetadataKey :: Year , src_year) ,
210
+ ( MetadataKey :: Poster , IlstItemBox :: default ( ) ) ,
211
+ ( MetadataKey :: Summary , IlstItemBox :: default ( ) ) ,
212
+ ]
213
+ . into ( ) ,
214
+ } ;
215
+ let mut buf = Vec :: new ( ) ;
216
+ src_box. write_box ( & mut buf) . unwrap ( ) ;
217
+ assert_eq ! ( buf. len( ) , src_box. box_size( ) as usize ) ;
218
+
219
+ let mut reader = Cursor :: new ( & buf) ;
220
+ let header = BoxHeader :: read ( & mut reader) . unwrap ( ) ;
221
+ assert_eq ! ( header. name, BoxType :: IlstBox ) ;
222
+ assert_eq ! ( src_box. box_size( ) , header. size) ;
223
+
224
+ let dst_box = IlstBox :: read_box ( & mut reader, header. size ) . unwrap ( ) ;
225
+ assert_eq ! ( src_box, dst_box) ;
226
+ }
227
+
228
+ #[ test]
229
+ fn test_ilst_empty ( ) {
230
+ let src_box = IlstBox :: default ( ) ;
231
+ let mut buf = Vec :: new ( ) ;
232
+ src_box. write_box ( & mut buf) . unwrap ( ) ;
233
+ assert_eq ! ( buf. len( ) , src_box. box_size( ) as usize ) ;
234
+
235
+ let mut reader = Cursor :: new ( & buf) ;
236
+ let header = BoxHeader :: read ( & mut reader) . unwrap ( ) ;
237
+ assert_eq ! ( header. name, BoxType :: IlstBox ) ;
238
+ assert_eq ! ( src_box. box_size( ) , header. size) ;
239
+
240
+ let dst_box = IlstBox :: read_box ( & mut reader, header. size ) . unwrap ( ) ;
241
+ assert_eq ! ( src_box, dst_box) ;
242
+ }
243
+ }
0 commit comments