lofty 0.25.3

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
pub(super) mod advisory_rating;
pub(super) mod atom;
pub(super) mod constants;
pub(super) mod data_type;
pub(super) mod error;
pub(super) mod read;
mod r#ref;
pub(crate) mod write;

#[cfg(test)]
mod tests;

use super::AtomIdent;
use crate::config::{WriteOptions, global_options};
use crate::error::{FileEncodingError, TagEncodingError};
use crate::io::VerifiedFile;
use crate::mp4::ilst::atom::AtomDataStorage;
use crate::picture::{Picture, PictureType};
use crate::tag::companion_tag::CompanionTag;
use crate::tag::items::Timestamp;
use crate::tag::items::popularimeter::Popularimeter;
use crate::tag::{
	Accessor, ItemKey, ItemValue, MergeTag, SplitTag, Tag, TagExt, TagItem, TagType, TagWriteExt,
	try_parse_timestamp,
};
use crate::util::flag_item;
use crate::util::io::FileLike;
use advisory_rating::AdvisoryRating;
use atom::{Atom, AtomData};
use data_type::DataType;

use std::borrow::Cow;
use std::io::Write;
use std::ops::Deref;

use lofty_attr::tag;

const ARTIST: AtomIdent<'_> = AtomIdent::Fourcc(*b"\xa9ART");
const TITLE: AtomIdent<'_> = AtomIdent::Fourcc(*b"\xa9nam");
const ALBUM: AtomIdent<'_> = AtomIdent::Fourcc(*b"\xa9alb");
const GENRE: AtomIdent<'_> = AtomIdent::Fourcc(*b"\xa9gen");
const COMMENT: AtomIdent<'_> = AtomIdent::Fourcc(*b"\xa9cmt");
const ADVISORY_RATING: AtomIdent<'_> = AtomIdent::Fourcc(*b"rtng");
const COVR: AtomIdent<'_> = AtomIdent::Fourcc(*b"covr");
const TRACK_NUMBER: AtomIdent<'_> = AtomIdent::Fourcc(*b"trkn");
const DISC_NUMBER: AtomIdent<'_> = AtomIdent::Fourcc(*b"disk");

macro_rules! impl_accessor {
	($($name:ident => $const:ident;)+) => {
		paste::paste! {
			$(
				fn $name(&self) -> Option<Cow<'_, str>> {
					if let Some(atom) = self.get(&$const) {
						if let Some(AtomData::UTF8(val) | AtomData::UTF16(val)) = atom.data().next() {
							return Some(Cow::Borrowed(val));
						}
					}

					None
				}

				fn [<set_ $name>](&mut self, value: String) {
					self.replace_atom(Atom {
						ident: $const,
						data: AtomDataStorage::Single(AtomData::UTF8(value)),
					})
				}

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

macro_rules! impl_flag_accessors {
	($($name:ident ($ident:ident)),+ $(,)?) => {
		$(
			paste::paste! {
				#[doc = "Whether the `" $ident "` flag atom is set"]
				///
				/// # Examples
				///
				/// ```rust
				#[doc = "use lofty::mp4::constants::flags::" $name ";"]
				/// use lofty::mp4::{AtomIdent, Ilst};
				///
				/// let mut ilst = Ilst::new();
				///
				/// // I want to toggle this flag!
				#[doc = "ilst.set_flag(" $name ", true);"]
				///
				#[doc = "assert!(ilst.is_" $name:lower "());"]
				pub fn [<is_ $name:lower>](&self) -> bool {
					self.get_flag(&constants::flags::$name).unwrap_or(false)
				}
			}
		)+
	};
}

/// ## Pictures
///
/// Unlike other formats, ilst does not store a [`PictureType`]. All pictures will have
/// [`PictureType::Other`].
///
/// ## Conversions
///
/// ### To `Tag`
///
/// For an [`Atom`] to be converted it must:
///
/// * Have a value of [`AtomData::UTF8`] and [`AtomData::UTF16`]
/// * **OR** be a `trkn`/`disk` atom
/// * **OR** be a `covr` atom
///
/// Note that all pictures will be [`PictureType::Other`].
///
/// ### From `Tag`
///
/// #### Items
///
/// For a [`TagItem`] to be converted, it must have a value of [`ItemValue::Text`].
///
/// An attempt will be made to create the `TrackNumber/TrackTotal` (trkn) and `DiscNumber/DiscTotal` (disk) atoms.
///
/// #### Pictures
///
/// [`Picture`]s will also be preserved, but their [`PictureType`] will be overwritten with [`PictureType::Other`].
#[derive(Default, PartialEq, Debug, Clone)]
#[tag(description = "An MP4 ilst atom", supported_formats(Mp4))]
pub struct Ilst {
	pub(crate) atoms: Vec<Atom<'static>>,
}

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

	/// Get an item by its [`AtomIdent`]
	///
	/// # Examples
	///
	/// ```rust
	/// use lofty::mp4::{AtomIdent, Ilst};
	/// use lofty::tag::Accessor;
	///
	/// let mut ilst = Ilst::new();
	/// ilst.set_title(String::from("Foo title"));
	///
	/// // Get the title by its FOURCC identifier
	/// let title = ilst.get(&AtomIdent::Fourcc(*b"\xa9nam"));
	/// assert!(title.is_some());
	/// ```
	pub fn get(&self, ident: &AtomIdent<'_>) -> Option<&Atom<'static>> {
		self.atoms.iter().find(|a| &a.ident == ident)
	}

	fn get_mut(&mut self, ident: &AtomIdent<'_>) -> Option<&mut Atom<'static>> {
		self.atoms.iter_mut().find(|a| &a.ident == ident)
	}

	/// Inserts an [`Atom`]
	///
	/// NOTE: Do not use this to replace atoms. This will take the value from the provided atom and
	///       merge it into an atom of the same type, keeping any existing value(s). To ensure an atom
	///       is replaced, use [`Ilst::replace_atom`].
	///
	/// # Examples
	///
	/// ```rust
	/// use lofty::mp4::{Atom, AtomData, AtomIdent, Ilst};
	///
	/// const TITLE_IDENTIFIER: AtomIdent = AtomIdent::Fourcc(*b"\xa9nam");
	///
	/// let mut ilst = Ilst::new();
	///
	/// // Set the title by manually constructing an `Atom`
	/// let title_atom = Atom::new(TITLE_IDENTIFIER, AtomData::UTF8(String::from("Foo title")));
	/// ilst.insert(title_atom);
	///
	/// // Get the title by its FOURCC identifier
	/// let title = ilst.get(&TITLE_IDENTIFIER);
	/// assert!(title.is_some());
	/// ```
	#[allow(clippy::missing_panics_doc)] // Unwrap on an infallible
	pub fn insert(&mut self, atom: Atom<'static>) {
		if atom.ident == COVR && atom.data.is_pictures() {
			for data in atom.data {
				match data {
					AtomData::Picture(p) => self.insert_picture(p),
					_ => unreachable!(),
				}
			}
			return;
		}

		if let Some(existing) = self.get_mut(atom.ident()) {
			existing.merge(atom).expect(
				"Somehow the atom merge condition failed, despite the validation beforehand.",
			);
			return;
		}

		self.atoms.push(atom);
	}

	/// Inserts an [`Atom`], replacing any atom with the same [`AtomIdent`]
	///
	/// # Examples
	///
	/// ```rust
	/// use lofty::mp4::{Atom, AtomData, AtomIdent, Ilst};
	/// use lofty::tag::Accessor;
	///
	/// const TITLE_IDENTIFIER: AtomIdent = AtomIdent::Fourcc(*b"\xa9nam");
	///
	/// let mut ilst = Ilst::new();
	///
	/// ilst.set_title(String::from("FooBar"));
	/// assert_eq!(ilst.title().as_deref(), Some("FooBar"));
	///
	/// // Replace our old title
	/// ilst.replace_atom(Atom::new(
	/// 	TITLE_IDENTIFIER,
	/// 	AtomData::UTF8(String::from("BarFoo")),
	/// ));
	/// assert_eq!(ilst.title().as_deref(), Some("BarFoo"));
	/// ```
	pub fn replace_atom(&mut self, atom: Atom<'_>) {
		let _ = self.remove(&atom.ident);
		self.atoms.push(atom.into_owned());
	}

	/// Remove an atom by its [`AtomIdent`]
	///
	/// # Examples
	///
	/// ```rust
	/// use lofty::mp4::{Atom, AtomData, AtomIdent, Ilst};
	/// use lofty::tag::Accessor;
	///
	/// const TITLE_IDENTIFIER: AtomIdent = AtomIdent::Fourcc(*b"\xa9nam");
	///
	/// let mut ilst = Ilst::new();
	/// ilst.set_title(String::from("Foo title"));
	///
	/// // Get the title by its FOURCC identifier
	/// let title = ilst.get(&TITLE_IDENTIFIER);
	/// assert!(title.is_some());
	///
	/// // Remove the title
	/// let returned = ilst.remove(&TITLE_IDENTIFIER);
	/// assert_eq!(returned.count(), 1);
	///
	/// let title = ilst.get(&TITLE_IDENTIFIER);
	/// assert!(title.is_none());
	/// ```
	pub fn remove<'a>(
		&'a mut self,
		ident: &AtomIdent<'_>,
	) -> impl Iterator<Item = Atom<'static>> + use<'a> {
		// TODO: drain_filter
		let mut split_idx = 0_usize;

		for read_idx in 0..self.atoms.len() {
			if &self.atoms[read_idx].ident == ident {
				self.atoms.swap(split_idx, read_idx);
				split_idx += 1;
			}
		}

		self.atoms.drain(..split_idx)
	}

	/// Retain atoms based on the predicate
	///
	/// See [`Vec::retain`](std::vec::Vec::retain)
	pub fn retain<F>(&mut self, f: F)
	where
		F: FnMut(&Atom<'_>) -> bool,
	{
		self.atoms.retain(f)
	}

	/// Returns all pictures, if there are any
	///
	/// # Examples
	///
	/// ```rust
	/// use lofty::mp4::Ilst;
	/// use lofty::picture::{MimeType, Picture, PictureType};
	/// use lofty::tag::TagExt;
	///
	/// let mut ilst = Ilst::new();
	///
	/// # let png_data = b"foo".to_vec();
	/// // Insert pictures
	/// ilst.insert_picture(
	/// 	Picture::unchecked(png_data)
	/// 		.mime_type(MimeType::Png)
	/// 		.build(),
	/// );
	///
	/// # let jpeg_data = b"bar".to_vec();
	/// ilst.insert_picture(
	/// 	Picture::unchecked(jpeg_data)
	/// 		.mime_type(MimeType::Jpeg)
	/// 		.build(),
	/// );
	///
	/// assert_eq!(ilst.pictures().unwrap().count(), 2);
	/// ```
	pub fn pictures(&self) -> Option<impl Iterator<Item = &Picture>> {
		let covr = self.get(&COVR)?;

		Some(covr.data().filter_map(|d| {
			if let AtomData::Picture(pic) = d {
				Some(pic)
			} else {
				None
			}
		}))
	}

	/// Inserts a picture
	///
	/// NOTE: If a `covr` atom exists in the tag, the picture will be appended to it.
	///
	/// # Examples
	///
	/// ```rust
	/// use lofty::mp4::Ilst;
	/// use lofty::picture::{MimeType, Picture, PictureType};
	/// use lofty::tag::TagExt;
	///
	/// let mut ilst = Ilst::new();
	///
	/// # let png_data = b"foo".to_vec();
	/// // Insert a single picture
	/// ilst.insert_picture(
	/// 	Picture::unchecked(png_data)
	/// 		.mime_type(MimeType::Png)
	/// 		.build(),
	/// );
	/// assert_eq!(ilst.len(), 1);
	///
	/// # let jpeg_data = b"bar".to_vec();
	/// // Insert another picture
	/// ilst.insert_picture(
	/// 	Picture::unchecked(jpeg_data)
	/// 		.mime_type(MimeType::Jpeg)
	/// 		.build(),
	/// );
	///
	/// // The existing `covr` atom is reused
	/// assert_eq!(ilst.len(), 1);
	/// assert_eq!(ilst.pictures().unwrap().count(), 2);
	/// ```
	pub fn insert_picture(&mut self, mut picture: Picture) {
		// This is just for correctness, it doesn't really matter.
		picture.pic_type = PictureType::Other;

		let data = AtomData::Picture(picture);
		let Some(existing_covr) = self.get_mut(&COVR) else {
			self.atoms.push(Atom {
				ident: COVR,
				data: AtomDataStorage::Single(data),
			});
			return;
		};

		existing_covr.push_data(data);
	}

	/// Removes all pictures
	pub fn remove_pictures(&mut self) {
		self.atoms
			.retain(|a| !matches!(a.data().next(), Some(AtomData::Picture(_))))
	}

	/// Sets the value of a flag ([`AtomData::Bool`]) atom
	///
	/// For identifiers, see [`constants::flags`].
	///
	/// # Examples
	///
	/// ```rust
	/// use lofty::mp4::constants::flags::COMPILATION;
	/// use lofty::mp4::{AtomIdent, Ilst};
	///
	/// // This file part of a compilation!
	/// let mut ilst = Ilst::new();
	/// ilst.set_flag(COMPILATION, true);
	///
	/// assert!(ilst.is_compilation());
	/// ```
	pub fn set_flag(&mut self, ident: AtomIdent<'_>, value: bool) {
		if !value {
			// A flag with a value of `false` is equivalent to removing it.
			let _ = self.remove(&ident);
			return;
		}

		let data = AtomData::Bool(value);
		self.replace_atom(Atom {
			ident,
			data: AtomDataStorage::Single(data),
		});
	}

	fn get_flag(&self, ident: &AtomIdent<'_>) -> Option<bool> {
		self.get(ident)
			.and_then(|atom| atom.data().next())
			.and_then(|data| match data {
				AtomData::Bool(b) => Some(*b),
				_ => None,
			})
	}

	impl_flag_accessors!(
		PODCAST(pcst),
		GAPLESS(pgap),
		SHOW_WORK(shwm),
		HD_VIDEO(hdvd),
		COMPILATION(cpil)
	);

	/// Returns the parental advisory rating according to the `rtng` atom
	pub fn advisory_rating(&self) -> Option<AdvisoryRating> {
		self.get(&ADVISORY_RATING)
			.into_iter()
			.flat_map(Atom::data)
			.filter_map(|data| match data {
				AtomData::SignedInteger(si) => u8::try_from(*si).ok(),
				AtomData::Unknown { data, .. } => data.first().copied(),
				_ => None,
			})
			.find_map(|rating| AdvisoryRating::try_from(rating).ok())
	}

	/// Sets the advisory rating
	pub fn set_advisory_rating(&mut self, advisory_rating: AdvisoryRating) {
		let byte = advisory_rating.as_u8();

		self.replace_atom(Atom {
			ident: ADVISORY_RATING,
			data: AtomDataStorage::Single(AtomData::SignedInteger(i32::from(byte))),
		})
	}

	// Extracts a u16 from an integer pair
	fn extract_number(&self, fourcc: [u8; 4], expected_size: usize) -> Option<u16> {
		if let Some(atom) = self.get(&AtomIdent::Fourcc(fourcc)) {
			match atom.data().next() {
				Some(AtomData::Unknown {
					code: DataType::Reserved,
					data,
				}) if data.len() >= expected_size => {
					return Some(u16::from_be_bytes([
						data[expected_size - 2],
						data[expected_size - 1],
					]));
				},
				_ => {},
			}
		}

		None
	}
}

impl<'a> IntoIterator for &'a Ilst {
	type Item = &'a Atom<'static>;
	type IntoIter = std::slice::Iter<'a, Atom<'static>>;

	fn into_iter(self) -> Self::IntoIter {
		self.atoms.iter()
	}
}

impl IntoIterator for Ilst {
	type Item = Atom<'static>;
	type IntoIter = std::vec::IntoIter<Self::Item>;

	fn into_iter(self) -> Self::IntoIter {
		self.atoms.into_iter()
	}
}

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

	fn track(&self) -> Option<u32> {
		self.extract_number(*b"trkn", 4).map(u32::from)
	}

	fn set_track(&mut self, value: u32) {
		let track = (value as u16).to_be_bytes();
		let track_total = (self.track_total().unwrap_or(0) as u16).to_be_bytes();

		let data = vec![0, 0, track[0], track[1], track_total[0], track_total[1]];
		self.replace_atom(Atom::unknown_implicit(TRACK_NUMBER, data));
	}

	fn remove_track(&mut self) {
		let _ = self.remove(&TRACK_NUMBER);
	}

	fn track_total(&self) -> Option<u32> {
		self.extract_number(*b"trkn", 6).map(u32::from)
	}

	fn set_track_total(&mut self, value: u32) {
		let track_total = (value as u16).to_be_bytes();
		let track = (self.track().unwrap_or(0) as u16).to_be_bytes();

		let data = vec![0, 0, track[0], track[1], track_total[0], track_total[1]];
		self.replace_atom(Atom::unknown_implicit(TRACK_NUMBER, data));
	}

	fn remove_track_total(&mut self) {
		let track = self.track();
		let _ = self.remove(&TRACK_NUMBER);

		if let Some(track) = track {
			let track_bytes = (track as u16).to_be_bytes();
			let data = vec![0, 0, track_bytes[0], track_bytes[1], 0, 0];

			self.replace_atom(Atom::unknown_implicit(TRACK_NUMBER, data));
		}
	}

	fn disk(&self) -> Option<u32> {
		self.extract_number(*b"disk", 4).map(u32::from)
	}

	fn set_disk(&mut self, value: u32) {
		let disk = (value as u16).to_be_bytes();
		let disk_total = (self.disk_total().unwrap_or(0) as u16).to_be_bytes();

		let data = vec![0, 0, disk[0], disk[1], disk_total[0], disk_total[1]];
		self.replace_atom(Atom::unknown_implicit(DISC_NUMBER, data));
	}

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

	fn disk_total(&self) -> Option<u32> {
		self.extract_number(*b"disk", 6).map(u32::from)
	}

	fn set_disk_total(&mut self, value: u32) {
		let disk_total = (value as u16).to_be_bytes();
		let disk = (self.disk().unwrap_or(0) as u16).to_be_bytes();

		let data = vec![0, 0, disk[0], disk[1], disk_total[0], disk_total[1]];
		self.replace_atom(Atom::unknown_implicit(AtomIdent::Fourcc(*b"disk"), data));
	}

	fn remove_disk_total(&mut self) {
		let disk = self.disk();
		let _ = self.remove(&DISC_NUMBER);

		if let Some(disk) = disk {
			let disk_bytes = (disk as u16).to_be_bytes();
			let data = vec![0, 0, disk_bytes[0], disk_bytes[1], 0, 0];

			self.replace_atom(Atom::unknown_implicit(DISC_NUMBER, data));
		}
	}

	fn date(&self) -> Option<Timestamp> {
		if let Some(atom) = self.get(&AtomIdent::Fourcc(*b"\xa9day"))
			&& let Some(AtomData::UTF8(text)) = atom.data().next()
		{
			return try_parse_timestamp(text);
		}

		None
	}

	fn set_date(&mut self, value: Timestamp) {
		self.replace_atom(Atom::text(
			AtomIdent::Fourcc(*b"\xa9day"),
			value.to_string(),
		));
	}

	fn remove_date(&mut self) {
		let _ = self.remove(&AtomIdent::Fourcc(*b"\xa9day"));
	}
}

impl TagExt for Ilst {
	type RefKey<'a> = &'a AtomIdent<'a>;

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

	fn len(&self) -> usize {
		self.atoms.len()
	}

	fn contains<'a>(&'a self, key: Self::RefKey<'a>) -> bool {
		self.atoms.iter().any(|atom| &atom.ident == key)
	}

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

	fn dump_to<W: Write>(
		&self,
		writer: &mut W,
		write_options: WriteOptions,
	) -> std::result::Result<(), TagEncodingError> {
		self.as_ref()
			.dump_to(writer, write_options)
			.map_err(Into::into)
	}

	fn clear(&mut self) {
		self.atoms.clear();
	}
}

impl TagWriteExt for Ilst {
	fn save_to<F>(
		&self,
		file: VerifiedFile<'_, F>,
		write_options: WriteOptions,
	) -> Result<(), FileEncodingError>
	where
		F: FileLike,
	{
		self.as_ref().write_to(file, write_options)
	}
}

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

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

impl Deref for SplitTagRemainder {
	type Target = Ilst;

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

impl SplitTag for Ilst {
	type Remainder = SplitTagRemainder;

	#[allow(clippy::let_and_return)] // For clarity
	fn split_tag(mut self) -> (Self::Remainder, Tag) {
		let mut tag = Tag::new(TagType::Mp4Ilst);

		self.atoms.retain_mut(|atom| {
			let Atom { ident, data } = atom;

			let key_str = match ident {
				AtomIdent::Fourcc(fourcc) => fourcc.iter().map(|b| *b as char).collect::<String>(),
				AtomIdent::Freeform { mean, name } => {
					format!("----:{mean}:{name}")
				},
			};

			/// AtomIdent -> ItemKey mapping
			enum ItemKeyMapping {
				Mapped(ItemKey),
				/// `covr` atoms, which don't have an `ItemKey` mapping
				Picture,
			}

			let key = match ItemKey::from_key(TagType::Mp4Ilst, &key_str) {
				Some(key) => ItemKeyMapping::Mapped(key),
				None if *ident == COVR => ItemKeyMapping::Picture,
				None => return true, // Keep atom
			};

			// `AtomData::retain_mut()` returns a bool indicating whether every value in the atom
			// was consumed. If so, we can discard the entire atom. Otherwise, we'll still need to keep
			// it around for later
			let atom_retained = data.retain_mut(|val| {
				match val {
					AtomData::UTF8(text) | AtomData::UTF16(text) => {
						if let ItemKeyMapping::Mapped(key) = &key {
							if *key == ItemKey::Popularimeter {
								let Ok(rate) = text.parse::<u8>() else {
									log::warn!(
										"Unable to parse popularimeter rating during tag split, \
										 retaining"
									);
									return true; // Data retained
								};

								let Some(generic) = Popularimeter::mapped(
									// We only have a rating available, so we'll just fall through to the custom
									// rating provider. By default, the custom provider is MusicBee, which it *seems*
									// like every other app uses for MP4 ratings. Not 100% sure...
									"",
									TagType::Mp4Ilst,
									rate,
									0,
								) else {
									log::warn!(
										"Unable to find handler for popularimeter during tag \
										 split, retaining"
									);
									return true; // Data retained
								};

								tag.items
									.push(TagItem::new(*key, ItemValue::Text(generic.to_string())));
								return false; // Data consumed
							}

							tag.items
								.push(TagItem::new(*key, ItemValue::Text(std::mem::take(text))));
						}
					},
					AtomData::Bool(b) => {
						if let ItemKeyMapping::Mapped(key) = &key {
							let text = if *b { "1".to_owned() } else { "0".to_owned() };
							tag.items.push(TagItem::new(*key, ItemValue::Text(text)));
						}
					},
					AtomData::Picture(picture) => {
						if let ItemKeyMapping::Picture = key {
							tag.pictures
								.push(std::mem::replace(picture, Picture::EMPTY));
						}
					},
					// We have to special case track/disc numbers since they are stored together
					AtomData::Unknown {
						code: DataType::Reserved,
						data,
					} if data.len() >= 6 => {
						let (number_key, total_key) = if *ident == TRACK_NUMBER {
							(ItemKey::TrackNumber, ItemKey::TrackTotal)
						} else if *ident == DISC_NUMBER {
							(ItemKey::DiscNumber, ItemKey::DiscTotal)
						} else {
							return true; // Data retained
						};

						let current = u16::from_be_bytes([data[2], data[3]]);
						let total = u16::from_be_bytes([data[4], data[5]]);
						if current > 0 {
							tag.insert_text(number_key, current.to_string());
						}
						if total > 0 {
							tag.insert_text(total_key, total.to_string());
						}
					},
					// Data retained
					_ => return true,
				}

				false // Data consumed
			});

			atom_retained
		});

		if let Some(rating) = self.advisory_rating() {
			tag.insert_text(ItemKey::ParentalAdvisory, rating.as_u8().to_string());
			let _ = self.remove(&ADVISORY_RATING);
		}

		(SplitTagRemainder(self), tag)
	}
}

impl MergeTag for SplitTagRemainder {
	type Merged = Ilst;

	fn merge_tag(self, tag: Tag) -> Self::Merged {
		fn convert_to_uint(space: &mut Option<u16>, cont: &str) {
			if let Ok(num) = cont.parse::<u16>() {
				*space = Some(num);
			}
		}

		fn create_int_pair(tag: &mut Ilst, ident: [u8; 4], pair: (Option<u16>, Option<u16>)) {
			match pair {
				(None, None) => {},
				_ => {
					let current = pair.0.unwrap_or(0).to_be_bytes();
					let total = pair.1.unwrap_or(0).to_be_bytes();

					tag.atoms.push(Atom {
						ident: AtomIdent::Fourcc(ident),
						data: AtomDataStorage::Single(AtomData::Unknown {
							code: DataType::Reserved,
							data: vec![0, 0, current[0], current[1], total[0], total[1], 0, 0],
						}),
					})
				},
			}
		}

		let Self(mut merged) = self;

		// Storage for integer pairs
		let mut tracks: (Option<u16>, Option<u16>) = (None, None);
		let mut discs: (Option<u16>, Option<u16>) = (None, None);

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

			let Ok(ident) = TryInto::<AtomIdent<'_>>::try_into(&key) else {
				log::debug!("No mapping exists for item key `{:?}`, discarding", key);
				continue;
			};

			let ItemValue::Text(text) = item.item_value else {
				continue;
			};

			match key {
				ItemKey::TrackNumber => convert_to_uint(&mut tracks.0, text.as_str()),
				ItemKey::TrackTotal => convert_to_uint(&mut tracks.1, text.as_str()),
				ItemKey::DiscNumber => convert_to_uint(&mut discs.0, text.as_str()),
				ItemKey::DiscTotal => convert_to_uint(&mut discs.1, text.as_str()),
				ItemKey::FlagCompilation | ItemKey::FlagPodcast => {
					let Some(data) = flag_item(text.as_str()) else {
						continue;
					};

					merged.atoms.push(Atom {
						ident: ident.into_owned(),
						data: AtomDataStorage::Single(AtomData::Bool(data)),
					})
				},
				ItemKey::ParentalAdvisory => {
					let Ok(rating) = text.parse::<u8>() else {
						log::warn!(
							"Parental advisory rating is not a number: {}, discarding",
							text
						);
						continue;
					};

					let Ok(parsed_rating) = AdvisoryRating::try_from(rating) else {
						log::warn!(
							"Parental advisory rating is out of range: {rating}, discarding"
						);
						continue;
					};

					merged.atoms.push(Atom {
						ident: ident.into_owned(),
						data: AtomDataStorage::Single(AtomData::SignedInteger(i32::from(
							parsed_rating.as_u8(),
						))),
					})
				},
				ItemKey::Popularimeter => {
					let Ok(popm) = Popularimeter::from_str(&text) else {
						log::warn!("Failed to parse popularimeter during tag merge, skipping");
						continue;
					};

					// AFAICT the apps that support `rate` (which are very few and far between) only
					// support the ratings as text for some reason?
					let value = popm.mapped_value(TagType::Mp4Ilst);
					merged.atoms.push(Atom {
						ident: ident.into_owned(),
						data: AtomDataStorage::Single(AtomData::UTF8(value.to_string())),
					});
				},
				_ => merged.atoms.push(Atom {
					ident: ident.into_owned(),
					data: AtomDataStorage::Single(AtomData::UTF8(text)),
				}),
			}
		}

		let mut covr = None;
		for mut picture in tag.pictures {
			// Just for correctness, since we can't actually
			// assign a picture type in this format
			picture.pic_type = PictureType::Other;

			match &mut covr {
				None => {
					covr = Some(Atom {
						ident: COVR,
						data: AtomDataStorage::Single(AtomData::Picture(picture)),
					});
				},
				Some(covr) => {
					covr.push_data(AtomData::Picture(picture));
				},
			}
		}

		if let Some(covr) = covr {
			merged.atoms.push(covr);
		}

		create_int_pair(&mut merged, *b"trkn", tracks);
		create_int_pair(&mut merged, *b"disk", discs);

		merged
	}
}

impl From<Ilst> for Tag {
	fn from(input: Ilst) -> Self {
		let (remainder, mut tag) = input.split_tag();

		if unsafe { global_options().preserve_format_specific_items } && remainder.0.len() > 0 {
			tag.companion_tag = Some(CompanionTag::Ilst(remainder.0));
		}

		tag
	}
}

impl From<Tag> for Ilst {
	fn from(mut input: Tag) -> Self {
		if unsafe { global_options().preserve_format_specific_items }
			&& let Some(companion) = input.companion_tag.take().and_then(CompanionTag::ilst)
		{
			return SplitTagRemainder(companion).merge_tag(input);
		}

		SplitTagRemainder::default().merge_tag(input)
	}
}