@@ -16,12 +16,12 @@ pub use tag_name::*;
1616pub use target:: * ;
1717
1818use crate :: config:: { WriteOptions , global_options} ;
19+ use crate :: ebml:: tag:: write:: { ElementWriterCtx , WriteableElement } ;
1920use crate :: error:: { LoftyError , Result } ;
2021use crate :: io:: { FileLike , Length , Truncate } ;
2122use crate :: picture:: Picture ;
2223use crate :: tag:: companion_tag:: CompanionTag ;
2324use crate :: tag:: { Accessor , MergeTag , SplitTag , TagExt , TagType } ;
24- use crate :: ebml:: tag:: write:: { ElementWriterCtx , WriteableElement } ;
2525
2626use std:: borrow:: Cow ;
2727use std:: collections:: HashMap ;
@@ -35,7 +35,7 @@ macro_rules! impl_accessor {
3535 paste:: paste! {
3636 $(
3737 fn $method( & self ) -> Option <Cow <' _, str >> {
38- self . get_str( MatroskaTagKey ( TargetType :: $target, $name. into ( ) ) )
38+ self . get_str( TargetType :: $target, $name)
3939 }
4040
4141 fn [ <set_ $method>] ( & mut self , value: String ) {
@@ -72,8 +72,11 @@ pub struct MatroskaTag {
7272pub struct MatroskaTagKey < ' a > ( TargetType , Cow < ' a , str > ) ;
7373
7474impl MatroskaTag {
75- fn get ( & self , key : MatroskaTagKey < ' _ > ) -> Option < & SimpleTag < ' _ > > {
76- let MatroskaTagKey ( target, key) = key;
75+ fn get < ' a , K > ( & self , target : TargetType , key : K ) -> Option < & SimpleTag < ' _ > >
76+ where
77+ K : Into < Cow < ' a , str > > ,
78+ {
79+ let key = key. into ( ) ;
7780
7881 let applicable_tags = self . tags . iter ( ) . filter ( |tag| tag. matches_target ( target) ) ;
7982 for applicable_tag in applicable_tags {
@@ -112,8 +115,11 @@ impl MatroskaTag {
112115 self . tags . get_mut ( pos. unwrap ( ) ) . unwrap ( )
113116 }
114117
115- fn get_str ( & self , key : MatroskaTagKey < ' _ > ) -> Option < Cow < ' _ , str > > {
116- let simple_tag = self . get ( key) ?;
118+ fn get_str < ' a , K > ( & self , target : TargetType , key : K ) -> Option < Cow < ' _ , str > >
119+ where
120+ K : Into < Cow < ' a , str > > ,
121+ {
122+ let simple_tag = self . get ( target, key) ?;
117123 simple_tag. get_str ( ) . map ( Cow :: from)
118124 }
119125
@@ -279,39 +285,60 @@ impl Accessor for MatroskaTag {
279285 ) ;
280286
281287 fn track ( & self ) -> Option < u32 > {
282- self . get ( MatroskaTagKey (
283- TargetType :: Track ,
284- Cow :: Borrowed ( "PART_NUMBER" ) ,
285- ) )
286- . and_then ( SimpleTag :: get_str)
287- . and_then ( |val| val. parse :: < u32 > ( ) . ok ( ) )
288+ self . get_str ( TargetType :: Track , TagName :: PartNumber )
289+ . and_then ( |val| val. parse :: < u32 > ( ) . ok ( ) )
288290 }
289291
290- fn set_track ( & mut self , _value : u32 ) {
291- todo ! ( )
292+ fn set_track ( & mut self , value : u32 ) {
293+ let tag = SimpleTag :: new ( TagName :: PartNumber , value. to_string ( ) ) ;
294+ self . push ( TargetType :: Track , tag) ;
292295 }
293296
294297 fn remove_track ( & mut self ) {
295298 todo ! ( )
296299 }
297300
298301 fn track_total ( & self ) -> Option < u32 > {
299- self . get ( MatroskaTagKey (
300- TargetType :: Album ,
301- Cow :: Borrowed ( "TOTAL_PARTS" ) ,
302- ) )
303- . and_then ( SimpleTag :: get_str)
304- . and_then ( |val| val. parse :: < u32 > ( ) . ok ( ) )
302+ self . get ( TargetType :: Album , TagName :: TotalParts )
303+ . and_then ( SimpleTag :: get_str)
304+ . and_then ( |val| val. parse :: < u32 > ( ) . ok ( ) )
305305 }
306306
307- fn set_track_total ( & mut self , _value : u32 ) {
308- todo ! ( )
307+ fn set_track_total ( & mut self , value : u32 ) {
308+ let tag = SimpleTag :: new ( TagName :: TotalParts , value. to_string ( ) ) ;
309+ self . push ( TargetType :: Album , tag) ;
309310 }
310311
311312 fn remove_track_total ( & mut self ) {
312313 todo ! ( )
313314 }
314315
316+ fn disk ( & self ) -> Option < u32 > {
317+ self . get ( TargetType :: Edition , TagName :: PartNumber )
318+ . and_then ( SimpleTag :: get_str)
319+ . and_then ( |val| val. parse :: < u32 > ( ) . ok ( ) )
320+ }
321+
322+ fn set_disk ( & mut self , value : u32 ) {
323+ let tag = SimpleTag :: new ( TagName :: PartNumber , value. to_string ( ) ) ;
324+ self . push ( TargetType :: Edition , tag) ;
325+ }
326+
327+ fn remove_disk ( & mut self ) { }
328+
329+ fn disk_total ( & self ) -> Option < u32 > {
330+ self . get ( TargetType :: Edition , TagName :: TotalParts )
331+ . and_then ( SimpleTag :: get_str)
332+ . and_then ( |val| val. parse :: < u32 > ( ) . ok ( ) )
333+ }
334+
335+ fn set_disk_total ( & mut self , value : u32 ) {
336+ let tag = SimpleTag :: new ( TagName :: TotalParts , value. to_string ( ) ) ;
337+ self . push ( TargetType :: Edition , tag) ;
338+ }
339+
340+ fn remove_disk_total ( & mut self ) { }
341+
315342 fn year ( & self ) -> Option < u32 > {
316343 // `DATE_RELEASED`
317344 todo ! ( )
@@ -441,15 +468,14 @@ impl From<crate::tag::Tag> for MatroskaTag {
441468 }
442469}
443470
444- pub ( crate ) struct MatroskaTagRef < ' a >
445- {
471+ pub ( crate ) struct MatroskaTagRef < ' a > {
446472 pub ( crate ) tags : Vec < TagRef < ' a > > ,
447473}
448474
449475impl < ' a > From < & ' a MatroskaTag > for MatroskaTagRef < ' a > {
450476 fn from ( value : & ' a MatroskaTag ) -> Self {
451477 Self {
452- tags : value. tags . iter ( ) . map ( Into :: into) . collect :: < Vec < _ > > ( )
478+ tags : value. tags . iter ( ) . map ( Into :: into) . collect :: < Vec < _ > > ( ) ,
453479 }
454480 }
455481}
@@ -459,7 +485,9 @@ impl<'a> From<&'a crate::tag::Tag> for MatroskaTagRef<'static> {
459485 let mut mapped_tags: HashMap < TargetType , Vec < Cow < ' static , SimpleTag < ' static > > > > =
460486 HashMap :: new ( ) ;
461487 for item in & value. items {
462- if let Some ( ( simple_tag, target_type) ) = generic:: simple_tag_for_item ( Cow :: Borrowed ( item) ) {
488+ if let Some ( ( simple_tag, target_type) ) =
489+ generic:: simple_tag_for_item ( Cow :: Borrowed ( item) )
490+ {
463491 mapped_tags
464492 . entry ( target_type)
465493 . or_default ( )
@@ -472,16 +500,14 @@ impl<'a> From<&'a crate::tag::Tag> for MatroskaTagRef<'static> {
472500 . map ( |( target_type, simple_tags) | TagRef {
473501 targets : TargetDescriptor :: Basic ( target_type) ,
474502 simple_tags,
475- } ) . collect :: < Vec < _ > > ( ) ;
503+ } )
504+ . collect :: < Vec < _ > > ( ) ;
476505
477- Self {
478- tags
479- }
506+ Self { tags }
480507 }
481508}
482509
483- impl < ' a > MatroskaTagRef < ' a >
484- {
510+ impl < ' a > MatroskaTagRef < ' a > {
485511 pub ( crate ) fn write_to < F > ( & mut self , file : & mut F , write_options : WriteOptions ) -> Result < ( ) >
486512 where
487513 F : FileLike ,
0 commit comments