File tree Expand file tree Collapse file tree 5 files changed +47
-14
lines changed Expand file tree Collapse file tree 5 files changed +47
-14
lines changed Original file line number Diff line number Diff line change 4
4
//! which can be extended at any time.
5
5
6
6
use crate :: file:: FileType ;
7
+ use crate :: ItemKey ;
7
8
8
9
use std:: collections:: TryReserveError ;
9
10
use std:: fmt:: { Debug , Display , Formatter } ;
@@ -83,7 +84,11 @@ pub enum Id3v2ErrorKind {
83
84
84
85
// Frame
85
86
/// Arises when a frame ID contains invalid characters (must be within `'A'..'Z'` or `'0'..'9'`)
86
- BadFrameId ,
87
+ /// or if the ID is too short/long.
88
+ BadFrameId ( Vec < u8 > ) ,
89
+ /// Arises when no frame ID is available in the ID3v2 specification for an item key
90
+ /// and the associated value type.
91
+ UnsupportedFrameId ( ItemKey ) ,
87
92
/// Arises when a frame doesn't have enough data
88
93
BadFrameLength ,
89
94
/// Arises when reading/writing a compressed or encrypted frame with no data length indicator
@@ -133,7 +138,10 @@ impl Display for Id3v2ErrorKind {
133
138
} ,
134
139
135
140
// Frame
136
- Self :: BadFrameId => write ! ( f, "Failed to parse a frame ID" ) ,
141
+ Self :: BadFrameId ( frame_id) => write ! ( f, "Failed to parse a frame ID: 0x{frame_id:x?}" ) ,
142
+ Self :: UnsupportedFrameId ( item_key) => {
143
+ write ! ( f, "Unsupported frame ID for item key {item_key:?}" )
144
+ } ,
137
145
Self :: BadFrameLength => write ! (
138
146
f,
139
147
"Frame isn't long enough to extract the necessary information"
Original file line number Diff line number Diff line change 24
24
return Ok ( None ) ;
25
25
}
26
26
27
- let id_str = std:: str:: from_utf8 ( & frame_header[ ..3 ] )
28
- . map_err ( |_| Id3v2Error :: new ( Id3v2ErrorKind :: BadFrameId ) ) ?;
27
+ let frame_id = & frame_header[ ..3 ] ;
28
+ let id_str = std:: str:: from_utf8 ( frame_id)
29
+ . map_err ( |_| Id3v2Error :: new ( Id3v2ErrorKind :: BadFrameId ( frame_id. to_vec ( ) ) ) ) ?;
29
30
let id = upgrade_v2 ( id_str) . map_or_else ( || Cow :: Owned ( id_str. to_owned ( ) ) , Cow :: Borrowed ) ;
30
31
31
32
let frame_id = FrameId :: new_cow ( id) ?;
63
64
frame_id_end = 3 ;
64
65
}
65
66
67
+ let frame_id = & frame_header[ ..frame_id_end] ;
66
68
let id_str = std:: str:: from_utf8 ( & frame_header[ ..frame_id_end] )
67
- . map_err ( |_| Id3v2Error :: new ( Id3v2ErrorKind :: BadFrameId ) ) ?;
69
+ . map_err ( |_| Id3v2Error :: new ( Id3v2ErrorKind :: BadFrameId ( frame_id . to_vec ( ) ) ) ) ?;
68
70
69
71
let mut size = u32:: from_be_bytes ( [
70
72
frame_header[ 4 ] ,
Original file line number Diff line number Diff line change @@ -38,7 +38,9 @@ impl<'a> FrameId<'a> {
38
38
match id. len ( ) {
39
39
3 => Ok ( FrameId :: Outdated ( id) ) ,
40
40
4 => Ok ( FrameId :: Valid ( id) ) ,
41
- _ => Err ( Id3v2Error :: new ( Id3v2ErrorKind :: BadFrameId ) . into ( ) ) ,
41
+ _ => Err (
42
+ Id3v2Error :: new ( Id3v2ErrorKind :: BadFrameId ( id. into_owned ( ) . into_bytes ( ) ) ) . into ( ) ,
43
+ ) ,
42
44
}
43
45
}
44
46
@@ -52,7 +54,10 @@ impl<'a> FrameId<'a> {
52
54
pub ( super ) fn verify_id ( id_str : & str ) -> Result < ( ) > {
53
55
for c in id_str. chars ( ) {
54
56
if !c. is_ascii_uppercase ( ) && !c. is_ascii_digit ( ) {
55
- return Err ( Id3v2Error :: new ( Id3v2ErrorKind :: BadFrameId ) . into ( ) ) ;
57
+ return Err ( Id3v2Error :: new ( Id3v2ErrorKind :: BadFrameId (
58
+ id_str. as_bytes ( ) . to_vec ( ) ,
59
+ ) )
60
+ . into ( ) ) ;
56
61
}
57
62
}
58
63
@@ -93,7 +98,7 @@ impl<'a> TryFrom<&'a ItemKey> for FrameId<'a> {
93
98
}
94
99
}
95
100
96
- Err ( Id3v2Error :: new ( Id3v2ErrorKind :: BadFrameId ) . into ( ) )
101
+ Err ( Id3v2Error :: new ( Id3v2ErrorKind :: UnsupportedFrameId ( k . clone ( ) ) ) . into ( ) )
97
102
} ,
98
103
}
99
104
}
Original file line number Diff line number Diff line change @@ -112,7 +112,12 @@ impl<'a> Frame<'a> {
112
112
None => id,
113
113
Some ( upgraded) => Cow :: Borrowed ( upgraded) ,
114
114
} ,
115
- _ => return Err ( Id3v2Error :: new ( Id3v2ErrorKind :: BadFrameId ) . into ( ) ) ,
115
+ _ => {
116
+ return Err ( Id3v2Error :: new ( Id3v2ErrorKind :: BadFrameId (
117
+ id. into_owned ( ) . into_bytes ( ) ,
118
+ ) )
119
+ . into ( ) )
120
+ } ,
116
121
} ;
117
122
118
123
let id = FrameId :: new_cow ( id_upgraded) ?;
@@ -504,8 +509,12 @@ impl<'a> TryFrom<&'a TagItem> for FrameRef<'a> {
504
509
frame_id = id;
505
510
} ,
506
511
Err ( _) => {
507
- let Some ( desc) = tag_item. key ( ) . map_key ( TagType :: Id3v2 , true ) else {
508
- return Err ( Id3v2Error :: new ( Id3v2ErrorKind :: BadFrameId ) . into ( ) ) ;
512
+ let item_key = tag_item. key ( ) ;
513
+ let Some ( desc) = item_key. map_key ( TagType :: Id3v2 , true ) else {
514
+ return Err ( Id3v2Error :: new ( Id3v2ErrorKind :: UnsupportedFrameId (
515
+ item_key. clone ( ) ,
516
+ ) )
517
+ . into ( ) ) ;
509
518
} ;
510
519
511
520
match tag_item. value ( ) {
@@ -525,7 +534,12 @@ impl<'a> TryFrom<&'a TagItem> for FrameRef<'a> {
525
534
content : locator. clone ( ) ,
526
535
} )
527
536
} ,
528
- _ => return Err ( Id3v2Error :: new ( Id3v2ErrorKind :: BadFrameId ) . into ( ) ) ,
537
+ _ => {
538
+ return Err ( Id3v2Error :: new ( Id3v2ErrorKind :: UnsupportedFrameId (
539
+ item_key. clone ( ) ,
540
+ ) )
541
+ . into ( ) )
542
+ } ,
529
543
}
530
544
} ,
531
545
}
Original file line number Diff line number Diff line change @@ -533,7 +533,9 @@ impl Accessor for Id3v2Tag {
533
533
fn set_comment ( & mut self , value : String ) {
534
534
let mut value = Some ( value) ;
535
535
self . frames . retain_mut ( |frame| {
536
- let Some ( CommentFrame { content, .. } ) = filter_comment_frame_by_description_mut ( frame, & EMPTY_CONTENT_DESCRIPTOR ) else {
536
+ let Some ( CommentFrame { content, .. } ) =
537
+ filter_comment_frame_by_description_mut ( frame, & EMPTY_CONTENT_DESCRIPTOR )
538
+ else {
537
539
return true ;
538
540
} ;
539
541
if let Some ( value) = value. take ( ) {
@@ -759,7 +761,9 @@ impl SplitTag for Id3v2Tag {
759
761
) => {
760
762
if owner == MUSICBRAINZ_UFID_OWNER {
761
763
let mut identifier = Cursor :: new ( identifier) ;
762
- let Ok ( recording_id) = decode_text ( & mut identifier, TextEncoding :: Latin1 , false ) else {
764
+ let Ok ( recording_id) =
765
+ decode_text ( & mut identifier, TextEncoding :: Latin1 , false )
766
+ else {
763
767
return true ; // Keep frame
764
768
} ;
765
769
tag. items . push ( TagItem :: new (
You can’t perform that action at this time.
0 commit comments