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
use crate::tag::TagType;
use std::borrow::Cow;
use std::collections::HashMap;
macro_rules! first_key {
($key:tt $(| $remaining:expr)*) => {
$key
};
}
pub(crate) use first_key;
// This is used to create the key/ItemKey maps
//
// First comes the name of the map.
// Ex:
//
// APE_MAP;
//
// This is followed by the key value pairs separated by `=>`, with the key being the
// format-specific key and the value being the appropriate ItemKey variant.
// Ex. "Artist" => Artist
//
// Some formats have multiple keys that map to the same ItemKey variant, which can be added with '|'.
// The standard key(s) **must** come before any popular non-standard keys.
// Keys should appear in order of popularity.
macro_rules! gen_map {
($(#[$meta:meta])? $NAME:ident; $($($key:literal)|+ => $variant:ident),+) => {
paste::paste! {
$(#[$meta])?
static [<$NAME _INNER>]: once_cell::sync::Lazy<HashMap<&'static str, ItemKey>> = once_cell::sync::Lazy::new(|| {
let mut map = HashMap::new();
$(
$(
map.insert($key, ItemKey::$variant);
)+
)+
map
});
$(#[$meta])?
#[allow(non_camel_case_types)]
struct $NAME;
$(#[$meta])?
impl $NAME {
pub(crate) fn get_item_key(&self, key: &str) -> Option<ItemKey> {
[<$NAME _INNER>].iter().find(|(k, _)| k.eq_ignore_ascii_case(key)).map(|(_, v)| v.clone())
}
pub(crate) fn get_key(&self, item_key: &ItemKey) -> Option<&str> {
match item_key {
$(
ItemKey::$variant => Some(first_key!($($key)|*)),
)+
_ => None
}
}
}
}
}
}
gen_map!(
AIFF_TEXT_MAP;
"NAME" => TrackTitle,
"AUTH" => TrackArtist,
"(c) " => CopyrightMessage,
"COMM" | "ANNO" => Comment
);
gen_map!(
APE_MAP;
"Album" => AlbumTitle,
"DiscSubtitle" => SetSubtitle,
"Grouping" => ContentGroup,
"Title" => TrackTitle,
"Subtitle" => TrackSubtitle,
"WORKTITLE" => Work,
"MOVEMENTNAME" => Movement,
"MOVEMENT" => MovementNumber,
"MOVEMENTTOTAL" => MovementTotal,
"ALBUMSORT" => AlbumTitleSortOrder,
"ALBUMARTISTSORT" => AlbumArtistSortOrder,
"TITLESORT" => TrackTitleSortOrder,
"ARTISTSORT" => TrackArtistSortOrder,
"Album Artist" | "ALBUMARTIST" => AlbumArtist,
"Artist" => TrackArtist,
"Arranger" => Arranger,
"Writer" => Writer,
"Composer" => Composer,
"Conductor" => Conductor,
"Director" => Director,
"Engineer" => Engineer,
"Lyricist" => Lyricist,
"DjMixer" => MixDj,
"Mixer" => MixEngineer,
"Performer" => Performer,
"Producer" => Producer,
"Label" => Label,
"MixArtist" => Remixer,
"Disc" => DiscNumber,
"Disc" => DiscTotal,
"Track" => TrackNumber,
"Track" => TrackTotal,
"Year" => Year,
"ISRC" => Isrc,
"Barcode" => Barcode,
"CatalogNumber" => CatalogNumber,
"Compilation" => FlagCompilation,
"Media" => OriginalMediaType,
"EncodedBy" => EncodedBy,
"REPLAYGAIN_ALBUM_GAIN" => ReplayGainAlbumGain,
"REPLAYGAIN_ALBUM_PEAK" => ReplayGainAlbumPeak,
"REPLAYGAIN_TRACK_GAIN" => ReplayGainTrackGain,
"REPLAYGAIN_TRACK_PEAK" => ReplayGainTrackPeak,
"Genre" => Genre,
"Color" => Color,
"Mood" => Mood,
"Copyright" => CopyrightMessage,
"Comment" => Comment,
"language" => Language,
"Script" => Script,
"Lyrics" => Lyrics,
"MUSICBRAINZ_TRACKID" => MusicBrainzRecordingId,
"MUSICBRAINZ_RELEASETRACKID" => MusicBrainzTrackId,
"MUSICBRAINZ_ALBUMID" => MusicBrainzReleaseId,
"MUSICBRAINZ_RELEASEGROUPID" => MusicBrainzReleaseGroupId,
"MUSICBRAINZ_ARTISTID" => MusicBrainzArtistId,
"MUSICBRAINZ_ALBUMARTISTID" => MusicBrainzReleaseArtistId,
"MUSICBRAINZ_WORKID" => MusicBrainzWorkId
);
gen_map!(
ID3V2_MAP;
"TALB" => AlbumTitle,
"TSST" => SetSubtitle,
"TIT1" => ContentGroup,
"GRP1" => AppleId3v2ContentGroup,
"TIT2" => TrackTitle,
"TIT3" => TrackSubtitle,
"TOAL" => OriginalAlbumTitle,
"TOPE" => OriginalArtist,
"TOLY" => OriginalLyricist,
"TSOA" => AlbumTitleSortOrder,
"TSO2" => AlbumArtistSortOrder,
"TSOT" => TrackTitleSortOrder,
"TSOP" => TrackArtistSortOrder,
"TSOC" => ComposerSortOrder,
"TPE2" => AlbumArtist,
"TPE1" => TrackArtist,
"TEXT" => Writer,
"TCOM" => Composer,
"TPE3" => Conductor,
"DIRECTOR" => Director,
"TIPL" => InvolvedPeople,
"TEXT" => Lyricist,
"TMCL" => MusicianCredits,
"IPRO" => Producer,
"TPUB" => Publisher,
"TPUB" => Label,
"TRSN" => InternetRadioStationName,
"TRSO" => InternetRadioStationOwner,
"TPE4" => Remixer,
"TPOS" => DiscNumber,
"TPOS" => DiscTotal,
"TRCK" => TrackNumber,
"TRCK" => TrackTotal,
"POPM" => Popularimeter,
"TDRC" => RecordingDate,
"TDOR" => OriginalReleaseDate,
"TSRC" => Isrc,
"BARCODE" => Barcode,
"CATALOGNUMBER" => CatalogNumber,
"WORK" => Work, // ID3v2.4: TXXX:WORK (Apple uses TIT1/ContentGroup, see GRP1/AppleId3v2ContentGroup for disambiguation)
"MVNM" => Movement,
"MVIN" => MovementNumber,
"MVIN" => MovementTotal,
"TCMP" => FlagCompilation,
"PCST" => FlagPodcast,
"TFLT" => FileType,
"TOWN" => FileOwner,
"TDTG" => TaggingTime,
"TLEN" => Length,
"TOFN" => OriginalFileName,
"TMED" => OriginalMediaType,
"TENC" => EncodedBy,
"TSSE" => EncoderSoftware,
"TSSE" => EncoderSettings,
"TDEN" => EncodingTime,
"REPLAYGAIN_ALBUM_GAIN" => ReplayGainAlbumGain,
"REPLAYGAIN_ALBUM_PEAK" => ReplayGainAlbumPeak,
"REPLAYGAIN_TRACK_GAIN" => ReplayGainTrackGain,
"REPLAYGAIN_TRACK_PEAK" => ReplayGainTrackPeak,
"WOAF" => AudioFileUrl,
"WOAS" => AudioSourceUrl,
"WCOM" => CommercialInformationUrl,
"WCOP" => CopyrightUrl,
"WOAR" => TrackArtistUrl,
"WORS" => RadioStationUrl,
"WPAY" => PaymentUrl,
"WPUB" => PublisherUrl,
"TCON" => Genre,
"TKEY" => InitialKey,
"COLOR" => Color,
"TMOO" => Mood,
"TBPM" => Bpm,
"TCOP" => CopyrightMessage,
"TDES" => PodcastDescription,
"TCAT" => PodcastSeriesCategory,
"WFED" => PodcastURL,
"TDRL" => PodcastReleaseDate,
"TGID" => PodcastGlobalUniqueID,
"TKWD" => PodcastKeywords,
"COMM" => Comment,
"TLAN" => Language,
"USLT" => Lyrics,
// Mapping of MusicBrainzRecordingId is implemented as a special case
"MusicBrainz Release Track Id" => MusicBrainzTrackId,
"MusicBrainz Album Id" => MusicBrainzReleaseId,
"MusicBrainz Release Group Id" => MusicBrainzReleaseGroupId,
"MusicBrainz Artist Id" => MusicBrainzArtistId,
"MusicBrainz Album Artist Id" => MusicBrainzReleaseArtistId,
"MusicBrainz Work Id" => MusicBrainzWorkId
);
gen_map!(
ILST_MAP;
"\u{a9}alb" => AlbumTitle,
"----:com.apple.iTunes:DISCSUBTITLE" => SetSubtitle,
"tvsh" => ShowName,
"\u{a9}grp" => ContentGroup,
"\u{a9}nam" => TrackTitle,
"----:com.apple.iTunes:SUBTITLE" => TrackSubtitle,
"\u{a9}wrk" => Work,
"\u{a9}mvn" => Movement,
"\u{a9}mvi" => MovementNumber,
"\u{a9}mvc" => MovementTotal,
"soal" => AlbumTitleSortOrder,
"soaa" => AlbumArtistSortOrder,
"sonm" => TrackTitleSortOrder,
"soar" => TrackArtistSortOrder,
"sosn" => ShowNameSortOrder,
"soco" => ComposerSortOrder,
"aART" => AlbumArtist,
"\u{a9}ART" => TrackArtist,
"\u{a9}wrt" => Composer,
"\u{a9}dir" => Director,
"----:com.apple.iTunes:CONDUCTOR" => Conductor,
"----:com.apple.iTunes:ENGINEER" => Engineer,
"----:com.apple.iTunes:LYRICIST" => Lyricist,
"----:com.apple.iTunes:DJMIXER" => MixDj,
"----:com.apple.iTunes:MIXER" => MixEngineer,
"----:com.apple.iTunes:PRODUCER" => Producer,
"----:com.apple.iTunes:LABEL" => Label,
"----:com.apple.iTunes:REMIXER" => Remixer,
"disk" => DiscNumber,
"disk" => DiscTotal,
"trkn" => TrackNumber,
"trkn" => TrackTotal,
"rate" => Popularimeter,
"rtng" => ParentalAdvisory,
"\u{a9}day" => RecordingDate,
"----:com.apple.iTunes:ISRC" => Isrc,
"----:com.apple.iTunes:BARCODE" => Barcode,
"----:com.apple.iTunes:CATALOGNUMBER" => CatalogNumber,
"cpil" => FlagCompilation,
"pcst" => FlagPodcast,
"----:com.apple.iTunes:MEDIA" => OriginalMediaType,
"\u{a9}enc" => EncodedBy,
"\u{a9}too" => EncoderSoftware,
"\u{a9}gen" => Genre,
"----:com.apple.iTunes:COLOR" => Color,
"----:com.apple.iTunes:MOOD" => Mood,
"tmpo" | "----:com.apple.iTunes:BPM" => Bpm, // integer bpm (fourcc atom) vs. precise bpm (freeform atom)
"----:com.apple.iTunes:initialkey" => InitialKey,
"----:com.apple.iTunes:replaygain_album_gain" => ReplayGainAlbumGain,
"----:com.apple.iTunes:replaygain_album_peak" => ReplayGainAlbumPeak,
"----:com.apple.iTunes:replaygain_track_gain" => ReplayGainTrackGain,
"----:com.apple.iTunes:replaygain_track_peak" => ReplayGainTrackPeak,
"cprt" => CopyrightMessage,
"----:com.apple.iTunes:LICENSE" => License,
"ldes" => PodcastDescription,
"catg" => PodcastSeriesCategory,
"purl" => PodcastURL,
"egid" => PodcastGlobalUniqueID,
"keyw" => PodcastKeywords,
"\u{a9}cmt" => Comment,
"desc" => Description,
"----:com.apple.iTunes:LANGUAGE" => Language,
"----:com.apple.iTunes:SCRIPT" => Script,
"\u{a9}lyr" => Lyrics,
"xid " => AppleXid,
"----:com.apple.iTunes:MusicBrainz Track Id" => MusicBrainzRecordingId,
"----:com.apple.iTunes:MusicBrainz Release Track Id" => MusicBrainzTrackId,
"----:com.apple.iTunes:MusicBrainz Album Id" => MusicBrainzReleaseId,
"----:com.apple.iTunes:MusicBrainz Release Group Id" => MusicBrainzReleaseGroupId,
"----:com.apple.iTunes:MusicBrainz Artist Id" => MusicBrainzArtistId,
"----:com.apple.iTunes:MusicBrainz Album Artist Id" => MusicBrainzReleaseArtistId,
"----:com.apple.iTunes:MusicBrainz Work Id" => MusicBrainzWorkId
);
gen_map!(
RIFF_INFO_MAP;
"IPRD" => AlbumTitle,
"INAM" => TrackTitle,
"IART" => TrackArtist,
"IWRI" => Writer,
"IMUS" => Composer,
"IPRO" => Producer,
"IPRT" | "ITRK" => TrackNumber,
"IFRM" => TrackTotal,
"IRTD" => Popularimeter,
"ICRD" => RecordingDate,
"TLEN" => Length,
"ISRF" => OriginalMediaType,
"ITCH" => EncodedBy,
"ISFT" => EncoderSoftware,
"IGNR" => Genre,
"ICOP" => CopyrightMessage,
"ICMT" => Comment,
"ILNG" => Language
);
gen_map!(
VORBIS_MAP;
"ALBUM" => AlbumTitle,
"DISCSUBTITLE" => SetSubtitle,
"GROUPING" => ContentGroup,
"TITLE" => TrackTitle,
"SUBTITLE" => TrackSubtitle,
"WORK" => Work,
"MOVEMENTNAME" => Movement,
"MOVEMENT" => MovementNumber,
"MOVEMENTTOTAL" => MovementTotal,
"ALBUMSORT" => AlbumTitleSortOrder,
"ALBUMARTISTSORT" => AlbumArtistSortOrder,
"TITLESORT" => TrackTitleSortOrder,
"ARTISTSORT" => TrackArtistSortOrder,
"ALBUMARTIST" => AlbumArtist,
"ARTIST" => TrackArtist,
"ARRANGER" => Arranger,
"AUTHOR" | "WRITER" => Writer,
"COMPOSER" => Composer,
"CONDUCTOR" => Conductor,
"DIRECTOR" => Director,
"ENGINEER" => Engineer,
"LYRICIST" => Lyricist,
"DJMIXER" => MixDj,
"MIXER" => MixEngineer,
"PERFORMER" => Performer,
"PRODUCER" => Producer,
"PUBLISHER" => Publisher,
"LABEL" | "ORGANIZATION" => Label,
"REMIXER" | "MIXARTIST" => Remixer,
"DISCNUMBER" => DiscNumber,
"DISCTOTAL" | "TOTALDISCS" => DiscTotal,
"TRACKNUMBER" => TrackNumber,
"TRACKTOTAL" | "TOTALTRACKS" => TrackTotal,
"RATING" => Popularimeter,
"DATE" => RecordingDate,
"YEAR" => Year,
"ORIGINALDATE" => OriginalReleaseDate,
"ISRC" => Isrc,
"BARCODE" => Barcode,
"CATALOGNUMBER" => CatalogNumber,
"COMPILATION" => FlagCompilation,
"MEDIA" => OriginalMediaType,
"ENCODEDBY" | "ENCODED-BY" | "ENCODED_BY" => EncodedBy,
"ENCODER" => EncoderSoftware,
"ENCODING" | "ENCODERSETTINGS" => EncoderSettings,
"REPLAYGAIN_ALBUM_GAIN" => ReplayGainAlbumGain,
"REPLAYGAIN_ALBUM_PEAK" => ReplayGainAlbumPeak,
"REPLAYGAIN_TRACK_GAIN" => ReplayGainTrackGain,
"REPLAYGAIN_TRACK_PEAK" => ReplayGainTrackPeak,
"GENRE" => Genre,
"COLOR" => Color,
"MOOD" => Mood,
"BPM" => Bpm,
// MusicBrainz Picard suggests "KEY" (VirtualDJ, Denon Engine DJ), but "INITIALKEY"
// seems to be more common (Rekordbox, Serato DJ, Traktor DJ, Mixxx).
// <https://picard-docs.musicbrainz.org/en/appendices/tag_mapping.html#initial-key>
// <https://github.com/beetbox/beets/issues/637#issuecomment-39528023>
"INITIALKEY" | "KEY" => InitialKey,
"COPYRIGHT" => CopyrightMessage,
"LICENSE" => License,
"COMMENT" => Comment,
"LANGUAGE" => Language,
"SCRIPT" => Script,
"LYRICS" => Lyrics,
"MUSICBRAINZ_TRACKID" => MusicBrainzRecordingId,
"MUSICBRAINZ_RELEASETRACKID" => MusicBrainzTrackId,
"MUSICBRAINZ_ALBUMID" => MusicBrainzReleaseId,
"MUSICBRAINZ_RELEASEGROUPID" => MusicBrainzReleaseGroupId,
"MUSICBRAINZ_ARTISTID" => MusicBrainzArtistId,
"MUSICBRAINZ_ALBUMARTISTID" => MusicBrainzReleaseArtistId,
"MUSICBRAINZ_WORKID" => MusicBrainzWorkId
);
macro_rules! gen_item_keys {
(
MAPS => [
$(
$(#[$feat:meta])?
[$tag_type:pat, $MAP:ident]
),+
];
KEYS => [
$(
$(#[$variant_meta:meta])*
$variant_ident:ident
),+
$(,)?
]
) => {
#[derive(PartialEq, Clone, Debug, Eq, Hash)]
#[allow(missing_docs)]
#[non_exhaustive]
/// A generic representation of a tag's key
pub enum ItemKey {
$(
$(#[$variant_meta])*
$variant_ident,
)+
/// When a key couldn't be mapped to another variant
///
/// This **will not** allow writing keys that are out of spec (Eg. ID3v2.4 frame IDs **must** be 4 characters)
Unknown(String),
}
impl ItemKey {
/// Map a format specific key to an `ItemKey`
///
/// NOTE: If used with ID3v2, this will only check against the ID3v2.4 keys.
/// If you wish to use a V2 or V3 key, see [`upgrade_v2`](crate::id3::v2::upgrade_v2) and [`upgrade_v3`](crate::id3::v2::upgrade_v3)
pub fn from_key(tag_type: TagType, key: &str) -> Self {
match tag_type {
$(
$(#[$feat])?
$tag_type => $MAP.get_item_key(key).unwrap_or_else(|| Self::Unknown(key.to_string())),
)+
_ => Self::Unknown(key.to_string())
}
}
/// Maps the variant to a format-specific key
///
/// Use `allow_unknown` to include [`ItemKey::Unknown`]. It is up to the caller
/// to determine if the unknown key actually fits the format's specifications.
pub fn map_key(&self, tag_type: TagType, allow_unknown: bool) -> Option<&str> {
match tag_type {
$(
$(#[$feat])?
$tag_type => if let Some(key) = $MAP.get_key(self) {
return Some(key)
},
)+
_ => {}
}
if let ItemKey::Unknown(ref unknown) = self {
if allow_unknown {
return Some(unknown)
}
}
None
}
}
}
}
gen_item_keys!(
MAPS => [
[TagType::AiffText, AIFF_TEXT_MAP],
[TagType::Ape, APE_MAP],
[TagType::Id3v2, ID3V2_MAP],
[TagType::Mp4Ilst, ILST_MAP],
[TagType::RiffInfo, RIFF_INFO_MAP],
[TagType::VorbisComments, VORBIS_MAP]
];
KEYS => [
// Titles
AlbumTitle,
SetSubtitle,
ShowName,
ContentGroup,
TrackTitle,
TrackSubtitle,
// Original names
OriginalAlbumTitle,
OriginalArtist,
OriginalLyricist,
// Sorting
AlbumTitleSortOrder,
AlbumArtistSortOrder,
TrackTitleSortOrder,
TrackArtistSortOrder,
ShowNameSortOrder,
ComposerSortOrder,
// People & Organizations
AlbumArtist,
TrackArtist,
Arranger,
Writer,
Composer,
Conductor,
Director,
Engineer,
InvolvedPeople,
Lyricist,
MixDj,
MixEngineer,
MusicianCredits,
Performer,
Producer,
Publisher,
Label,
InternetRadioStationName,
InternetRadioStationOwner,
Remixer,
// Counts & Indexes
DiscNumber,
DiscTotal,
TrackNumber,
TrackTotal,
Popularimeter,
ParentalAdvisory,
// Dates
RecordingDate,
Year,
OriginalReleaseDate,
// Identifiers
Isrc,
Barcode,
CatalogNumber,
Work,
Movement,
MovementNumber,
MovementTotal,
///////////////////////////////////////////////////////////////
// MusicBrainz Identifiers
/// MusicBrainz Recording ID
///
/// Textual representation of the UUID.
///
/// Reference: <https://picard-docs.musicbrainz.org/en/appendices/tag_mapping.html#id21>
MusicBrainzRecordingId,
/// MusicBrainz Track ID
///
/// Textual representation of the UUID.
///
/// Reference: <https://picard-docs.musicbrainz.org/en/appendices/tag_mapping.html#id24>
MusicBrainzTrackId,
/// MusicBrainz Release ID
///
/// Textual representation of the UUID.
///
/// Reference: <https://picard-docs.musicbrainz.org/en/appendices/tag_mapping.html#id23>
MusicBrainzReleaseId,
/// MusicBrainz Release Group ID
///
/// Textual representation of the UUID.
///
/// Reference: <https://picard-docs.musicbrainz.org/en/appendices/tag_mapping.html#musicbrainz-release-group-id>
MusicBrainzReleaseGroupId,
/// MusicBrainz Artist ID
///
/// Textual representation of the UUID.
///
/// Reference: <https://picard-docs.musicbrainz.org/en/appendices/tag_mapping.html#id17>
MusicBrainzArtistId,
/// MusicBrainz Release Artist ID
///
/// Textual representation of the UUID.
///
/// Reference: <https://picard-docs.musicbrainz.org/en/appendices/tag_mapping.html#id22>
MusicBrainzReleaseArtistId,
/// MusicBrainz Work ID
///
/// Textual representation of the UUID.
///
/// Reference: <https://picard-docs.musicbrainz.org/en/appendices/tag_mapping.html#musicbrainz-work-id>
MusicBrainzWorkId,
///////////////////////////////////////////////////////////////
// Flags
FlagCompilation,
FlagPodcast,
// File Information
FileType,
FileOwner,
TaggingTime,
Length,
OriginalFileName,
OriginalMediaType,
// Encoder information
EncodedBy,
EncoderSoftware,
EncoderSettings,
EncodingTime,
ReplayGainAlbumGain,
ReplayGainAlbumPeak,
ReplayGainTrackGain,
ReplayGainTrackPeak,
// URLs
AudioFileUrl,
AudioSourceUrl,
CommercialInformationUrl,
CopyrightUrl,
TrackArtistUrl,
RadioStationUrl,
PaymentUrl,
PublisherUrl,
// Style
Genre,
InitialKey,
Color,
Mood,
Bpm,
// Legal
CopyrightMessage,
License,
// Podcast
PodcastDescription,
PodcastSeriesCategory,
PodcastURL,
PodcastReleaseDate,
PodcastGlobalUniqueID,
PodcastKeywords,
// Miscellaneous
Comment,
Description,
Language,
Script,
Lyrics,
// Vendor-specific
AppleXid,
AppleId3v2ContentGroup, // GRP1
]
);
/// Represents a tag item's value
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum ItemValue {
/// Any UTF-8 encoded text
Text(String),
/// Any UTF-8 encoded locator of external information
///
/// This is only gets special treatment in `ID3v2` and `APE` tags, being written
/// as a normal string in other tags
Locator(String),
/// Binary information
Binary(Vec<u8>),
}
impl ItemValue {
/// Returns the value if the variant is `Text`
pub fn text(&self) -> Option<&str> {
match self {
Self::Text(ref text) => Some(text),
_ => None,
}
}
/// Returns the value if the variant is `Locator`
pub fn locator(&self) -> Option<&str> {
match self {
Self::Locator(ref locator) => Some(locator),
_ => None,
}
}
/// Returns the value if the variant is `Binary`
pub fn binary(&self) -> Option<&[u8]> {
match self {
Self::Binary(ref bin) => Some(bin),
_ => None,
}
}
/// Consumes the `ItemValue`, returning a `String` if the variant is `Text` or `Locator`
pub fn into_string(self) -> Option<String> {
match self {
Self::Text(s) | Self::Locator(s) => Some(s),
_ => None,
}
}
/// Consumes the `ItemValue`, returning a `Vec<u8>` if the variant is `Binary`
pub fn into_binary(self) -> Option<Vec<u8>> {
match self {
Self::Binary(b) => Some(b),
_ => None,
}
}
/// Check for emptiness
pub fn is_empty(&self) -> bool {
match self {
Self::Binary(binary) => binary.is_empty(),
Self::Locator(locator) => locator.is_empty(),
Self::Text(text) => text.is_empty(),
}
}
}
pub(crate) enum ItemValueRef<'a> {
Text(Cow<'a, str>),
Locator(&'a str),
Binary(&'a [u8]),
}
impl<'a> Into<ItemValueRef<'a>> for &'a ItemValue {
fn into(self) -> ItemValueRef<'a> {
match self {
ItemValue::Text(text) => ItemValueRef::Text(Cow::Borrowed(text)),
ItemValue::Locator(locator) => ItemValueRef::Locator(locator),
ItemValue::Binary(binary) => ItemValueRef::Binary(binary),
}
}
}
/// Represents a tag item (key/value)
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct TagItem {
pub(crate) item_key: ItemKey,
pub(crate) item_value: ItemValue,
}
impl TagItem {
/// Create a new [`TagItem`]
///
/// NOTES:
///
/// * This will check for validity based on the [`TagType`].
/// * If the [`ItemKey`] does not map to a key in the target format, `None` will be returned.
/// * This is unnecessary if you plan on using [`Tag::insert_item`](crate::Tag::insert), as it does validity checks itself.
pub fn new_checked(
tag_type: TagType,
item_key: ItemKey,
item_value: ItemValue,
) -> Option<Self> {
item_key.map_key(tag_type, false).is_some().then_some(Self {
item_key,
item_value,
})
}
/// Create a new [`TagItem`]
#[must_use]
pub const fn new(item_key: ItemKey, item_value: ItemValue) -> Self {
Self {
item_key,
item_value,
}
}
/// Returns a reference to the [`ItemKey`]
pub fn key(&self) -> &ItemKey {
&self.item_key
}
/// Consumes the `TagItem`, returning its [`ItemKey`]
pub fn into_key(self) -> ItemKey {
self.item_key
}
/// Returns a reference to the [`ItemValue`]
pub fn value(&self) -> &ItemValue {
&self.item_value
}
/// Consumes the `TagItem`, returning its [`ItemValue`]
pub fn into_value(self) -> ItemValue {
self.item_value
}
/// Consumes the `TagItem`, returning its [`ItemKey`] and [`ItemValue`]
pub fn consume(self) -> (ItemKey, ItemValue) {
(self.item_key, self.item_value)
}
pub(crate) fn re_map(&self, tag_type: TagType) -> bool {
if tag_type == TagType::Id3v1 {
use crate::id3::v1::constants::VALID_ITEMKEYS;
return VALID_ITEMKEYS.contains(&self.item_key);
}
self.item_key.map_key(tag_type, false).is_some()
}
}