lofty 0.24.0

Audio metadata library
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
use crate::config::WriteOptions;
use crate::error::{LoftyError, Result};
use crate::file::FileType;
use crate::macros::err;
use crate::ogg::picture_storage::OggPictureStorage;
use crate::ogg::write::OGGFormat;
use crate::picture::{Picture, PictureInformation};
use crate::probe::Probe;
use crate::tag::items::Timestamp;
use crate::tag::items::popularimeter::Popularimeter;
use crate::tag::{
	Accessor, ItemKey, ItemValue, MergeTag, SplitTag, Tag, TagExt, TagItem, TagType,
	try_parse_timestamp,
};
use crate::util::flag_item;
use crate::util::io::{FileLike, Length, Truncate};

use crate::ogg::read::valid_vorbis_comments_key;
use lofty_attr::tag;
use std::borrow::Cow;
use std::io::Write;
use std::ops::Deref;

macro_rules! impl_accessor {
	($($name:ident => $key:literal;)+) => {
		paste::paste! {
			$(
				fn $name(&self) -> Option<Cow<'_, str>> {
					self.get($key).map(Cow::Borrowed)
				}

				fn [<set_ $name>](&mut self, value: String) {
					self.insert(String::from($key), value)
				}

				fn [<remove_ $name>](&mut self) {
					let _ = self.remove($key);
				}
			)+
		}
	}
}

/// ## Conversions
///
/// ### To `Tag`
///
/// All items without an [`ItemKey`] mapping will be discarded.
///
/// In order to preserve the vendor string, a required part of the OGG formats, it will simply be inserted as
/// [`ItemKey::EncoderSoftware`], given an item with this key does not already exist.
///
/// ### From `Tag`
///
/// #### Items
///
/// When converting a [`TagItem`], the only conditions are that:
///
/// * It has an [`ItemKey`] mapping
/// * It has a value of [`ItemValue::Text`] or [`ItemValue::Locator`]
///
/// If a [`TagItem`] with the key [`ItemKey::EncoderSoftware`] is available, it will be taken and
/// used for the vendor string.
///
/// #### Pictures
///
/// When converting [`Picture`]s, they will first be passed through [`PictureInformation::from_picture()`].
/// If the information is available, it will be used. Otherwise, the picture will be stored with zeroed out
/// [`PictureInformation`].
#[derive(Default, PartialEq, Eq, Debug, Clone)]
#[tag(
	description = "Vorbis comments",
	supported_formats(Flac, Opus, Speex, Vorbis)
)]
pub struct VorbisComments {
	/// An identifier for the encoding software
	pub(crate) vendor: String,
	/// A collection of key-value pairs
	pub(crate) items: Vec<(String, String)>,
	/// A collection of all pictures
	pub(crate) pictures: Vec<(Picture, PictureInformation)>,
}

impl VorbisComments {
	/// Create a new empty `VorbisComments`
	///
	/// # Examples
	///
	/// ```rust
	/// use lofty::ogg::VorbisComments;
	/// use lofty::tag::TagExt;
	///
	/// let vorbis_comments_tag = VorbisComments::new();
	/// assert!(vorbis_comments_tag.is_empty());
	/// ```
	pub fn new() -> Self {
		Self::default()
	}

	/// Returns the vendor string
	///
	/// ```rust
	/// use lofty::ogg::VorbisComments;
	///
	/// let mut vorbis_comments = VorbisComments::default();
	/// assert!(vorbis_comments.vendor().is_empty());
	///
	/// vorbis_comments.set_vendor(String::from("FooBar"));
	/// assert_eq!(vorbis_comments.vendor(), "FooBar");
	/// ```
	pub fn vendor(&self) -> &str {
		&self.vendor
	}

	/// Sets the vendor string
	///
	/// ```rust
	/// use lofty::ogg::VorbisComments;
	///
	/// let mut vorbis_comments = VorbisComments::default();
	///
	/// vorbis_comments.set_vendor(String::from("FooBar"));
	/// assert_eq!(vorbis_comments.vendor(), "FooBar");
	/// ```
	pub fn set_vendor(&mut self, vendor: String) {
		self.vendor = vendor
	}

	/// Get all items
	///
	/// Returns an [`Iterator`] over the stored key/value pairs.
	///
	/// ```rust
	/// use lofty::ogg::VorbisComments;
	///
	/// let mut vorbis_comments = VorbisComments::default();
	///
	/// vorbis_comments.push(String::from("ARTIST"), String::from("Foo artist"));
	/// vorbis_comments.push(String::from("TITLE"), String::from("Bar title"));
	///
	/// let mut items = vorbis_comments.items();
	///
	/// assert_eq!(items.next(), Some(("ARTIST", "Foo artist")));
	/// assert_eq!(items.next(), Some(("TITLE", "Bar title")));
	/// ```
	pub fn items(&self) -> impl ExactSizeIterator<Item = (&str, &str)> + Clone {
		self.items.iter().map(|(k, v)| (k.as_str(), v.as_str()))
	}

	/// Consume all items
	///
	/// Returns an [`Iterator`] with the stored key/value pairs.
	///
	/// ```rust
	/// use lofty::ogg::VorbisComments;
	/// use lofty::tag::TagExt;
	///
	/// let mut vorbis_comments = VorbisComments::default();
	///
	/// vorbis_comments.push(String::from("ARTIST"), String::from("Foo artist"));
	/// vorbis_comments.push(String::from("TITLE"), String::from("Bar title"));
	///
	/// for (key, value) in vorbis_comments.take_items() {
	/// 	println!("We took field: {key}={value}");
	/// }
	///
	/// // We've taken all the items
	/// assert!(vorbis_comments.is_empty());
	/// ```
	pub fn take_items(&mut self) -> impl ExactSizeIterator<Item = (String, String)> + use<> {
		let items = std::mem::take(&mut self.items);
		items.into_iter()
	}

	/// Gets the first item with `key`
	///
	/// NOTE: There can be multiple items with the same key, this grabs whichever happens to be the first
	///
	/// # Examples
	///
	/// ```rust
	/// use lofty::ogg::VorbisComments;
	///
	/// let mut vorbis_comments = VorbisComments::default();
	///
	/// // Vorbis comments allows multiple fields with the same key, such as artist
	/// vorbis_comments.push(String::from("ARTIST"), String::from("Foo artist"));
	/// vorbis_comments.push(String::from("ARTIST"), String::from("Bar artist"));
	/// vorbis_comments.push(String::from("ARTIST"), String::from("Baz artist"));
	///
	/// let first_artist = vorbis_comments.get("ARTIST").unwrap();
	/// assert_eq!(first_artist, "Foo artist");
	/// ```
	pub fn get(&self, key: &str) -> Option<&str> {
		if !verify_key(key) {
			return None;
		}

		self.items
			.iter()
			.find(|(k, _)| k.eq_ignore_ascii_case(key))
			.map(|(_, v)| v.as_str())
	}

	/// Gets all items with the key
	///
	/// # Examples
	///
	/// ```rust
	/// use lofty::ogg::VorbisComments;
	///
	/// let mut vorbis_comments = VorbisComments::default();
	///
	/// // Vorbis comments allows multiple fields with the same key, such as artist
	/// vorbis_comments.push(String::from("ARTIST"), String::from("Foo artist"));
	/// vorbis_comments.push(String::from("ARTIST"), String::from("Bar artist"));
	/// vorbis_comments.push(String::from("ARTIST"), String::from("Baz artist"));
	///
	/// let all_artists = vorbis_comments.get_all("ARTIST").collect::<Vec<&str>>();
	/// assert_eq!(all_artists, vec!["Foo artist", "Bar artist", "Baz artist"]);
	/// ```
	pub fn get_all<'a>(&'a self, key: &'a str) -> impl Iterator<Item = &'a str> + Clone + 'a {
		self.items
			.iter()
			.filter_map(move |(k, v)| (k.eq_ignore_ascii_case(key)).then_some(v.as_str()))
	}

	/// Inserts an item
	///
	/// This is the same as [`VorbisComments::push`], except it will remove any items with the same key.
	///
	/// NOTE: This will do nothing if the key is invalid. This specification is available [here](https://xiph.org/vorbis/doc/v-comment.html#vectorformat).
	///
	/// # Examples
	///
	/// ```rust
	/// use lofty::ogg::VorbisComments;
	///
	/// let mut tag = VorbisComments::default();
	/// tag.insert(String::from("TITLE"), String::from("Title 1"));
	/// tag.insert(String::from("TITLE"), String::from("Title 2"));
	///
	/// // We only retain the last title inserted
	/// let mut titles = tag.get_all("TITLE");
	/// assert_eq!(titles.next(), Some("Title 2"));
	/// assert_eq!(titles.next(), None);
	/// ```
	pub fn insert(&mut self, key: String, value: String) {
		if !verify_key(&key) {
			return;
		}

		self.items.retain(|(k, _)| !k.eq_ignore_ascii_case(&key));
		self.items.push((key, value))
	}

	/// Appends an item
	///
	/// NOTE: This will do nothing if the key is invalid. This specification is available [here](https://xiph.org/vorbis/doc/v-comment.html#vectorformat).
	///
	/// # Examples
	///
	/// ```rust
	/// use lofty::ogg::VorbisComments;
	///
	/// let mut tag = VorbisComments::default();
	/// tag.push(String::from("TITLE"), String::from("Title 1"));
	/// tag.push(String::from("TITLE"), String::from("Title 2"));
	///
	/// // We retain both titles
	/// let mut titles = tag.get_all("TITLE");
	/// assert_eq!(titles.next(), Some("Title 1"));
	/// assert_eq!(titles.next(), Some("Title 2"));
	/// ```
	pub fn push(&mut self, key: String, value: String) {
		if !verify_key(&key) {
			return;
		}

		self.items.push((key, value))
	}

	/// Removes all items with a key, returning an iterator
	///
	/// # Examples
	///
	/// ```rust
	/// use lofty::ogg::VorbisComments;
	///
	/// let mut tag = VorbisComments::default();
	/// tag.push(String::from("TITLE"), String::from("Title 1"));
	/// tag.push(String::from("TITLE"), String::from("Title 2"));
	///
	/// // Remove both titles
	/// for title in tag.remove("TITLE") {
	/// 	println!("We removed the title: {title}");
	/// }
	/// ```
	pub fn remove<'a>(&'a mut self, key: &str) -> impl Iterator<Item = String> + use<'a> {
		// TODO: drain_filter
		let mut split_idx = 0_usize;

		for read_idx in 0..self.items.len() {
			if self.items[read_idx].0.eq_ignore_ascii_case(key) {
				self.items.swap(split_idx, read_idx);
				split_idx += 1;
			}
		}

		self.items.drain(..split_idx).map(|(_, v)| v)
	}
}

// A case-insensitive field name that may consist of ASCII 0x20 through 0x7D, 0x3D ('=') excluded.
// ASCII 0x41 through 0x5A inclusive (A-Z) is to be considered equivalent to ASCII 0x61 through 0x7A inclusive (a-z).
fn verify_key(key: &str) -> bool {
	if key.is_empty() {
		return false;
	}

	key.bytes()
		.all(|byte| (0x20..=0x7D).contains(&byte) && byte != 0x3D)
}

impl OggPictureStorage for VorbisComments {
	fn pictures(&self) -> &[(Picture, PictureInformation)] {
		&self.pictures
	}
}

impl Accessor for VorbisComments {
	impl_accessor!(
		artist  => "ARTIST";
		title   => "TITLE";
		album   => "ALBUM";
		genre   => "GENRE";
		comment => "COMMENT";
	);

	fn track(&self) -> Option<u32> {
		if let Some(item) = self
			.get("TRACKNUMBER")
			.map_or_else(|| self.get("TRACKNUM"), Some)
		{
			return item.parse::<u32>().ok();
		}

		None
	}

	fn set_track(&mut self, value: u32) {
		self.remove_track();
		self.insert(String::from("TRACKNUMBER"), value.to_string());
	}

	fn remove_track(&mut self) {
		let _ = self.remove("TRACKNUMBER");
		let _ = self.remove("TRACKNUM");
	}

	fn track_total(&self) -> Option<u32> {
		if let Some(item) = self
			.get("TRACKTOTAL")
			.map_or_else(|| self.get("TOTALTRACKS"), Some)
		{
			return item.parse::<u32>().ok();
		}

		None
	}

	fn set_track_total(&mut self, value: u32) {
		self.insert(String::from("TRACKTOTAL"), value.to_string());
		let _ = self.remove("TOTALTRACKS");
	}

	fn remove_track_total(&mut self) {
		let _ = self.remove("TRACKTOTAL");
		let _ = self.remove("TOTALTRACKS");
	}

	fn disk(&self) -> Option<u32> {
		if let Some(item) = self.get("DISCNUMBER") {
			return item.parse::<u32>().ok();
		}

		None
	}

	fn set_disk(&mut self, value: u32) {
		self.insert(String::from("DISCNUMBER"), value.to_string());
	}

	fn remove_disk(&mut self) {
		let _ = self.remove("DISCNUMBER");
	}

	fn disk_total(&self) -> Option<u32> {
		if let Some(item) = self
			.get("DISCTOTAL")
			.map_or_else(|| self.get("TOTALDISCS"), Some)
		{
			return item.parse::<u32>().ok();
		}

		None
	}

	fn set_disk_total(&mut self, value: u32) {
		self.insert(String::from("DISCTOTAL"), value.to_string());
		let _ = self.remove("TOTALDISCS");
	}

	fn remove_disk_total(&mut self) {
		let _ = self.remove("DISCTOTAL");
		let _ = self.remove("TOTALDISCS");
	}

	fn date(&self) -> Option<Timestamp> {
		if let Some(item) = self.get("DATE").or_else(|| self.get("YEAR")) {
			return try_parse_timestamp(item);
		}

		None
	}

	fn set_date(&mut self, value: Timestamp) {
		// Just like the Accessor impl for `Tag`, we remove "YEAR". It's not a standard key for
		// Vorbis Comments, but it appears sometimes in the wild. Just normalize the tag to use "DATE".
		self.insert(String::from("DATE"), value.to_string());
		let _ = self.remove("YEAR");
	}

	fn remove_date(&mut self) {
		let _ = self.remove("DATE");
		let _ = self.remove("YEAR");
	}
}

impl TagExt for VorbisComments {
	type Err = LoftyError;
	type RefKey<'a> = &'a str;

	#[inline]
	fn tag_type(&self) -> TagType {
		TagType::VorbisComments
	}

	fn len(&self) -> usize {
		self.items.len() + self.pictures.len()
	}

	fn contains<'a>(&'a self, key: Self::RefKey<'a>) -> bool {
		self.items
			.iter()
			.any(|(item_key, _)| item_key.eq_ignore_ascii_case(key))
	}

	fn is_empty(&self) -> bool {
		self.items.is_empty() && self.pictures.is_empty()
	}

	/// Writes the tag to a file
	///
	/// # Errors
	///
	/// * Attempting to write the tag to a format that does not support it
	/// * The file does not contain valid packets
	/// * [`PictureInformation::from_picture`]
	/// * [`std::io::Error`]
	fn save_to<F>(
		&self,
		file: &mut F,
		write_options: WriteOptions,
	) -> std::result::Result<(), Self::Err>
	where
		F: FileLike,
		LoftyError: From<<F as Truncate>::Error>,
		LoftyError: From<<F as Length>::Error>,
	{
		VorbisCommentsRef {
			vendor: Cow::from(self.vendor.as_str()),
			items: self.items.iter().map(|(k, v)| (k.as_str(), v.as_str())),
			pictures: self.pictures.iter().map(|(p, i)| (p, *i)),
		}
		.write_to(file, write_options)
	}

	/// Dumps the tag to a writer
	///
	/// This does not include a vendor string, and will thus
	/// not create a usable file.
	///
	/// # Errors
	///
	/// * [`PictureInformation::from_picture`]
	/// * [`std::io::Error`]
	fn dump_to<W: Write>(
		&self,
		writer: &mut W,
		write_options: WriteOptions,
	) -> std::result::Result<(), Self::Err> {
		VorbisCommentsRef {
			vendor: Cow::from(self.vendor.as_str()),
			items: self.items.iter().map(|(k, v)| (k.as_str(), v.as_str())),
			pictures: self.pictures.iter().map(|(p, i)| (p, *i)),
		}
		.dump_to(writer, write_options)
	}

	fn clear(&mut self) {
		self.items.clear();
		self.pictures.clear();
	}
}

#[derive(Debug, Clone, Default)]
pub struct SplitTagRemainder(VorbisComments);

impl From<SplitTagRemainder> for VorbisComments {
	fn from(from: SplitTagRemainder) -> Self {
		from.0
	}
}

impl Deref for SplitTagRemainder {
	type Target = VorbisComments;

	fn deref(&self) -> &Self::Target {
		&self.0
	}
}

impl SplitTag for VorbisComments {
	type Remainder = SplitTagRemainder;

	fn split_tag(mut self) -> (Self::Remainder, Tag) {
		const ITEM_RETAINED: bool = true;
		const ITEM_CONSUMED: bool = false;

		let mut tag = Tag::new(TagType::VorbisComments);

		self.items.retain_mut(|(k, v)| {
			let key;
			match ItemKey::from_key(TagType::VorbisComments, k) {
				Some(k) => key = k,
				// Special case for ratings with associated emails (RATING:foo@example.com). Ratings
				// without emails are handled by `ItemKey::from_key`.
				None if k.starts_with("RATING:") => {
					let Some((_, email)) = k.split_once(':') else {
						return ITEM_RETAINED;
					};

					let Ok(value) = v.parse::<u8>() else {
						log::warn!(
							"Unable to parse popularimeter rating during tag split, retaining"
						);
						return ITEM_RETAINED;
					};

					// There is no play counter in Vorbis Comments
					let play_counter = 0;
					let Some(popm) =
						Popularimeter::mapped(email, TagType::VorbisComments, value, play_counter)
					else {
						log::warn!(
							"Unable to find handler for popularimeter during tag split, retaining"
						);
						return ITEM_RETAINED;
					};

					tag.items.push(TagItem::new(
						ItemKey::Popularimeter,
						ItemValue::Text(popm.to_string()),
					));
					return ITEM_CONSUMED;
				},
				None => return ITEM_RETAINED,
			}

			let v = std::mem::take(v);
			tag.items.push(TagItem::new(key, ItemValue::Text(v)));

			ITEM_CONSUMED // Item consumed
		});

		// We need to preserve the vendor string
		if !tag
			.items
			.iter()
			.any(|i| i.key() == ItemKey::EncoderSoftware)
		{
			tag.items.push(TagItem::new(
				ItemKey::EncoderSoftware,
				// Preserve the original vendor by cloning
				ItemValue::Text(self.vendor.clone()),
			));
		}

		for (pic, _info) in std::mem::take(&mut self.pictures) {
			tag.push_picture(pic)
		}

		(SplitTagRemainder(self), tag)
	}
}

impl MergeTag for SplitTagRemainder {
	type Merged = VorbisComments;

	fn merge_tag(self, mut tag: Tag) -> Self::Merged {
		let Self(mut merged) = self;

		if let Some(TagItem {
			item_value: ItemValue::Text(val),
			..
		}) = tag.take(ItemKey::EncoderSoftware).next()
		{
			merged.vendor = val;
		}

		for item in tag.items {
			let item_key = item.item_key;
			let item_value = item.item_value;

			// Discard binary items, as they are not allowed in Vorbis comments
			let (ItemValue::Text(mut val) | ItemValue::Locator(mut val)) = item_value else {
				continue;
			};

			// Normalize flag items
			if item_key == ItemKey::FlagCompilation {
				let Some(flag) = flag_item(&val) else {
					continue;
				};

				val = u8::from(flag).to_string();
			}

			let mut key;
			match item_key.map_key(TagType::VorbisComments) {
				Some(mapped_key) => key = mapped_key.to_string(),
				None => continue, // No mapping exists, discard the item
			}

			// Special case for generic popularimeters, since emails are part of the field name
			if item_key == ItemKey::Popularimeter {
				let Ok(popm) = Popularimeter::from_str(&val) else {
					log::warn!("Failed to parse popularimeter during tag merge, skipping");
					continue;
				};

				if let Some(email) = popm.email() {
					if !valid_vorbis_comments_key(email.as_bytes()) {
						log::warn!("Popularimeter email contains invalid characters, skipping");
						continue;
					}

					key = format!("{key}:{email}");
				}

				val = popm.mapped_value(TagType::VorbisComments).to_string();
			}

			merged.items.push((key, val));
		}

		for picture in tag.pictures {
			if let Ok(information) = PictureInformation::from_picture(&picture) {
				merged.pictures.push((picture, information))
			}
		}

		merged
	}
}

impl From<VorbisComments> for Tag {
	fn from(input: VorbisComments) -> Self {
		input.split_tag().1
	}
}

impl From<Tag> for VorbisComments {
	fn from(input: Tag) -> Self {
		SplitTagRemainder::default().merge_tag(input)
	}
}

pub(crate) struct VorbisCommentsRef<'a, II, IP>
where
	II: Iterator<Item = (&'a str, &'a str)>,
	IP: Iterator<Item = (&'a Picture, PictureInformation)>,
{
	pub vendor: Cow<'a, str>,
	pub items: II,
	pub pictures: IP,
}

impl<'a, II, IP> VorbisCommentsRef<'a, II, IP>
where
	II: Iterator<Item = (&'a str, &'a str)>,
	IP: Iterator<Item = (&'a Picture, PictureInformation)>,
{
	#[allow(clippy::shadow_unrelated)]
	pub(crate) fn write_to<F>(&mut self, file: &mut F, write_options: WriteOptions) -> Result<()>
	where
		F: FileLike,
		LoftyError: From<<F as Truncate>::Error>,
		LoftyError: From<<F as Length>::Error>,
	{
		let probe = Probe::new(file).guess_file_type()?;
		let f_ty = probe.file_type();

		let file = probe.into_inner();

		let file_type = match f_ty {
			Some(ft) if VorbisComments::SUPPORTED_FORMATS.contains(&ft) => ft,
			_ => err!(UnsupportedTag),
		};

		// FLAC has its own special writing needs :)
		if file_type == FileType::Flac {
			return crate::flac::write::write_to_inner(file, self, write_options);
		}

		let (format, header_packet_count) = OGGFormat::from_filetype(file_type);

		super::write::write(file, self, format, header_packet_count, write_options)
	}

	pub(crate) fn dump_to<W: Write>(
		&mut self,
		writer: &mut W,
		_write_options: WriteOptions,
	) -> Result<()> {
		let metadata_packet = super::write::create_metadata_packet(self, &[], false)?;
		writer.write_all(&metadata_packet)?;
		Ok(())
	}
}

pub(crate) fn create_vorbis_comments_ref(
	tag: &Tag,
) -> (
	&str,
	impl Iterator<Item = (&str, &str)>,
	impl Iterator<Item = (&Picture, PictureInformation)>,
) {
	let vendor = tag.get_string(ItemKey::EncoderSoftware).unwrap_or("");

	let items = tag.items.iter().filter_map(|i| match i.value() {
		ItemValue::Text(val) | ItemValue::Locator(val) => i
			.key()
			.map_key(TagType::VorbisComments)
			.map(|key| (key, val.as_str())),
		_ => None,
	});

	let pictures = tag
		.pictures
		.iter()
		.map(|p| (p, PictureInformation::from_picture(p).unwrap_or_default()));
	(vendor, items, pictures)
}

#[cfg(test)]
mod tests {
	use crate::config::{ParseOptions, ParsingMode, WriteOptions};
	use crate::ogg::{OggPictureStorage, VorbisComments};
	use crate::picture::{MimeType, Picture, PictureType};
	use crate::prelude::*;
	use crate::tag::{ItemValue, Tag, TagItem, TagType};
	use std::io::Cursor;

	fn read_tag(tag: &[u8]) -> VorbisComments {
		let mut reader = std::io::Cursor::new(tag);

		crate::ogg::read::read_comments(
			&mut reader,
			tag.len() as u64,
			ParseOptions::new().parsing_mode(ParsingMode::Strict),
		)
		.unwrap()
	}

	#[test_log::test]
	fn parse_vorbis_comments() {
		let mut expected_tag = VorbisComments::default();

		expected_tag.set_vendor(String::from("Lavf58.76.100"));

		expected_tag.push(String::from("ALBUM"), String::from("Baz album"));
		expected_tag.push(String::from("ARTIST"), String::from("Bar artist"));
		expected_tag.push(String::from("COMMENT"), String::from("Qux comment"));
		expected_tag.push(String::from("DATE"), String::from("1984"));
		expected_tag.push(String::from("GENRE"), String::from("Classical"));
		expected_tag.push(String::from("TITLE"), String::from("Foo title"));
		expected_tag.push(String::from("TRACKNUMBER"), String::from("1"));

		let file_cont = crate::tag::utils::test_utils::read_path("tests/tags/assets/test.vorbis");
		let parsed_tag = read_tag(&file_cont);

		assert_eq!(expected_tag, parsed_tag);
	}

	#[test_log::test]
	fn vorbis_comments_re_read() {
		let file_cont = crate::tag::utils::test_utils::read_path("tests/tags/assets/test.vorbis");
		let mut parsed_tag = read_tag(&file_cont);

		// Create a zero-size vendor for comparison
		parsed_tag.vendor = String::new();

		let mut writer = Vec::new();
		parsed_tag
			.dump_to(&mut writer, WriteOptions::default())
			.unwrap();

		let temp_parsed_tag = read_tag(&writer);

		assert_eq!(parsed_tag, temp_parsed_tag);
	}

	#[test_log::test]
	fn vorbis_comments_to_tag() {
		let tag_bytes = std::fs::read("tests/tags/assets/test.vorbis").unwrap();
		let vorbis_comments = read_tag(&tag_bytes);

		let tag: Tag = vorbis_comments.into();

		crate::tag::utils::test_utils::verify_tag(&tag, true, true);
	}

	#[test_log::test]
	fn tag_to_vorbis_comments() {
		let tag = crate::tag::utils::test_utils::create_tag(TagType::VorbisComments);

		let vorbis_comments: VorbisComments = tag.into();

		assert_eq!(vorbis_comments.get("TITLE"), Some("Foo title"));
		assert_eq!(vorbis_comments.get("ARTIST"), Some("Bar artist"));
		assert_eq!(vorbis_comments.get("ALBUM"), Some("Baz album"));
		assert_eq!(vorbis_comments.get("COMMENT"), Some("Qux comment"));
		assert_eq!(vorbis_comments.get("TRACKNUMBER"), Some("1"));
		assert_eq!(vorbis_comments.get("GENRE"), Some("Classical"));
	}

	#[test_log::test]
	fn multi_value_roundtrip() {
		let mut tag = Tag::new(TagType::VorbisComments);
		tag.insert_text(ItemKey::TrackArtist, "TrackArtist 1".to_owned());
		tag.push(TagItem::new(
			ItemKey::TrackArtist,
			ItemValue::Text("TrackArtist 2".to_owned()),
		));
		tag.insert_text(ItemKey::AlbumArtist, "AlbumArtist 1".to_owned());
		tag.push(TagItem::new(
			ItemKey::AlbumArtist,
			ItemValue::Text("AlbumArtist 2".to_owned()),
		));
		tag.insert_text(ItemKey::TrackTitle, "TrackTitle 1".to_owned());
		tag.push(TagItem::new(
			ItemKey::TrackTitle,
			ItemValue::Text("TrackTitle 2".to_owned()),
		));
		tag.insert_text(ItemKey::AlbumTitle, "AlbumTitle 1".to_owned());
		tag.push(TagItem::new(
			ItemKey::AlbumTitle,
			ItemValue::Text("AlbumTitle 2".to_owned()),
		));
		tag.insert_text(ItemKey::Comment, "Comment 1".to_owned());
		tag.push(TagItem::new(
			ItemKey::Comment,
			ItemValue::Text("Comment 2".to_owned()),
		));
		tag.insert_text(ItemKey::ContentGroup, "ContentGroup 1".to_owned());
		tag.push(TagItem::new(
			ItemKey::ContentGroup,
			ItemValue::Text("ContentGroup 2".to_owned()),
		));
		tag.insert_text(ItemKey::Genre, "Genre 1".to_owned());
		tag.push(TagItem::new(
			ItemKey::Genre,
			ItemValue::Text("Genre 2".to_owned()),
		));
		tag.insert_text(ItemKey::Mood, "Mood 1".to_owned());
		tag.push(TagItem::new(
			ItemKey::Mood,
			ItemValue::Text("Mood 2".to_owned()),
		));
		tag.insert_text(ItemKey::Composer, "Composer 1".to_owned());
		tag.push(TagItem::new(
			ItemKey::Composer,
			ItemValue::Text("Composer 2".to_owned()),
		));
		tag.insert_text(ItemKey::Conductor, "Conductor 1".to_owned());
		tag.push(TagItem::new(
			ItemKey::Conductor,
			ItemValue::Text("Conductor 2".to_owned()),
		));
		// Otherwise the following item would be inserted implicitly
		// during the conversion.
		tag.insert_text(ItemKey::EncoderSoftware, "EncoderSoftware".to_owned());
		assert_eq!(20 + 1, tag.len());

		let mut vorbis_comments1 = VorbisComments::from(tag.clone());

		let (split_remainder, split_tag) = vorbis_comments1.clone().split_tag();
		assert_eq!(0, split_remainder.len());
		assert_eq!(tag.len(), split_tag.len());

		// Merge back into Vorbis Comments for comparison
		let mut vorbis_comments2 = split_remainder.merge_tag(split_tag);
		// Soft before comparison -> unordered comparison
		vorbis_comments1
			.items
			.sort_by(|(lhs_key, lhs_val), (rhs_key, rhs_val)| {
				lhs_key.cmp(rhs_key).then_with(|| lhs_val.cmp(rhs_val))
			});
		vorbis_comments2
			.items
			.sort_by(|(lhs_key, lhs_val), (rhs_key, rhs_val)| {
				lhs_key.cmp(rhs_key).then_with(|| lhs_val.cmp(rhs_val))
			});
		assert_eq!(vorbis_comments1.items, vorbis_comments2.items);
	}

	#[test_log::test]
	fn zero_sized_vorbis_comments() {
		let tag_bytes = std::fs::read("tests/tags/assets/zero.vorbis").unwrap();
		let _ = read_tag(&tag_bytes);
	}

	#[test_log::test]
	fn issue_60() {
		let tag_bytes = std::fs::read("tests/tags/assets/issue_60.vorbis").unwrap();
		let tag = read_tag(&tag_bytes);

		assert_eq!(tag.pictures().len(), 1);
		assert!(tag.items.is_empty());
	}

	#[test_log::test]
	fn initial_key_roundtrip() {
		// Both the primary and alternate key should be mapped to the primary
		// key if stored again. Note: The outcome is undefined if both the
		// primary and alternate key would be stored redundantly in VorbisComments!
		for key in ["INITIALKEY", "KEY"] {
			let mut vorbis_comments = VorbisComments {
				items: vec![(key.to_owned(), "Cmaj".to_owned())],
				..Default::default()
			};
			let mut tag = Tag::from(vorbis_comments);
			assert_eq!(Some("Cmaj"), tag.get_string(ItemKey::InitialKey));
			tag.insert_text(ItemKey::InitialKey, "Cmin".to_owned());
			vorbis_comments = tag.into();
			assert_eq!(Some("Cmin"), vorbis_comments.get("INITIALKEY"));
		}
	}

	#[test_log::test]
	fn skip_reading_cover_art() {
		let p = Picture::unchecked(std::iter::repeat_n(0, 50).collect::<Vec<u8>>())
			.pic_type(PictureType::CoverFront)
			.mime_type(MimeType::Jpeg)
			.build();

		let mut tag = Tag::new(TagType::VorbisComments);
		tag.push_picture(p);

		tag.set_artist(String::from("Foo artist"));

		let mut writer = Vec::new();
		tag.dump_to(&mut writer, WriteOptions::new()).unwrap();

		let mut reader = Cursor::new(&writer);
		let tag = crate::ogg::read::read_comments(
			&mut reader,
			writer.len() as u64,
			ParseOptions::new()
				.parsing_mode(ParsingMode::Strict)
				.read_cover_art(false),
		)
		.unwrap();

		assert_eq!(tag.pictures().len(), 0); // Artist, no picture
		assert!(tag.artist().is_some());
	}

	// case TRACKNUMBER=01/05 disable implicit_conversions
	#[test_log::test]
	fn issue_540_disable_implicit_conversions() {
		let mut comments = VorbisComments::new();
		comments.insert(String::from("TRACKNUMBER"), String::from("01/05"));

		let mut comments_bytes = Vec::new();
		comments
			.dump_to(&mut comments_bytes, WriteOptions::default())
			.unwrap();

		let mut reader = Cursor::new(&comments_bytes);
		let tag = crate::ogg::read::read_comments(
			&mut reader,
			comments_bytes.len() as u64,
			ParseOptions::new()
				.parsing_mode(ParsingMode::Strict)
				.implicit_conversions(false)
				.read_cover_art(false),
		)
		.unwrap();

		assert_eq!(tag.track(), None);
		assert_eq!(tag.track_total(), None);
		assert_eq!(tag.get("TRACKNUMBER"), Some("01/05"));
	}

	// case track number and total with leading 0
	#[test_log::test]
	fn opus_issue_540_leading_0() {
		let mut comments = VorbisComments::new();
		comments.insert(String::from("TRACKNUMBER"), String::from("01"));
		comments.insert(String::from("TRACKTOTAL"), String::from("05"));

		let mut comments_bytes = Vec::new();
		comments
			.dump_to(&mut comments_bytes, WriteOptions::default())
			.unwrap();

		let mut reader = Cursor::new(&comments_bytes);
		let tag = crate::ogg::read::read_comments(
			&mut reader,
			comments_bytes.len() as u64,
			ParseOptions::new()
				.parsing_mode(ParsingMode::Strict)
				.implicit_conversions(false)
				.read_cover_art(false),
		)
		.unwrap();

		assert_eq!(tag.get("TRACKNUMBER"), Some("01"));
		assert_eq!(tag.get("TRACKTOTAL"), Some("05"));
	}

	// case DISCNUMBER=01/05
	#[test_log::test]
	fn opus_issue_543() {
		let mut comments = VorbisComments::new();
		comments.insert(String::from("DISCNUMBER"), String::from("01/05"));

		let mut comments_bytes = Vec::new();
		comments
			.dump_to(&mut comments_bytes, WriteOptions::default())
			.unwrap();

		let mut reader = Cursor::new(&comments_bytes);
		let tag = crate::ogg::read::read_comments(
			&mut reader,
			comments_bytes.len() as u64,
			ParseOptions::new()
				.parsing_mode(ParsingMode::Strict)
				.read_cover_art(false),
		)
		.unwrap();

		assert_eq!(tag.disk(), Some(1));
		assert_eq!(tag.disk_total(), Some(5));
	}

	// case DISCNUMBER=01/05 disable implicit_conversions
	#[test_log::test]
	fn opus_disc_disable_implicit_conversions() {
		let mut comments = VorbisComments::new();
		comments.insert(String::from("DISCNUMBER"), String::from("01/05"));

		let mut comments_bytes = Vec::new();
		comments
			.dump_to(&mut comments_bytes, WriteOptions::default())
			.unwrap();

		let mut reader = Cursor::new(&comments_bytes);
		let tag = crate::ogg::read::read_comments(
			&mut reader,
			comments_bytes.len() as u64,
			ParseOptions::new()
				.parsing_mode(ParsingMode::Strict)
				.implicit_conversions(false)
				.read_cover_art(false),
		)
		.unwrap();

		assert_eq!(tag.disk(), None);
		assert_eq!(tag.disk_total(), None);
		assert_eq!(tag.get("DISCNUMBER"), Some("01/05"));
	}

	// case track number and total with leading 0
	#[test_log::test]
	fn opus_disc_leading_0() {
		let mut comments = VorbisComments::new();
		comments.insert(String::from("DISCNUMBER"), String::from("01"));
		comments.insert(String::from("DISCTOTAL"), String::from("05"));

		let mut comments_bytes = Vec::new();
		comments
			.dump_to(&mut comments_bytes, WriteOptions::default())
			.unwrap();

		let mut reader = Cursor::new(&comments_bytes);
		let tag = crate::ogg::read::read_comments(
			&mut reader,
			comments_bytes.len() as u64,
			ParseOptions::new()
				.parsing_mode(ParsingMode::Strict)
				.implicit_conversions(false)
				.read_cover_art(false),
		)
		.unwrap();

		assert_eq!(tag.get("DISCNUMBER"), Some("01"));
		assert_eq!(tag.get("DISCTOTAL"), Some("05"));
	}
}