Chapter 1 Start Simple

The following example shows how you can read an audio file, parse, set, and save its metadata:

use audiotags::{MimeType, Picture, Tag, TagType};

const MP3_FILE: &'static str = "assets/a.mp3";

fn main() {
    // using `default()` so that the metadata format is guessed
    // (from the file extension) (in this case, Id3v2 tag is read)
    let mut tag = Tag::default().read_from_path(MP3_FILE).unwrap();
    // You can also specify the metadata format (tag type):
    let _tag = Tag::with_tag_type(TagType::Id3v2)
        .read_from_path(MP3_FILE)
        .expect("Fail to read!");

    tag.set_title("foo title");
    assert_eq!(tag.title(), Some("foo title"));
    tag.remove_title();
    assert!(tag.title().is_none());
    tag.remove_title();
    // trying to remove a field that's already empty won't hurt

    let cover = Picture {
        mime_type: MimeType::Jpeg,
        data: &vec![0u8; 10],
    };

    tag.set_album_cover(cover.clone());
    assert_eq!(tag.album_cover(), Some(cover));
    tag.remove_album_cover();
    assert!(tag.album_cover().is_none());
    tag.remove_album_cover();

    tag.save_to_path(MP3_FILE).expect("Fail to save");
    // TASK: reload the file and prove the data have been saved
}