@@ -21,10 +21,11 @@ use crate::io::{FileLike, Length, Truncate};
21
21
use crate :: picture:: Picture ;
22
22
use crate :: tag:: companion_tag:: CompanionTag ;
23
23
use crate :: tag:: { Accessor , MergeTag , SplitTag , TagExt , TagType } ;
24
+ use crate :: ebml:: tag:: write:: { ElementWriterCtx , WriteableElement } ;
24
25
25
26
use std:: borrow:: Cow ;
26
27
use std:: collections:: HashMap ;
27
- use std:: io:: Write ;
28
+ use std:: io:: { Cursor , Write } ;
28
29
use std:: ops:: Deref ;
29
30
30
31
use lofty_attr:: tag;
@@ -356,23 +357,23 @@ impl TagExt for MatroskaTag {
356
357
357
358
fn save_to < F > (
358
359
& self ,
359
- _file : & mut F ,
360
- _write_options : WriteOptions ,
360
+ file : & mut F ,
361
+ write_options : WriteOptions ,
361
362
) -> std:: result:: Result < ( ) , Self :: Err >
362
363
where
363
364
F : FileLike ,
364
365
LoftyError : From < <F as Truncate >:: Error > ,
365
366
LoftyError : From < <F as Length >:: Error > ,
366
367
{
367
- todo ! ( )
368
+ MatroskaTagRef :: from ( self ) . write_to ( file , write_options )
368
369
}
369
370
370
371
fn dump_to < W : Write > (
371
372
& self ,
372
- _writer : & mut W ,
373
- _write_options : WriteOptions ,
373
+ writer : & mut W ,
374
+ write_options : WriteOptions ,
374
375
) -> std:: result:: Result < ( ) , Self :: Err > {
375
- todo ! ( )
376
+ MatroskaTagRef :: from ( self ) . dump_to ( writer , write_options )
376
377
}
377
378
378
379
fn clear ( & mut self ) {
@@ -440,51 +441,64 @@ impl From<crate::tag::Tag> for MatroskaTag {
440
441
}
441
442
}
442
443
443
- pub ( crate ) struct MatroskaTagRef < ' a , I >
444
- where
445
- I : Iterator < Item = TagRef < ' a > > ,
444
+ pub ( crate ) struct MatroskaTagRef < ' a >
446
445
{
447
- pub ( crate ) tags : I ,
446
+ pub ( crate ) tags : Vec < TagRef < ' a > > ,
448
447
}
449
448
450
- pub ( crate ) fn simple_tags_for_tag ( tag : & crate :: tag:: Tag ) -> impl Iterator < Item = TagRef < ' static > > {
451
- let mut mapped_tags: HashMap < TargetType , Vec < Cow < ' static , SimpleTag < ' static > > > > =
452
- HashMap :: new ( ) ;
453
- for item in & tag. items {
454
- if let Some ( ( simple_tag, target_type) ) = generic:: simple_tag_for_item ( Cow :: Borrowed ( item) ) {
455
- mapped_tags
456
- . entry ( target_type)
457
- . or_default ( )
458
- . push ( Cow :: Owned ( simple_tag) )
449
+ impl < ' a > From < & ' a MatroskaTag > for MatroskaTagRef < ' a > {
450
+ fn from ( value : & ' a MatroskaTag ) -> Self {
451
+ Self {
452
+ tags : value. tags . iter ( ) . map ( Into :: into) . collect :: < Vec < _ > > ( )
459
453
}
460
454
}
455
+ }
461
456
462
- mapped_tags
463
- . into_iter ( )
464
- . map ( |( target_type, simple_tags) | TagRef {
465
- targets : TargetDescriptor :: Basic ( target_type) ,
466
- simple_tags : Box :: new ( simple_tags. into_iter ( ) ) ,
467
- } )
457
+ impl < ' a > From < & ' a crate :: tag:: Tag > for MatroskaTagRef < ' static > {
458
+ fn from ( value : & ' a crate :: tag:: Tag ) -> Self {
459
+ let mut mapped_tags: HashMap < TargetType , Vec < Cow < ' static , SimpleTag < ' static > > > > =
460
+ HashMap :: new ( ) ;
461
+ for item in & value. items {
462
+ if let Some ( ( simple_tag, target_type) ) = generic:: simple_tag_for_item ( Cow :: Borrowed ( item) ) {
463
+ mapped_tags
464
+ . entry ( target_type)
465
+ . or_default ( )
466
+ . push ( Cow :: Owned ( simple_tag) )
467
+ }
468
+ }
469
+
470
+ let tags = mapped_tags
471
+ . into_iter ( )
472
+ . map ( |( target_type, simple_tags) | TagRef {
473
+ targets : TargetDescriptor :: Basic ( target_type) ,
474
+ simple_tags,
475
+ } ) . collect :: < Vec < _ > > ( ) ;
476
+
477
+ Self {
478
+ tags
479
+ }
480
+ }
468
481
}
469
482
470
- impl < ' a , I > MatroskaTagRef < ' a , I >
471
- where
472
- I : Iterator < Item = TagRef < ' a > > ,
483
+ impl < ' a > MatroskaTagRef < ' a >
473
484
{
474
- pub ( crate ) fn write_to < F > ( & mut self , _file : & mut F , _write_options : WriteOptions ) -> Result < ( ) >
485
+ pub ( crate ) fn write_to < F > ( & mut self , file : & mut F , write_options : WriteOptions ) -> Result < ( ) >
475
486
where
476
487
F : FileLike ,
477
488
LoftyError : From < <F as Truncate >:: Error > ,
478
489
LoftyError : From < <F as Length >:: Error > ,
479
490
{
480
- todo ! ( "Writing matroska tags" )
491
+ write :: write_to ( file , self , write_options )
481
492
}
482
493
483
494
pub ( crate ) fn dump_to < W : Write > (
484
495
& self ,
485
- _writer : & mut W ,
496
+ writer : & mut W ,
486
497
_write_options : WriteOptions ,
487
498
) -> Result < ( ) > {
488
- todo ! ( "Dumping matroska tags" )
499
+ let mut buf = Cursor :: new ( Vec :: new ( ) ) ;
500
+ self . write_element ( ElementWriterCtx :: default ( ) , & mut buf) ?;
501
+ writer. write_all ( & buf. into_inner ( ) ) ?;
502
+ Ok ( ( ) )
489
503
}
490
504
}
0 commit comments