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
use std::fmt;
use std::fs::File;
use std::io::{BufReader, BufWriter, Read, Seek, SeekFrom, Write};
use std::ops::Deref;

use crate::{data, Content, ContentT, Data, DataT, ErrorKind, Tag};

/// A lowercase list of valid file types defined by the `ftyp` atom.
#[rustfmt::skip]
pub const VALID_FILETYPES: [&str; 8] = [
    "iso2",
    "isom",
    "m4a ",
    "m4b ",
    "m4p ",
    "m4v ",
    "mp41",
    "mp42",
];

/// (`ftyp`) Identifier of an atom information about the filetype.
pub const FILETYPE: Ident = Ident(*b"ftyp");
/// (`moov`) Identifier of an atom containing a structure of children storing metadata.
pub const MOVIE: Ident = Ident(*b"moov");
/// (`trak`) Identifier of an atom containing information about a single track.
pub const TRACK: Ident = Ident(*b"trak");
/// (`mdia`) Identifier of an atom containing information about a tracks media type and data.
pub const MEDIA: Ident = Ident(*b"mdia");
/// (`mdhd`) Identifier of an atom specifying the characteristics of a media atom.
pub const MEDIA_HEADER: Ident = Ident(*b"mdhd");
/// (`udta`) Identifier of an atom containing user metadata.
pub const USER_DATA: Ident = Ident(*b"udta");
/// (`meta`) Identifier of an atom containing a metadata item list.
pub const METADATA: Ident = Ident(*b"meta");
/// (`ilst`) Identifier of an atom containing a list of metadata atoms.
pub const ITEM_LIST: Ident = Ident(*b"ilst");
/// (`data`) Identifier of an atom containing typed data.
pub const DATA: Ident = Ident(*b"data");

// iTunes 4.0 atoms
/// (`©alb`)
pub const ALBUM: Ident = Ident(*b"\xa9alb");
/// (`aART`)
pub const ALBUM_ARTIST: Ident = Ident(*b"aART");
/// (`©ART`)
pub const ARTIST: Ident = Ident(*b"\xa9ART");
/// (`covr`)
pub const ARTWORK: Ident = Ident(*b"covr");
/// (`tmpo`)
pub const BPM: Ident = Ident(*b"tmpo");
/// (`©cmt`)
pub const COMMENT: Ident = Ident(*b"\xa9cmt");
/// (`cpil`)
pub const COMPILATION: Ident = Ident(*b"cpil");
/// (`©wrt`)
pub const COMPOSER: Ident = Ident(*b"\xa9wrt");
/// (`cprt`)
pub const COPYRIGHT: Ident = Ident(*b"cprt");
/// (`©gen`)
pub const CUSTOM_GENRE: Ident = Ident(*b"\xa9gen");
/// (`disk`)
pub const DISC_NUMBER: Ident = Ident(*b"disk");
/// (`©too`)
pub const ENCODER: Ident = Ident(*b"\xa9too");
/// (`rtng`)
pub const ADVISORY_RATING: Ident = Ident(*b"rtng");
/// (`gnre`)
pub const STANDARD_GENRE: Ident = Ident(*b"gnre");
/// (`©nam`)
pub const TITLE: Ident = Ident(*b"\xa9nam");
/// (`trkn`)
pub const TRACK_NUMBER: Ident = Ident(*b"trkn");
/// (`©day`)
pub const YEAR: Ident = Ident(*b"\xa9day");

// iTunes 4.2 atoms
/// (`©grp`)
pub const GROUPING: Ident = Ident(*b"\xa9grp");
/// (`stik`)
pub const MEDIA_TYPE: Ident = Ident(*b"stik");

// iTunes 4.9 atoms
/// (`catg`)
pub const CATEGORY: Ident = Ident(*b"catg");
/// (`keyw`)
pub const KEYWORD: Ident = Ident(*b"keyw");
/// (`pcst`)
pub const PODCAST: Ident = Ident(*b"pcst");
/// (`egid`)
pub const PODCAST_EPISODE_GLOBAL_UNIQUE_ID: Ident = Ident(*b"egid");
/// (`purl`)
pub const PODCAST_URL: Ident = Ident(*b"purl");

// iTunes 5.0
/// (`desc`)
pub const DESCRIPTION: Ident = Ident(*b"desc");
/// (`©lyr`)
pub const LYRICS: Ident = Ident(*b"\xa9lyr");

// iTunes 6.0
/// (`tves`)
pub const TV_EPISODE: Ident = Ident(*b"tves");
/// (`tven`)
pub const TV_EPISODE_NUMBER: Ident = Ident(*b"tven");
/// (`tvnn`)
pub const TV_NETWORK_NAME: Ident = Ident(*b"tvnn");
/// (`tvsn`)
pub const TV_SEASON: Ident = Ident(*b"tvsn");
/// (`tvsh`)
pub const TV_SHOW_NAME: Ident = Ident(*b"tvsh");

// iTunes 6.0.2
/// (`purd`)
pub const PURCHASE_DATE: Ident = Ident(*b"purd");

// iTunes 7.0
/// (`pgap`)
pub const GAPLESS_PLAYBACK: Ident = Ident(*b"pgap");

// Work, Movement
/// (`©mvn`)
pub const MOVEMENT: Ident = Ident(*b"\xa9mvn");
/// (`©mvc`)
pub const MOVEMENT_COUNT: Ident = Ident(*b"\xa9mvc");
/// (`©mvi`)
pub const MOVEMENT_INDEX: Ident = Ident(*b"\xa9mvi");
/// (`©wrk`)
pub const WORK: Ident = Ident(*b"\xa9wrk");
/// (`shwm`)
pub const SHOW_MOVEMENT: Ident = Ident(*b"shwm");

lazy_static! {
    /// Lazily initialized static reference to a `ftyp` atom template.
    pub static ref FILETYPE_ATOM_T: AtomT = filetype_atom_t();
    /// Lazily initialized static reference to an atom hierarchy template leading to an empty `ilst` atom.
    pub static ref ITEM_LIST_ATOM_T: AtomT = item_list_atom_t();
    /// Lazily initialized static reference to an atom metadata hierarchy template needed to parse metadata.
    pub static ref METADATA_ATOM_T: AtomT = metadata_atom_t();
}

/// A 4 byte atom identifier.
#[derive(Clone, Copy, Default, Eq, PartialEq)]
pub struct Ident(pub [u8; 4]);

impl Deref for Ident {
    type Target = [u8; 4];

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

impl fmt::Debug for Ident {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Ident({})", self.0.iter().map(|b| char::from(*b)).collect::<String>())
    }
}

impl fmt::Display for Ident {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0.iter().map(|b| char::from(*b)).collect::<String>())
    }
}

/// A struct representing data that is associated with an Atom identifier.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AtomData {
    /// The 4 byte identifier of the atom.
    pub ident: Ident,
    /// The data corresponding to the identifier.
    pub data: Data,
}

impl AtomData {
    /// Creates atom data with the `identifier` and `data`.
    pub const fn new(ident: Ident, data: Data) -> Self {
        Self { ident, data }
    }

    /// Creates atom data with the `identifier` and raw `data` contained by the
    /// atom.
    pub fn try_from_raw(atom: Atom) -> Option<Self> {
        match atom.content {
            Content::RawData(d) => Some(Self::new(atom.ident, d)),
            _ => None,
        }
    }

    /// Creates atom data with the `identifier` and typed `data` contained by a children data atom.
    pub fn try_from_typed(atom: Atom) -> Option<Self> {
        if let Some(d) = atom.content.take_child(DATA) {
            if let Content::TypedData(data) = d.content {
                return Some(Self::new(atom.ident, data));
            }
        }
        None
    }

    /// Creates an atom with the `ident`, `offset` 0, containing a data atom with the `data`.
    pub fn into_typed(self) -> Atom {
        Atom::new(self.ident, 0, Content::data_atom_with(self.data))
    }

    /// Creates an atom with the `ident`, `offset` 0, containing a data atom with the `data`.
    pub fn to_typed(&self) -> Atom {
        Atom::new(self.ident, 0, Content::data_atom_with(self.data.clone()))
    }

    /// Creates an atom with the `ident`, `offset` 0, containing the raw `data`.
    pub fn into_raw(self) -> Atom {
        Atom::new(self.ident, 0, Content::RawData(self.data))
    }

    /// Creates an atom with the `ident`, `offset` 0, containing the raw `data`.
    pub fn to_raw(&self) -> Atom {
        Atom::new(self.ident, 0, Content::RawData(self.data.clone()))
    }
}

/// A struct that represents a MPEG-4 audio metadata atom.
#[derive(Clone, Default, Eq, PartialEq)]
pub struct Atom {
    /// The 4 byte identifier of the atom.
    pub ident: Ident,
    /// The offset in bytes separating the head from the content.
    pub offset: usize,
    /// The content of an atom.
    pub content: Content,
}

impl fmt::Debug for Atom {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Atom{{ {}, {}, {:#?} }}", self.ident, self.offset, self.content)
    }
}

impl Atom {
    /// Creates an atom containing the provided content at a n byte offset.
    pub const fn new(ident: Ident, offset: usize, content: Content) -> Self {
        Self { ident, offset, content }
    }

    /// Creates a data atom containing [`Content::TypedData`](crate::Content::TypedData)
    /// with the provided `data`.
    pub const fn data_atom_with(data: Data) -> Self {
        Self::new(DATA, 0, Content::TypedData(data))
    }

    /// Returns the length of the atom in bytes.
    pub fn len(&self) -> usize {
        8 + self.offset + self.content.len()
    }

    /// Returns true if the atom has no `offset` or `content` and only consists of it's 8 byte head.
    pub fn is_empty(&self) -> bool {
        self.offset + self.content.len() == 0
    }

    /// Returns a reference to the first children atom matching the `identifier`, if present.
    pub fn child(&self, ident: Ident) -> Option<&Self> {
        self.content.child(ident)
    }

    /// Returns a mutable reference to the first children atom matching the `identifier`, if
    /// present.
    pub fn child_mut(&mut self, ident: Ident) -> Option<&mut Self> {
        self.content.child_mut(ident)
    }

    /// Returns a mutable reference to the first children atom, if present.
    pub fn mut_first_child(&mut self) -> Option<&mut Self> {
        self.content.first_child_mut()
    }

    /// Consumes self and returns the first children atom matching the `identifier`, if present.
    pub fn take_child(self, ident: Ident) -> Option<Self> {
        self.content.take_child(ident)
    }

    /// Consumes self and returns the first children atom, if present.
    pub fn take_first_child(self) -> Option<Self> {
        self.content.take_first_child()
    }

    /// Attempts to write the atom to the writer.
    pub fn write_to(&self, writer: &mut impl Write) -> crate::Result<()> {
        writer.write_all(&(self.len() as u32).to_be_bytes())?;
        writer.write_all(&*self.ident)?;
        writer.write_all(&vec![0u8; self.offset])?;

        self.content.write_to(writer)?;

        Ok(())
    }

    /// Checks if the filetype is valid, returns an error otherwise.
    pub fn check_filetype(&self) -> crate::Result<()> {
        match &self.content {
            Content::RawData(Data::Utf8(s)) => {
                let major_brand = s.split('\u{0}').next().unwrap();
                if VALID_FILETYPES.iter().any(|e| e.eq_ignore_ascii_case(major_brand)) {
                    return Ok(());
                }

                Err(crate::Error::new(
                    ErrorKind::InvalidFiletype(s.to_string()),
                    "Invalid filetype.".to_owned(),
                ))
            }
            _ => Err(crate::Error::new(ErrorKind::NoTag, "No filetype atom found.".to_owned())),
        }
    }
}

/// A template representing a MPEG-4 audio metadata atom.
#[derive(Clone, Default, Eq, PartialEq)]
pub struct AtomT {
    /// The 4 byte identifier of the atom.
    pub ident: Ident,
    /// The offset in bytes separating the head from the content.
    pub offset: usize,
    /// The content template of an atom template.
    pub content: ContentT,
}

impl fmt::Debug for AtomT {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Atom{{ {}, {}, {:#?} }}", self.ident, self.offset, self.content)
    }
}

impl AtomT {
    /// Creates an atom template containing the provided content at a n byte offset.
    pub const fn new(ident: Ident, offset: usize, content: ContentT) -> Self {
        Self { ident, offset, content }
    }

    /// Creates a data atom template containing
    /// [`ContentT::TypedData`](crate::ContentT::TypedData).
    pub const fn data_atom() -> Self {
        Self::new(DATA, 0, ContentT::TypedData)
    }

    /// Returns a reference to the first children atom template matching the identifier, if present.
    pub fn child(&self, ident: Ident) -> Option<&Self> {
        self.content.child(ident)
    }

    /// Returns a reference to the first children atom template, if present.
    pub fn first_child(&self) -> Option<&Self> {
        self.content.first_child()
    }

    /// Returns a mutable reference to the first children atom template matching the identifier, if
    /// present.
    pub fn child_mut(&mut self, ident: Ident) -> Option<&mut Self> {
        self.content.child_mut(ident)
    }

    /// Returns a mutable reference to the first children atom template, if present.
    pub fn first_child_mut(&mut self) -> Option<&mut Self> {
        self.content.first_child_mut()
    }

    /// Consumes self and returns the first children atom template matching the `identifier`, if
    /// present.
    pub fn take_child(self, ident: Ident) -> Option<Self> {
        self.content.take_child(ident)
    }

    /// Consumes self and returns the first children atom template, if present.
    pub fn take_first_child(self) -> Option<Self> {
        self.content.take_first_child()
    }

    /// Attempts to parse an atom, that matches the template, from the `reader`.  This should only
    /// be used if the atom has to be in this exact position, if the parsed and expected
    /// `identifier`s don't match this will return an error.
    pub fn parse_next(&self, reader: &mut (impl Read + Seek)) -> crate::Result<Atom> {
        let (length, ident) = match parse_head(reader) {
            Ok(h) => h,
            Err(e) => return Err(e),
        };

        if ident == self.ident {
            match self.parse_content(reader, length) {
                Ok(c) => Ok(Atom::new(self.ident, self.offset, c)),
                Err(e) => Err(crate::Error::new(
                    e.kind,
                    format!("Error reading {}: {}", ident, e.description),
                )),
            }
        } else {
            Err(crate::Error::new(
                ErrorKind::AtomNotFound(self.ident),
                format!("Expected {} found {}", self.ident, ident),
            ))
        }
    }

    /// Attempts to parse an atom, that matches the template, from the reader.
    pub fn parse(&self, reader: &mut (impl Read + Seek)) -> crate::Result<Atom> {
        let current_position = reader.seek(SeekFrom::Current(0))?;
        let complete_length = reader.seek(SeekFrom::End(0))?;
        let length = (complete_length - current_position) as usize;
        reader.seek(SeekFrom::Start(current_position))?;

        let mut parsed_bytes = 0;

        while parsed_bytes < length {
            let (atom_length, atom_ident) = parse_head(reader)?;

            if atom_ident == self.ident {
                return match self.parse_content(reader, atom_length) {
                    Ok(c) => Ok(Atom::new(self.ident, self.offset, c)),
                    Err(e) => Err(crate::Error::new(
                        e.kind,
                        format!("Error reading {}: {}", atom_ident, e.description),
                    )),
                };
            } else {
                reader.seek(SeekFrom::Current((atom_length - 8) as i64))?;
            }

            parsed_bytes += atom_length;
        }

        Err(crate::Error::new(
            ErrorKind::AtomNotFound(self.ident),
            format!("No {} atom found", self.ident),
        ))
    }

    /// Attempts to parse the atom template's content from the reader.
    pub fn parse_content(
        &self,
        reader: &mut (impl Read + Seek),
        length: usize,
    ) -> crate::Result<Content> {
        if length > 8 {
            if self.offset != 0 {
                reader.seek(SeekFrom::Current(self.offset as i64))?;
            }
            self.content.parse(reader, length - 8 - self.offset)
        } else {
            Ok(Content::Empty)
        }
    }
}

/// Attempts to read MPEG-4 audio metadata from the reader.
pub fn read_tag_from(reader: &mut (impl Read + Seek)) -> crate::Result<Tag> {
    let mut tag_atoms = None;
    let mut mdhd_data = None;

    let ftyp = FILETYPE_ATOM_T.parse_next(reader)?;
    ftyp.check_filetype()?;
    let ftyp_data = match ftyp.content {
        Content::RawData(Data::Utf8(s)) => Some(s),
        _ => None,
    };

    let moov = METADATA_ATOM_T.parse(reader)?;
    for a in moov.content.into_iter() {
        match a.ident {
            TRACK => {
                if let Some(mdia) = a.take_child(MEDIA) {
                    if let Some(mdhd) = mdia.take_child(MEDIA_HEADER) {
                        if let Content::RawData(Data::Reserved(v)) = mdhd.content {
                            mdhd_data = Some(v);
                        }
                    }
                }
            }
            USER_DATA => {
                if let Some(meta) = a.take_child(METADATA) {
                    if let Some(ilst) = meta.take_child(ITEM_LIST) {
                        if let Content::Atoms(atoms) = ilst.content {
                            tag_atoms = Some(
                                atoms
                                    .into_iter()
                                    .filter(|a| {
                                        if let Some(d) = a.child(DATA) {
                                            if let Content::TypedData(_) = d.content {
                                                return true;
                                            }
                                        }
                                        false
                                    })
                                    .collect(),
                            );
                        }
                    }
                }
            }
            _ => (),
        }
    }

    let tag_atoms = match tag_atoms {
        Some(t) => t,
        None => Vec::new(),
    };

    Ok(Tag::new(ftyp_data, mdhd_data, tag_atoms))
}

/// Attempts to write the metadata atoms to the file inside the item list atom.
pub fn write_tag_to(file: &File, atoms: &[Atom]) -> crate::Result<()> {
    let mut reader = BufReader::new(file);
    let mut writer = BufWriter::new(file);

    let mut atom_pos_and_len = Vec::new();
    let mut destination = ITEM_LIST_ATOM_T.deref();
    let ftyp = FILETYPE_ATOM_T.parse_next(&mut reader)?;
    ftyp.check_filetype()?;

    while let Ok((length, ident)) = parse_head(&mut reader) {
        if ident == destination.ident {
            let pos = reader.seek(SeekFrom::Current(0))? as usize - 8;
            atom_pos_and_len.push((pos, length));

            reader.seek(SeekFrom::Current(destination.offset as i64))?;

            match destination.first_child() {
                Some(a) => destination = a,
                None => break,
            }
        } else {
            reader.seek(SeekFrom::Current(length as i64 - 8))?;
        }
    }

    let old_file_length = reader.seek(SeekFrom::End(0))?;
    let metadata_position = atom_pos_and_len[atom_pos_and_len.len() - 1].0 + 8;
    let old_metadata_length = atom_pos_and_len[atom_pos_and_len.len() - 1].1 - 8;
    let new_metadata_length = atoms.iter().map(|a| a.len()).sum::<usize>();
    let metadata_length_difference = new_metadata_length as i32 - old_metadata_length as i32;

    // reading additional data after metadata
    let mut additional_data =
        Vec::with_capacity(old_file_length as usize - (metadata_position + old_metadata_length));
    reader.seek(SeekFrom::Start((metadata_position + old_metadata_length) as u64))?;
    reader.read_to_end(&mut additional_data)?;

    // adjusting the file length
    file.set_len((old_file_length as i64 + metadata_length_difference as i64) as u64)?;

    // adjusting the atom lengths
    for (pos, len) in atom_pos_and_len {
        writer.seek(SeekFrom::Start(pos as u64))?;
        writer.write_all(&((len as i32 + metadata_length_difference) as u32).to_be_bytes())?;
    }

    // writing metadata
    writer.seek(SeekFrom::Current(4))?;
    for a in atoms {
        a.write_to(&mut writer)?;
    }

    // writing additional data after metadata
    writer.write_all(&additional_data)?;
    writer.flush()?;

    Ok(())
}

/// Attempts to dump the metadata atoms to the writer. This doesn't include a complete MPEG-4
/// container hierarchy and won't result in a usable file.
pub fn dump_tag_to(writer: &mut impl Write, atoms: Vec<Atom>) -> crate::Result<()> {
    #[rustfmt::skip]
    let ftyp = Atom::new( FILETYPE, 0, Content::RawData(
        Data::Utf8("M4A \u{0}\u{0}\u{2}\u{0}isomiso2".to_owned())),
    );
    #[rustfmt::skip]
    let moov = Atom::new(MOVIE, 0, Content::atom(
        Atom::new( USER_DATA, 0, Content::atom(
            Atom::new( METADATA, 4, Content::atom(
                Atom::new(ITEM_LIST, 0, Content::Atoms(atoms))
            )),
        )),
    ));

    ftyp.write_to(writer)?;
    moov.write_to(writer)?;

    Ok(())
}

/// Attempts to parse the list of atoms, matching the templates, from the reader.
pub fn parse_atoms(
    atoms: &[AtomT],
    reader: &mut (impl Read + Seek),
    length: usize,
) -> crate::Result<Vec<Atom>> {
    let mut parsed_bytes = 0;
    let mut parsed_atoms = Vec::with_capacity(atoms.len());

    while parsed_bytes < length {
        let (atom_length, atom_ident) = parse_head(reader)?;

        let mut parsed = false;
        for a in atoms {
            if atom_ident == a.ident {
                match a.parse_content(reader, atom_length) {
                    Ok(c) => {
                        parsed_atoms.push(Atom::new(a.ident, a.offset, c));
                        parsed = true;
                    }
                    Err(e) => {
                        return Err(crate::Error::new(
                            e.kind,
                            format!("Error reading {}: {}", atom_ident, e.description),
                        ));
                    }
                }
                break;
            }
        }

        if atom_length > 8 && !parsed {
            reader.seek(SeekFrom::Current((atom_length - 8) as i64))?;
        }

        parsed_bytes += atom_length;
    }

    Ok(parsed_atoms)
}

/// Attempts to parse the atom's head containing a 32 bit unsigned integer determining the size of
/// the atom in bytes and the following 4 byte identifier from the reader.
pub fn parse_head(reader: &mut (impl Read + Seek)) -> crate::Result<(usize, Ident)> {
    let length = match data::read_u32(reader) {
        Ok(l) => l as usize,
        Err(e) => {
            return Err(crate::Error::new(e.kind, "Error reading atom length".to_owned()));
        }
    };
    let mut ident = [0u8; 4];
    if let Err(e) = reader.read_exact(&mut ident) {
        return Err(crate::Error::new(
            ErrorKind::Io(e),
            "Error reading atom identifier".to_owned(),
        ));
    }

    Ok((length, Ident(ident)))
}

/// Returns an `ftyp` atom template needed to parse the filetype.
fn filetype_atom_t() -> AtomT {
    AtomT::new(FILETYPE, 0, ContentT::RawData(DataT::new(data::UTF8)))
}

/// Returns an atom metadata hierarchy template needed to parse metadata.
#[rustfmt::skip]
fn metadata_atom_t() -> AtomT {
    AtomT::new( MOVIE, 0, ContentT::Atoms(vec![
        AtomT::new( TRACK, 0, ContentT::atom_t(
            AtomT::new( MEDIA, 0, ContentT::atom_t(
                AtomT::new( MEDIA_HEADER, 0, ContentT::RawData(
                    DataT::new(data::RESERVED)
                )),
            )),
        )),
        AtomT::new(USER_DATA, 0, ContentT::atom_t(
            AtomT::new(METADATA, 4, ContentT::atom_t(
                AtomT::new(ITEM_LIST, 0, ContentT::Atoms(vec![
                    AtomT::new(ADVISORY_RATING, 0, ContentT::data_atom_t()),
                    AtomT::new(ALBUM, 0, ContentT::data_atom_t()),
                    AtomT::new(ALBUM_ARTIST, 0, ContentT::data_atom_t()),
                    AtomT::new(ARTIST, 0, ContentT::data_atom_t()),
                    AtomT::new(BPM, 0, ContentT::data_atom_t()),
                    AtomT::new(CATEGORY, 0, ContentT::data_atom_t()),
                    AtomT::new(COMMENT, 0, ContentT::data_atom_t()),
                    AtomT::new(COMPILATION, 0, ContentT::data_atom_t()),
                    AtomT::new(COMPOSER, 0, ContentT::data_atom_t()),
                    AtomT::new(COPYRIGHT, 0, ContentT::data_atom_t()),
                    AtomT::new(CUSTOM_GENRE, 0, ContentT::data_atom_t()),
                    AtomT::new(DESCRIPTION, 0, ContentT::data_atom_t()),
                    AtomT::new(DISC_NUMBER, 0, ContentT::data_atom_t()),
                    AtomT::new(ENCODER, 0, ContentT::data_atom_t()),
                    AtomT::new(GAPLESS_PLAYBACK, 0, ContentT::data_atom_t()),
                    AtomT::new(GROUPING, 0, ContentT::data_atom_t()),
                    AtomT::new(KEYWORD, 0, ContentT::data_atom_t()),
                    AtomT::new(LYRICS, 0, ContentT::data_atom_t()),
                    AtomT::new(MEDIA_TYPE, 0, ContentT::data_atom_t()),
                    AtomT::new(MOVEMENT_COUNT, 0, ContentT::data_atom_t()),
                    AtomT::new(MOVEMENT_INDEX, 0, ContentT::data_atom_t()),
                    AtomT::new(MOVEMENT, 0, ContentT::data_atom_t()),
                    AtomT::new(PODCAST, 0, ContentT::data_atom_t()),
                    AtomT::new(PODCAST_EPISODE_GLOBAL_UNIQUE_ID, 0, ContentT::data_atom_t()),
                    AtomT::new(PODCAST_URL, 0, ContentT::data_atom_t()),
                    AtomT::new(PURCHASE_DATE, 0, ContentT::data_atom_t()),
                    AtomT::new(SHOW_MOVEMENT, 0, ContentT::data_atom_t()),
                    AtomT::new(STANDARD_GENRE, 0, ContentT::data_atom_t()),
                    AtomT::new(TITLE, 0, ContentT::data_atom_t()),
                    AtomT::new(TRACK_NUMBER, 0, ContentT::data_atom_t()),
                    AtomT::new(TV_EPISODE, 0, ContentT::data_atom_t()),
                    AtomT::new(TV_EPISODE_NUMBER, 0, ContentT::data_atom_t()),
                    AtomT::new(TV_NETWORK_NAME, 0, ContentT::data_atom_t()),
                    AtomT::new(TV_SEASON, 0, ContentT::data_atom_t()),
                    AtomT::new(TV_SHOW_NAME, 0, ContentT::data_atom_t()),
                    AtomT::new(WORK, 0, ContentT::data_atom_t()),
                    AtomT::new(YEAR, 0, ContentT::data_atom_t()),
                    AtomT::new(ARTWORK, 0, ContentT::data_atom_t()),
                ])),
            )),
        )),
    ]))
}

/// Returns an atom hierarchy leading to an empty `ilst` atom template.
#[rustfmt::skip]
fn item_list_atom_t() -> AtomT {
    AtomT::new(MOVIE, 0, ContentT::atom_t(
        AtomT::new(USER_DATA, 0, ContentT::atom_t(
            AtomT::new(METADATA, 4, ContentT::atom_t(
                AtomT::new(ITEM_LIST, 0, ContentT::atoms_t())
            ))
        ))
    ))
}