audex 0.2.0

Audio metadata reading and writing library with flexible I/O and easy wrappers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
//! Core tagging interfaces and metadata handling
//!
//! This module defines the fundamental traits and types for working with audio metadata
//! tags across different file formats. It provides a unified interface that abstracts
//! over format-specific tagging systems.
//!
//! # Tagging System Overview
//!
//! The library uses a layered trait system for tag operations:
//!
//! - **[`Tags`]**: Base trait for key-value tag access (get, set, remove)
//! - **[`Metadata`]**: Extends Tags with file I/O operations (load, save, delete)
//! - **[`MetadataFields`]**: Convenience accessors for common tag fields
//!
//! ## Tags vs Metadata vs MetadataFields
//!
//! ### Tags Trait
//! The fundamental interface for tag manipulation. Provides:
//! - Key-value operations: `get()`, `set()`, `remove()`, `keys()`
//! - Multi-value tag support (most formats support multiple values per key)
//! - Format-agnostic tag access
//!
//! Use this when you need to work with tags on an already-loaded file.
//!
//! ### Metadata Trait
//! Extends Tags with file persistence operations:
//! - `load_from_path()` / `load_from_fileobj()`: Read tags from files
//! - `save_to_path()` / `save_to_fileobj()`: Write tags back to files
//! - `delete_from_path()` / `delete_from_fileobj()`: Remove tags entirely
//!
//! Use this for standalone tag formats like ID3v2, APEv2, or Vorbis Comments.
//!
//! ### MetadataFields Trait
//! Convenience accessors for common fields:
//! - `artist()`, `album()`, `title()`, `track_number()`, `date()`, `genre()`
//! - Automatic field name mapping (handles format-specific variations)
//! - Simpler API for common operations
//!
//! Use this when you just need standard music tags.
//!
//! # Common Tag Operations
//!
//! ## Reading Tags
//!
//! ```no_run
//! use audex::File;
//! use audex::FileType;
//!
//! # fn main() -> Result<(), audex::AudexError> {
//! let file = File::load("music.mp3")?;
//!
//! // Get a single tag value
//! if let Some(artists) = file.get("artist") {
//!     for artist in artists {
//!         println!("Artist: {}", artist);
//!     }
//! }
//!
//! // Get the first value only
//! if let Some(title) = file.get_first("title") {
//!     println!("Title: {}", title);
//! }
//!
//! // List all tags
//! for key in file.keys() {
//!     println!("Tag: {}", key);
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## Writing Tags
//!
//! ```no_run
//! use audex::File;
//! use audex::FileType;
//!
//! # fn main() -> Result<(), audex::AudexError> {
//! let mut file = File::load("music.mp3")?;
//!
//! // Set single value
//! file.set("artist", vec!["Artist Name".to_string()])?;
//!
//! // Set multiple values (for formats that support it)
//! file.set("artist", vec![
//!     "First Artist".to_string(),
//!     "Second Artist".to_string(),
//! ])?;
//!
//! // Remove a tag
//! file.remove("comment")?;
//!
//! // Save changes
//! file.save()?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Multi-Value Tags
//!
//! Many formats support multiple values for the same tag key:
//!
//! ```no_run
//! use audex::File;
//! use audex::FileType;
//!
//! # fn main() -> Result<(), audex::AudexError> {
//! let mut file = File::load("music.flac")?;
//!
//! // FLAC/Vorbis supports multiple artists
//! file.set("artist", vec![
//!     "Primary Artist".to_string(),
//!     "Featured Artist".to_string(),
//! ])?;
//!
//! // Get all values
//! if let Some(artists) = file.get("artist") {
//!     println!("Artists: {}", artists.len());
//!     for artist in artists {
//!         println!("  - {}", artist);
//!     }
//! }
//!
//! file.save()?;
//! # Ok(())
//! # }
//! ```
//!
//! # Format-Specific Considerations
//!
//! Different formats have different tagging capabilities:
//!
//! ## ID3v2 (MP3)
//! - Frame-based structure
//! - Most frames support single values
//! - Some frames (like TXXX) support multiple instances
//! - Binary data support (e.g., album art)
//!
//! ## Vorbis Comments (FLAC, Ogg Vorbis, Ogg Opus)
//! - Simple key=value pairs
//! - Native multi-value support
//! - Case-insensitive keys (by convention)
//! - Text-only (binary data uses base64 encoding)
//!
//! ## iTunes-style Tags (MP4/M4A)
//! - Atom-based structure
//! - Predefined atoms for common tags
//! - Custom atoms via "----" freeform boxes
//!
//! ## APEv2 (APE, WavPack, Musepack)
//! - Key-value items
//! - Supports both text and binary values
//! - Case-insensitive keys (original case preserved)
//!
//! # Padding Information
//!
//! Some formats support padding to minimize file rewrites. See [`PaddingInfo`]
//! for details on how padding is calculated and managed during save operations.

use crate::util::{AnyFileThing, loadfile_read, loadfile_write};
use crate::{AudexError, Result};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::collections::HashMap;
use std::fmt;
use std::io::{Cursor, ErrorKind, Read, Seek, SeekFrom, Write};
use std::path::Path;

/// Padding information for optimizing tag write operations
///
/// Many audio formats support padding - extra empty space reserved after metadata
/// tags. Padding allows tag updates without rewriting the entire file, which is
/// much faster for large audio files.
///
/// This struct calculates optimal padding amounts based on:
/// - Current available padding
/// - Size of trailing data (audio stream)
/// - Heuristics for minimizing future rewrites
///
/// # How Padding Works
///
/// When tags are written:
/// 1. If new tags fit in existing padding → fast update (no file rewrite)
/// 2. If new tags are larger → entire file must be shifted (slow)
/// 3. If new tags are much smaller → excessive wasted space
///
/// The default padding algorithm balances these concerns.
///
/// # Padding Calculation
///
/// The `get_default_padding()` method implements a smart algorithm:
/// - **High threshold**: 10 KiB + 1% of trailing data
/// - **Low threshold**: 1 KiB + 0.1% of trailing data
///
/// If current padding exceeds the high threshold, it's reduced to the low threshold.
/// If current padding is non-negative and within the high threshold, it's kept as-is.
/// If current padding is insufficient (negative), padding is added to reach the low threshold.
///
/// # Examples
///
/// ## Using Default Padding
///
/// ```no_run
/// use audex::tags::PaddingInfo;
///
/// // Simulate a file with 100 bytes of current padding
/// // and 10 MB of trailing audio data
/// let info = PaddingInfo {
///     padding: 100,
///     size: 10_000_000,
/// };
///
/// // Get recommended padding amount
/// let recommended = info.get_default_padding();
/// println!("Recommended padding: {} bytes", recommended);
/// // Output: 100 bytes (padding is below the high threshold, so it is kept as-is)
/// ```
///
/// ## Custom Padding Strategy
///
/// ```no_run
/// use audex::tags::PaddingInfo;
///
/// let info = PaddingInfo {
///     padding: -5000,  // Need 5000 more bytes
///     size: 50_000_000,
/// };
///
/// // Use a custom strategy (e.g., always 64 KiB)
/// let custom_padding = 65536;
///
/// // Or use the default algorithm
/// let default_padding = info.get_default_padding();
///
/// println!("Custom: {} bytes, Default: {} bytes",
///          custom_padding, default_padding);
/// ```
///
/// # Format Support
///
/// Padding is commonly used in:
/// - **ID3v2**: Padding bytes after tag frames
/// - **FLAC**: Padding metadata blocks
/// - **Ogg**: Not typically used (packets are page-based)
/// - **MP4**: Usually not used (atoms are size-prefixed)
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PaddingInfo {
    /// Current padding available (in bytes)
    ///
    /// - **Positive**: Bytes of available padding space
    /// - **Zero**: No padding (tags fit exactly)
    /// - **Negative**: Additional bytes needed (current padding insufficient)
    pub padding: i64,

    /// Size of data following the tag region (in bytes)
    ///
    /// Typically the size of the audio stream. Used to calculate
    /// percentage-based padding amounts.
    pub size: i64,
}

impl PaddingInfo {
    /// Create a new PaddingInfo with the given padding and trailing data size
    pub fn new(padding: i64, size: i64) -> Self {
        Self { padding, size }
    }

    /// The default implementation which tries to select a reasonable
    /// amount of padding and which might change in future versions.
    ///
    /// Returns the amount of padding after saving
    pub fn get_default_padding(&self) -> i64 {
        // Clamp size to non-negative — a negative trailing data size is
        // meaningless and would produce nonsensical padding calculations.
        let clamped_size = self.size.max(0);
        let high = (1024i64 * 10).saturating_add(clamped_size / 100); // 10 KiB + 1% of trailing data
        let low = 1024i64.saturating_add(clamped_size / 1000); // 1 KiB + 0.1% of trailing data

        if self.padding >= 0 {
            // enough padding left
            if self.padding > high {
                // padding too large, reduce
                low
            } else {
                // just use existing padding as is
                self.padding
            }
        } else {
            // not enough padding, add some
            low
        }
    }

    /// Get padding using a user-provided function, falling back to default calculation.
    ///
    /// This method is an internal implementation detail used by format implementations.
    /// It may change without notice.
    pub(crate) fn get_padding_with<F>(&self, user_func: Option<F>) -> i64
    where
        F: FnOnce(&PaddingInfo) -> i64,
    {
        match user_func {
            Some(func) => func(self),
            None => self.get_default_padding(),
        }
    }
}

impl fmt::Display for PaddingInfo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "<PaddingInfo size={} padding={}>",
            self.size, self.padding
        )
    }
}

/// Base trait for key-value tag access
///
/// This trait defines the fundamental interface for reading and writing metadata tags.
/// It provides a key-value API where each key can map to multiple string values,
/// allowing formats to naturally express multi-value tags (e.g., multiple artists).
///
/// # Design Philosophy
///
/// - **Keys are strings**: Tag names are format-specific strings (e.g., "ARTIST", "TPE1", "©ART")
/// - **Values are string lists**: Each key maps to a vector of strings, supporting multi-value tags
/// - **Case sensitivity**: Depends on the format (ID3 is case-sensitive, Vorbis is not)
/// - **Empty values**: Setting an empty vector typically removes the key
///
/// # Examples
///
/// ## Basic Tag Operations
///
/// Note: These examples use [`crate::File`] which returns a
/// `DynamicFileType`. The `DynamicFileType` wrapper provides
/// convenience methods that mirror this trait but return `Result<()>` instead
/// of `()`. The raw `Tags` trait methods (`set`, `remove`, `set_single`) do
/// not return `Result`.
///
/// ```no_run
/// use audex::File;
///
/// # fn main() -> Result<(), audex::AudexError> {
/// let mut file = File::load("song.mp3")?;
///
/// // Set a single-value tag (DynamicFileType::set returns Result<()>)
/// file.set("artist", vec!["Artist Name".to_string()])?;
///
/// // Set a multi-value tag
/// file.set("genre", vec!["Rock".to_string(), "Alternative".to_string()])?;
///
/// // Get tag values (DynamicFileType::get returns Option<Vec<String>>)
/// if let Some(artists) = file.get("artist") {
///     println!("Found {} artist(s)", artists.len());
/// }
///
/// // Remove a tag
/// file.remove("comment")?;
///
/// // Check if tag exists
/// if file.contains_key("album") {
///     println!("Album tag is present");
/// }
///
/// file.save()?;
/// # Ok(())
/// # }
/// ```
///
/// ## Direct Tags Trait Usage
///
/// When working with a concrete `Tags` implementor directly:
///
/// ```rust
/// use audex::tags::{BasicTags, Tags};
///
/// let mut tags = BasicTags::new();
///
/// // Tags::set() returns () (no Result)
/// tags.set("artist", vec!["Artist Name".to_string()]);
/// tags.set("genre", vec!["Rock".to_string()]);
///
/// // Tags::get() returns Option<&[String]> (borrowed slice)
/// if let Some(artists) = tags.get("artist") {
///     println!("Found {} artist(s)", artists.len());
/// }
///
/// // Tags::remove() returns () (no Result)
/// tags.remove("genre");
/// ```
pub trait Tags {
    /// Get all values for a tag key
    ///
    /// Returns a borrowed slice of strings if the key exists, or `None` if not found.
    ///
    /// Note: `DynamicFileType::get` returns `Option<Vec<String>>` (owned)
    /// instead of `Option<&[String]>` (borrowed) due to dynamic dispatch.
    ///
    /// # Arguments
    /// * `key` - The tag key to look up (format-specific, e.g., "ARTIST", "artist")
    ///
    /// # Returns
    /// * `Some(&[String])` - Slice of all values for this key
    /// * `None` - Key does not exist
    ///
    /// # Examples
    ///
    /// ```rust
    /// use audex::tags::{BasicTags, Tags};
    ///
    /// let mut tags = BasicTags::new();
    /// tags.set("artist", vec!["Artist Name".to_string()]);
    ///
    /// if let Some(values) = tags.get("artist") {
    ///     for value in values {
    ///         println!("Artist: {}", value);
    ///     }
    /// }
    /// ```
    fn get(&self, key: &str) -> Option<&[String]>;

    /// Set all values for a tag key, replacing any existing values
    ///
    /// If values is empty, this typically removes the key entirely.
    /// Some formats may have restrictions on multi-value tags.
    ///
    /// Note: This method returns `()`. The `DynamicFileType::set`
    /// wrapper returns `Result<()>` instead, adding error handling for the
    /// dynamic dispatch layer.
    ///
    /// # Arguments
    /// * `key` - The tag key to set
    /// * `values` - Vector of string values (empty to remove)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use audex::tags::{BasicTags, Tags};
    ///
    /// let mut tags = BasicTags::new();
    ///
    /// // Set single value
    /// tags.set("artist", vec!["New Artist".to_string()]);
    ///
    /// // Set multiple values
    /// tags.set("genre", vec!["Rock".to_string(), "Alternative".to_string()]);
    ///
    /// // Remove by setting empty
    /// tags.set("comment", vec![]);
    /// ```
    fn set(&mut self, key: &str, values: Vec<String>);

    /// Remove a tag key and all its values
    ///
    /// After this operation, the key will no longer exist in the tag collection.
    ///
    /// Note: This method returns `()`. The `DynamicFileType::remove`
    /// wrapper returns `Result<()>` instead.
    ///
    /// # Arguments
    /// * `key` - The tag key to remove
    ///
    /// # Examples
    ///
    /// ```rust
    /// use audex::tags::{BasicTags, Tags};
    ///
    /// let mut tags = BasicTags::new();
    /// tags.set("comment", vec!["test".to_string()]);
    /// tags.set("encoded_by", vec!["encoder".to_string()]);
    ///
    /// // Remove unwanted tags
    /// tags.remove("comment");
    /// tags.remove("encoded_by");
    /// ```
    fn remove(&mut self, key: &str);

    /// Get a vector of all tag keys present
    ///
    /// The order of keys is typically undefined. Use this to iterate over
    /// all available tags or check what tags are present.
    ///
    /// # Returns
    /// Vector of all tag key names
    ///
    /// # Examples
    ///
    /// ```rust
    /// use audex::tags::{BasicTags, Tags};
    ///
    /// let mut tags = BasicTags::new();
    /// tags.set("artist", vec!["Test".to_string()]);
    /// tags.set("title", vec!["Song".to_string()]);
    ///
    /// println!("Available tags:");
    /// for key in tags.keys() {
    ///     println!("  - {}", key);
    /// }
    /// ```
    fn keys(&self) -> Vec<String>;

    /// Check if a tag key exists
    ///
    /// # Arguments
    /// * `key` - The tag key to check
    ///
    /// # Returns
    /// `true` if the key exists, `false` otherwise
    ///
    /// # Examples
    ///
    /// ```rust
    /// use audex::tags::{BasicTags, Tags};
    ///
    /// let mut tags = BasicTags::new();
    /// tags.set("artist", vec!["Test".to_string()]);
    ///
    /// if tags.contains_key("artist") {
    ///     println!("Artist tag is present");
    /// }
    /// ```
    fn contains_key(&self, key: &str) -> bool {
        self.get(key).is_some()
    }

    /// Get the first value for a key
    ///
    /// This is a convenience method for when you only need one value
    /// and don't care about additional values.
    ///
    /// Note: This returns `Option<&String>` (borrowed). The
    /// `DynamicFileType::get_first` wrapper returns
    /// `Option<String>` (owned) instead.
    ///
    /// # Arguments
    /// * `key` - The tag key to look up
    ///
    /// # Returns
    /// * `Some(&String)` - Reference to the first value
    /// * `None` - Key does not exist or has no values
    ///
    /// # Examples
    ///
    /// ```rust
    /// use audex::tags::{BasicTags, Tags};
    ///
    /// let mut tags = BasicTags::new();
    /// tags.set("title", vec!["My Song".to_string()]);
    ///
    /// if let Some(title) = tags.get_first("title") {
    ///     println!("Title: {}", title);
    /// }
    /// ```
    fn get_first(&self, key: &str) -> Option<&String> {
        self.get(key)?.first()
    }

    /// Set a single value for a key
    ///
    /// Convenience method equivalent to `set(key, vec![value])`.
    /// Returns `()` like `set()`. The `DynamicFileType::set_single`
    /// wrapper returns `Result<()>` instead.
    ///
    /// # Arguments
    /// * `key` - The tag key to set
    /// * `value` - The single value to set
    ///
    /// # Examples
    ///
    /// ```rust
    /// use audex::tags::{BasicTags, Tags};
    ///
    /// let mut tags = BasicTags::new();
    ///
    /// // Simpler than set(key, vec![...])
    /// tags.set_single("title", "Song Title".to_string());
    /// ```
    fn set_single(&mut self, key: &str, value: String) {
        self.set(key, vec![value]);
    }

    /// Get all values from all tags as a list
    ///
    /// Returns a vector containing the value vectors for each key.
    /// The order corresponds to the order returned by `keys()`.
    ///
    /// # Returns
    /// Vector of value vectors
    ///
    /// # Examples
    ///
    /// ```rust
    /// use audex::tags::{BasicTags, Tags};
    ///
    /// let mut tags = BasicTags::new();
    /// tags.set("artist", vec!["Test".to_string()]);
    ///
    /// let all_values = tags.values();
    /// println!("Total tag groups: {}", all_values.len());
    /// ```
    fn values(&self) -> Vec<Vec<String>> {
        let keys = self.keys();
        keys.iter()
            .filter_map(|k| self.get(k).map(|v| v.to_vec()))
            .collect()
    }

    /// Get all key-value pairs as tuples
    ///
    /// Returns a vector of (key, values) tuples representing all tags.
    /// Useful for iterating over the entire tag collection.
    ///
    /// # Returns
    /// Vector of `(String, Vec<String>)` tuples
    ///
    /// # Examples
    ///
    /// ```rust
    /// use audex::tags::{BasicTags, Tags};
    ///
    /// let mut tags = BasicTags::new();
    /// tags.set("artist", vec!["Test".to_string()]);
    ///
    /// for (key, values) in tags.items() {
    ///     println!("{}: {:?}", key, values);
    /// }
    /// ```
    fn items(&self) -> Vec<(String, Vec<String>)> {
        let keys = self.keys();
        keys.iter()
            .filter_map(|k| self.get(k).map(|v| (k.clone(), v.to_vec())))
            .collect()
    }

    /// Returns tag information as a formatted string
    ///
    /// Creates a human-readable representation of all tags, typically
    /// in the format "key=value" with one tag per line.
    ///
    /// # Returns
    /// Formatted string representation of all tags
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use audex::tags::{BasicTags, Tags};
    ///
    /// let tags = BasicTags::new();
    /// // Print tags in human-readable format
    /// println!("{}", tags.pprint());
    /// ```
    fn pprint(&self) -> String;

    /// Returns the module name for this tag implementation
    ///
    /// Used internally for debugging and introspection.
    ///
    /// # Returns
    /// Static string identifying the module
    fn module_name(&self) -> &'static str {
        "audex"
    }
}

/// Trait for standalone metadata formats with file I/O operations
///
/// This trait extends [`Tags`] with methods to load, save, and delete metadata
/// from files. It's used for standalone tag formats that can exist independently
/// of the audio stream, such as ID3v2, APEv2, or Vorbis Comments.
///
/// # Design
///
/// The trait provides both path-based and file object-based I/O:
/// - `load_from_path()` / `save_to_path()` / `delete_from_path()` - Work with file paths
/// - `load_from_fileobj()` / `save_to_fileobj()` / `delete_from_fileobj()` - Work with open file handles
///
/// This dual API supports both convenience (path-based) and advanced use cases
/// (working with already-open files, custom I/O, testing).
///
/// # Examples
///
/// ## Loading and Saving Standalone Tags
///
/// ```no_run
/// use audex::id3::ID3;
/// use audex::tags::Metadata;
/// use audex::Tags;
///
/// # fn main() -> Result<(), audex::AudexError> {
/// // Load ID3v2 tags from file
/// let mut id3 = ID3::load_from_path("song.mp3")?;
///
/// // Modify tags
/// id3.set("TIT2", vec!["New Title".to_string()]);
/// id3.set("TPE1", vec!["New Artist".to_string()]);
///
/// // Save back to file
/// id3.save()?;
/// # Ok(())
/// # }
/// ```
///
/// ## Deleting All Tags
///
/// ```no_run
/// use audex::id3::ID3;
/// use audex::tags::Metadata;
///
/// # fn main() -> Result<(), audex::AudexError> {
/// // Remove all ID3v2 tags from a file
/// ID3::delete_from_path(Some("song.mp3"))?;
/// # Ok(())
/// # }
/// ```
pub trait Metadata: Tags {
    /// Associated error type for this metadata format
    ///
    /// Each format can define its own error type, which will be
    /// converted to [`AudexError`] when necessary.
    type Error: Into<AudexError>;

    /// Create a new empty metadata instance
    ///
    /// Creates a new, empty tag structure with no tags set.
    /// Useful for creating tags for files that don't have any yet.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use audex::id3::ID3;
    /// use audex::tags::Metadata;
    /// use audex::Tags;
    ///
    /// # fn main() -> Result<(), audex::AudexError> {
    /// // Create new empty ID3v2 tags
    /// let mut id3 = ID3::new();
    /// id3.set("TIT2", vec!["Title".to_string()]);
    /// # Ok(())
    /// # }
    /// ```
    fn new() -> Self
    where
        Self: Sized;

    /// Load metadata from a file path
    ///
    /// Opens the file, reads the metadata, and returns a new instance
    /// containing the loaded tags.
    ///
    /// # Arguments
    /// * `path` - Path to the audio file
    ///
    /// # Returns
    /// * `Ok(Self)` - Successfully loaded metadata
    /// * `Err(AudexError)` - Failed to read or parse metadata
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use audex::id3::ID3;
    /// use audex::tags::Metadata;
    ///
    /// # fn main() -> Result<(), audex::AudexError> {
    /// let id3 = ID3::load_from_path("song.mp3")?;
    /// # Ok(())
    /// # }
    /// ```
    fn load_from_path<P: AsRef<Path>>(path: P) -> Result<Self>
    where
        Self: Sized,
    {
        let mut file_thing = loadfile_read(path)?;
        Self::load_from_fileobj(&mut file_thing)
    }

    /// Load metadata from an open file handle
    ///
    /// Reads metadata from an already-opened file or any type that
    /// implements Read + Seek. Useful for custom I/O scenarios.
    ///
    /// # Arguments
    /// * `filething` - Mutable reference to an open file handle
    ///
    /// # Returns
    /// * `Ok(Self)` - Successfully loaded metadata
    /// * `Err(AudexError)` - Failed to read or parse metadata
    fn load_from_fileobj(filething: &mut AnyFileThing) -> Result<Self>
    where
        Self: Sized;

    /// Save metadata to a file path
    ///
    /// Writes the current tag state back to the file. The file must
    /// already exist; this method modifies it in place.
    ///
    /// # Arguments
    /// * `path` - Optional path to the file (Some for path-based, None returns error)
    ///
    /// # Returns
    /// * `Ok(())` - Successfully saved
    /// * `Err(AudexError)` - Failed to write
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use audex::id3::ID3;
    /// use audex::tags::Metadata;
    /// use audex::Tags;
    ///
    /// # fn main() -> Result<(), audex::AudexError> {
    /// let mut id3 = ID3::load_from_path("song.mp3")?;
    /// id3.set("TIT2", vec!["New Title".to_string()]);
    /// id3.save_to_path(Some("song.mp3"))?;
    /// # Ok(())
    /// # }
    /// ```
    fn save_to_path<P: AsRef<Path>>(&self, path: Option<P>) -> Result<()> {
        match path {
            Some(path) => {
                let mut file_thing = loadfile_write(path)?;
                self.save_to_fileobj(&mut file_thing)?;
                file_thing.write_back()
            }
            None => Err(AudexError::InvalidOperation(
                "No file path provided".to_string(),
            )),
        }
    }

    /// Save metadata to an open file handle
    ///
    /// Writes the tag data to an already-opened file or any type
    /// that implements Write + Seek.
    ///
    /// # Arguments
    /// * `filething` - Mutable reference to an open file handle
    ///
    /// # Returns
    /// * `Ok(())` - Successfully saved
    /// * `Err(AudexError)` - Failed to write
    fn save_to_fileobj(&self, filething: &mut AnyFileThing) -> Result<()>;

    /// Remove all metadata from a file
    ///
    /// Deletes all tag data from the file, removing any traces of the
    /// metadata structure. This operation is typically irreversible.
    ///
    /// # Arguments
    /// * `path` - Optional path to the file
    ///
    /// # Returns
    /// * `Ok(())` - Successfully deleted tags
    /// * `Err(AudexError)` - Failed to delete
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use audex::id3::ID3;
    /// use audex::tags::Metadata;
    ///
    /// # fn main() -> Result<(), audex::AudexError> {
    /// // Remove all ID3v2 tags from file
    /// ID3::delete_from_path(Some("song.mp3"))?;
    /// # Ok(())
    /// # }
    /// ```
    fn delete_from_path<P: AsRef<Path>>(path: Option<P>) -> Result<()>
    where
        Self: Sized,
    {
        match path {
            Some(path) => {
                let mut file_thing = loadfile_write(path)?;
                Self::delete_from_fileobj(&mut file_thing)?;
                file_thing.write_back()
            }
            None => Err(AudexError::InvalidOperation(
                "No file path provided".to_string(),
            )),
        }
    }

    /// Remove all metadata from an open file handle
    ///
    /// Deletes tag data from an already-opened file.
    ///
    /// # Arguments
    /// * `filething` - Mutable reference to an open file handle
    ///
    /// # Returns
    /// * `Ok(())` - Successfully deleted tags
    /// * `Err(AudexError)` - Failed to delete
    fn delete_from_fileobj(filething: &mut AnyFileThing) -> Result<()>
    where
        Self: Sized;
}

/// Convenience interface for common metadata fields
///
/// This trait provides a simplified API for accessing the most commonly used
/// tag fields. It automatically handles format-specific field name variations
/// and provides a consistent interface across all audio formats.
///
/// # Field Mapping
///
/// Different formats use different names for the same conceptual field:
/// - **Artist**: "ARTIST" (Vorbis), "TPE1" (ID3v2), "©ART" (MP4)
/// - **Album**: "ALBUM" (Vorbis), "TALB" (ID3v2), "©alb" (MP4)
/// - **Title**: "TITLE" (Vorbis), "TIT2" (ID3v2), "©nam" (MP4)
/// - **Track Number**: "TRACKNUMBER" (Vorbis), "TRCK" (ID3v2), "trkn" (MP4)
/// - **Date**: "DATE" (Vorbis), "TDRC" (ID3v2), "©day" (MP4)
/// - **Genre**: "GENRE" (Vorbis), "TCON" (ID3v2), "©gen" (MP4)
///
/// This trait abstracts these differences, providing a uniform API.
///
/// # Trait Independence
///
/// `MetadataFields` is an independent trait -- it does **not** require [`Tags`]
/// as a supertrait. Types may implement `MetadataFields` alone, `Tags` alone,
/// or both. [`BasicTags`] implements both.
///
/// # Examples
///
/// ## Using MetadataFields Convenience Accessors
///
/// ```rust
/// use audex::tags::{BasicTags, MetadataFields, Tags};
///
/// let mut tags = BasicTags::new();
///
/// // Use field-specific methods for common tags
/// tags.set_artist("Artist Name".to_string());
/// tags.set_album("Album Title".to_string());
/// tags.set_title("Track Title".to_string());
/// tags.set_date("2024".to_string());
/// tags.set_track_number(1);
///
/// // Read back using convenience accessors
/// assert_eq!(tags.artist().map(|s| s.as_str()), Some("Artist Name"));
/// assert_eq!(tags.track_number(), Some(1));
/// ```
pub trait MetadataFields {
    /// Get the artist name
    ///
    /// Returns the primary artist/performer of the track.
    ///
    /// # Returns
    /// * `Some(&String)` - Artist name if present
    /// * `None` - Artist tag not set
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use audex::tags::{BasicTags, MetadataFields};
    /// # fn example() -> Option<String> {
    /// # let tags = BasicTags::new();
    /// if let Some(artist) = tags.artist() {
    ///     println!("Artist: {}", artist);
    /// }
    /// # None
    /// # }
    /// ```
    fn artist(&self) -> Option<&String>;

    /// Set the artist name
    ///
    /// Sets the primary artist/performer for the track.
    ///
    /// # Arguments
    /// * `artist` - Artist name to set
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use audex::tags::{BasicTags, MetadataFields};
    /// # fn example() {
    /// # let mut tags = BasicTags::new();
    /// tags.set_artist("Artist Name".to_string());
    /// # }
    /// ```
    fn set_artist(&mut self, artist: String);

    /// Get the album title
    ///
    /// Returns the album or collection title for the track.
    ///
    /// # Returns
    /// * `Some(&String)` - Album title if present
    /// * `None` - Album tag not set
    fn album(&self) -> Option<&String>;

    /// Set the album title
    ///
    /// Sets the album or collection title for the track.
    ///
    /// # Arguments
    /// * `album` - Album title to set
    fn set_album(&mut self, album: String);

    /// Get the track title
    ///
    /// Returns the title/name of the track itself.
    ///
    /// # Returns
    /// * `Some(&String)` - Track title if present
    /// * `None` - Title tag not set
    fn title(&self) -> Option<&String>;

    /// Set the track title
    ///
    /// Sets the title/name of the track.
    ///
    /// # Arguments
    /// * `title` - Track title to set
    fn set_title(&mut self, title: String);

    /// Get the track number
    ///
    /// Returns the track's position number in the album.
    /// This is typically parsed from a string field (e.g., "3" or "3/12").
    ///
    /// # Returns
    /// * `Some(u32)` - Track number if present and valid
    /// * `None` - Track number not set or not parseable as integer
    fn track_number(&self) -> Option<u32>;

    /// Set the track number
    ///
    /// Sets the track's position number in the album.
    ///
    /// # Arguments
    /// * `track` - Track number to set
    fn set_track_number(&mut self, track: u32);

    /// Get the release date
    ///
    /// Returns the release date, typically in YYYY, YYYY-MM, or YYYY-MM-DD format.
    ///
    /// # Returns
    /// * `Some(&String)` - Date string if present
    /// * `None` - Date tag not set
    fn date(&self) -> Option<&String>;

    /// Set the release date
    ///
    /// Sets the release date. Recommended formats: YYYY, YYYY-MM, or YYYY-MM-DD.
    ///
    /// # Arguments
    /// * `date` - Date string to set
    fn set_date(&mut self, date: String);

    /// Get the genre
    ///
    /// Returns the musical genre classification.
    ///
    /// # Returns
    /// * `Some(&String)` - Genre name if present
    /// * `None` - Genre tag not set
    fn genre(&self) -> Option<&String>;

    /// Set the genre
    ///
    /// Sets the musical genre classification.
    ///
    /// # Arguments
    /// * `genre` - Genre name to set
    fn set_genre(&mut self, genre: String);
}

const BASIC_TAGS_MAGIC: &[u8; 8] = b"ADXBTAGS";

fn write_len_prefixed_string<W: Write>(writer: &mut W, value: &str) -> Result<()> {
    let bytes = value.as_bytes();
    if bytes.len() > u32::MAX as usize {
        return Err(AudexError::InvalidData(
            "String too large for tag serialization".to_string(),
        ));
    }
    writer.write_u32::<LittleEndian>(bytes.len() as u32)?;
    writer.write_all(bytes)?;
    Ok(())
}

/// Maximum byte length for a single string in BasicTags binary format (10 MB).
/// No realistic tag key or value should approach this size.
const MAX_STRING_LENGTH: u32 = 10 * 1024 * 1024;

/// Maximum number of entries (keys) in a BasicTags binary file.
const MAX_ENTRY_COUNT: u32 = 100_000;

/// Maximum number of values per entry in a BasicTags binary file.
const MAX_VALUE_COUNT: u32 = 100_000;

/// Maximum total number of values across all entries combined.
/// Prevents the product of entry_count * value_count from causing
/// excessive memory allocation during deserialization.
const MAX_TOTAL_VALUES: u64 = 1_000_000;
const MAX_TOTAL_STRING_BYTES: u64 = 100 * 1024 * 1024;

fn read_len_prefixed_string_counted<R: Read>(
    reader: &mut R,
    total_bytes: &mut u64,
) -> Result<String> {
    let len = reader.read_u32::<LittleEndian>()?;
    if len > MAX_STRING_LENGTH {
        return Err(AudexError::InvalidData(format!(
            "BasicTags string length {} exceeds {} byte limit",
            len, MAX_STRING_LENGTH
        )));
    }
    *total_bytes = total_bytes.checked_add(len as u64).ok_or_else(|| {
        AudexError::InvalidData("BasicTags string byte count overflow".to_string())
    })?;
    if *total_bytes > MAX_TOTAL_STRING_BYTES {
        return Err(AudexError::InvalidData(format!(
            "BasicTags cumulative string bytes {} exceed {} byte limit",
            *total_bytes, MAX_TOTAL_STRING_BYTES
        )));
    }
    let mut buf = vec![0u8; len as usize];
    reader.read_exact(&mut buf)?;
    String::from_utf8(buf).map_err(|e| {
        AudexError::InvalidData(format!("Invalid UTF-8 data in BasicTags store: {}", e))
    })
}

/// Simple hash-map based tag implementation for formats that support it
///
/// `BasicTags` is the simplest implementation of the [`Tags`], [`Metadata`],
/// and [`MetadataFields`] traits. It stores tags in an in-memory `HashMap<String, Vec<String>>`
/// and serializes to a custom binary format (prefixed with `ADXBTAGS` magic bytes).
///
/// # Examples
///
/// ```rust
/// use audex::tags::{BasicTags, Tags, MetadataFields};
///
/// let mut tags = BasicTags::new();
/// tags.set("artist", vec!["Artist".to_string()]);
/// tags.set_title("My Song".to_string());
///
/// assert_eq!(tags.get("artist"), Some(["Artist".to_string()].as_slice()));
/// assert_eq!(tags.title().map(|s| s.as_str()), Some("My Song"));
/// ```
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BasicTags {
    tags: HashMap<String, Vec<String>>,
}

impl BasicTags {
    /// Create a new empty `BasicTags` instance
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a new `BasicTags` with pre-allocated capacity for the given number of tags
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            tags: HashMap::with_capacity(capacity),
        }
    }
}

impl Tags for BasicTags {
    fn get(&self, key: &str) -> Option<&[String]> {
        self.tags.get(key).map(|v| v.as_slice())
    }

    fn set(&mut self, key: &str, values: Vec<String>) {
        if values.is_empty() {
            self.tags.remove(key);
        } else {
            self.tags.insert(key.to_string(), values);
        }
    }

    fn remove(&mut self, key: &str) {
        self.tags.remove(key);
    }

    fn keys(&self) -> Vec<String> {
        self.tags.keys().cloned().collect()
    }

    fn pprint(&self) -> String {
        let mut result = String::new();
        let mut keys: Vec<_> = self.keys();
        keys.sort();

        for key in keys {
            if let Some(values) = self.get(&key) {
                for value in values {
                    result.push_str(&format!("{}={}\n", key, value));
                }
            }
        }

        result
    }
}

impl Metadata for BasicTags {
    type Error = AudexError;

    fn new() -> Self {
        BasicTags::new()
    }

    fn load_from_fileobj(filething: &mut AnyFileThing) -> Result<Self> {
        filething.seek(SeekFrom::Start(0))?;

        let mut magic = [0u8; BASIC_TAGS_MAGIC.len()];
        match filething.read_exact(&mut magic) {
            Ok(()) => {}
            Err(ref err) if err.kind() == ErrorKind::UnexpectedEof => {
                // Empty file, treat as no tags
                return Ok(BasicTags::new());
            }
            Err(err) => return Err(err.into()),
        }

        if magic != *BASIC_TAGS_MAGIC {
            return Err(AudexError::InvalidData(
                "Invalid BasicTags storage header".to_string(),
            ));
        }

        let mut tags = BasicTags::new();
        let entry_count = filething.read_u32::<LittleEndian>()?;
        if entry_count > MAX_ENTRY_COUNT {
            return Err(AudexError::InvalidData(format!(
                "BasicTags entry count {} exceeds {} limit",
                entry_count, MAX_ENTRY_COUNT
            )));
        }
        // Track cumulative value count to prevent the product of
        // entry_count * value_count from causing excessive allocation
        let mut cumulative_values: u64 = 0;
        let mut cumulative_string_bytes: u64 = 0;

        for _ in 0..entry_count {
            let key = read_len_prefixed_string_counted(filething, &mut cumulative_string_bytes)?;
            let value_count = filething.read_u32::<LittleEndian>()?;
            if value_count > MAX_VALUE_COUNT {
                return Err(AudexError::InvalidData(format!(
                    "BasicTags value count {} exceeds {} limit",
                    value_count, MAX_VALUE_COUNT
                )));
            }
            cumulative_values += value_count as u64;
            if cumulative_values > MAX_TOTAL_VALUES {
                return Err(AudexError::InvalidData(format!(
                    "BasicTags cumulative value count {} exceeds {} limit",
                    cumulative_values, MAX_TOTAL_VALUES
                )));
            }
            // Cap pre-allocation to avoid excessive memory usage from
            // untrusted input. The Vec will grow dynamically if needed.
            let capped_capacity = std::cmp::min(value_count as usize, 256);
            let mut values = Vec::with_capacity(capped_capacity);
            for _ in 0..value_count {
                values.push(read_len_prefixed_string_counted(
                    filething,
                    &mut cumulative_string_bytes,
                )?);
            }
            tags.set(&key, values);
        }

        Ok(tags)
    }

    fn save_to_fileobj(&self, filething: &mut AnyFileThing) -> Result<()> {
        let mut buffer = Cursor::new(Vec::new());
        buffer.write_all(BASIC_TAGS_MAGIC)?;

        // Validate tag count fits in u32 before casting to prevent silent truncation
        if self.tags.len() > u32::MAX as usize {
            return Err(AudexError::InvalidData(format!(
                "Tag count {} exceeds maximum of {}",
                self.tags.len(),
                u32::MAX
            )));
        }
        buffer.write_u32::<LittleEndian>(self.tags.len() as u32)?;

        for (key, values) in &self.tags {
            write_len_prefixed_string(&mut buffer, key)?;
            // Validate value count fits in u32
            if values.len() > u32::MAX as usize {
                return Err(AudexError::InvalidData(format!(
                    "Value count for key '{}' exceeds maximum of {}",
                    key,
                    u32::MAX
                )));
            }
            buffer.write_u32::<LittleEndian>(values.len() as u32)?;
            for value in values {
                write_len_prefixed_string(&mut buffer, value)?;
            }
        }

        let data = buffer.into_inner();
        filething.truncate(0)?;
        filething.seek(SeekFrom::Start(0))?;
        filething.write_all(&data)?;
        filething.flush()?;
        Ok(())
    }

    fn delete_from_fileobj(filething: &mut AnyFileThing) -> Result<()> {
        filething.truncate(0)?;
        filething.seek(SeekFrom::Start(0))?;
        filething.flush()?;
        Ok(())
    }
}

// Key casing convention: setters always store keys in uppercase. Getters check
// both uppercase and lowercase variants for backward compatibility with tags
// that may have been written with lowercase keys by other software.
impl MetadataFields for BasicTags {
    fn artist(&self) -> Option<&String> {
        self.get_first("ARTIST")
            .or_else(|| self.get_first("artist"))
    }

    fn set_artist(&mut self, artist: String) {
        self.set_single("ARTIST", artist);
    }

    fn album(&self) -> Option<&String> {
        self.get_first("ALBUM").or_else(|| self.get_first("album"))
    }

    fn set_album(&mut self, album: String) {
        self.set_single("ALBUM", album);
    }

    fn title(&self) -> Option<&String> {
        self.get_first("TITLE").or_else(|| self.get_first("title"))
    }

    fn set_title(&mut self, title: String) {
        self.set_single("TITLE", title);
    }

    fn track_number(&self) -> Option<u32> {
        self.get_first("TRACKNUMBER")
            .or_else(|| self.get_first("tracknumber"))
            .and_then(|s| s.parse().ok())
    }

    fn set_track_number(&mut self, track: u32) {
        self.set_single("TRACKNUMBER", track.to_string());
    }

    fn date(&self) -> Option<&String> {
        self.get_first("DATE").or_else(|| self.get_first("date"))
    }

    fn set_date(&mut self, date: String) {
        self.set_single("DATE", date);
    }

    fn genre(&self) -> Option<&String> {
        self.get_first("GENRE").or_else(|| self.get_first("genre"))
    }

    fn set_genre(&mut self, genre: String) {
        self.set_single("GENRE", genre);
    }
}