@@ -21,10 +21,11 @@ use crate::io::{FileLike, Length, Truncate};
2121use crate :: picture:: Picture ;
2222use crate :: tag:: companion_tag:: CompanionTag ;
2323use crate :: tag:: { Accessor , MergeTag , SplitTag , TagExt , TagType } ;
24+ use crate :: ebml:: tag:: write:: { ElementWriterCtx , WriteableElement } ;
2425
2526use std:: borrow:: Cow ;
2627use std:: collections:: HashMap ;
27- use std:: io:: Write ;
28+ use std:: io:: { Cursor , Write } ;
2829use std:: ops:: Deref ;
2930
3031use lofty_attr:: tag;
@@ -356,23 +357,23 @@ impl TagExt for MatroskaTag {
356357
357358 fn save_to < F > (
358359 & self ,
359- _file : & mut F ,
360- _write_options : WriteOptions ,
360+ file : & mut F ,
361+ write_options : WriteOptions ,
361362 ) -> std:: result:: Result < ( ) , Self :: Err >
362363 where
363364 F : FileLike ,
364365 LoftyError : From < <F as Truncate >:: Error > ,
365366 LoftyError : From < <F as Length >:: Error > ,
366367 {
367- todo ! ( )
368+ MatroskaTagRef :: from ( self ) . write_to ( file , write_options )
368369 }
369370
370371 fn dump_to < W : Write > (
371372 & self ,
372- _writer : & mut W ,
373- _write_options : WriteOptions ,
373+ writer : & mut W ,
374+ write_options : WriteOptions ,
374375 ) -> std:: result:: Result < ( ) , Self :: Err > {
375- todo ! ( )
376+ MatroskaTagRef :: from ( self ) . dump_to ( writer , write_options )
376377 }
377378
378379 fn clear ( & mut self ) {
@@ -440,51 +441,64 @@ impl From<crate::tag::Tag> for MatroskaTag {
440441 }
441442}
442443
443- pub ( crate ) struct MatroskaTagRef < ' a , I >
444- where
445- I : Iterator < Item = TagRef < ' a > > ,
444+ pub ( crate ) struct MatroskaTagRef < ' a >
446445{
447- pub ( crate ) tags : I ,
446+ pub ( crate ) tags : Vec < TagRef < ' a > > ,
448447}
449448
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 < _ > > ( )
459453 }
460454 }
455+ }
461456
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+ }
468481}
469482
470- impl < ' a , I > MatroskaTagRef < ' a , I >
471- where
472- I : Iterator < Item = TagRef < ' a > > ,
483+ impl < ' a > MatroskaTagRef < ' a >
473484{
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 < ( ) >
475486 where
476487 F : FileLike ,
477488 LoftyError : From < <F as Truncate >:: Error > ,
478489 LoftyError : From < <F as Length >:: Error > ,
479490 {
480- todo ! ( "Writing matroska tags" )
491+ write :: write_to ( file , self , write_options )
481492 }
482493
483494 pub ( crate ) fn dump_to < W : Write > (
484495 & self ,
485- _writer : & mut W ,
496+ writer : & mut W ,
486497 _write_options : WriteOptions ,
487498 ) -> 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 ( ( ) )
489503 }
490504}
0 commit comments