Enum Tag

Source
pub enum Tag {
    Id3Tag {
        inner: Tag,
    },
    VorbisFlacTag {
        inner: Tag,
    },
    Mp4Tag {
        inner: Tag,
    },
    OpusTag {
        inner: Tag,
    },
}
Expand description

An object containing tags of one of the supported formats.

Variants§

§

Id3Tag

Fields

§inner: Tag
§

VorbisFlacTag

Fields

§inner: Tag
§

Mp4Tag

Fields

§inner: Tag
§

OpusTag

Fields

§inner: Tag

Implementations§

Source§

impl Tag

Source

pub fn read_from_path<P: AsRef<Path>>(path: P) -> Result<Self>

Attempts to read a set of tags from the given path.

§Errors

This function could error if the given path has a file extension which contains invalid unicode or if the given path does not have a file extension at all.

This function could also error if the given path has a valid extension but the extension is not among the types supported by this crate.

Lastly, an error will be raised if the file type is supported but the reading the tags fails for some reason other than missing tags.

Examples found in repository?
examples/read_tag.rs (line 12)
8fn main() {
9    let path = PathBuf::from(args().nth(1).unwrap());
10
11    // Option 1: read from path
12    let tag = Tag::read_from_path(&path).unwrap();
13    println!("{:#?}", tag.title());
14
15    // Option 2: read from reader
16    let mut f = File::open(&path).unwrap();
17
18    let mut data = Vec::new();
19    f.read_to_end(&mut data).unwrap();
20
21    let cursor = Cursor::new(data);
22    // You can also just pass in f instead of creating a cursor since Files are Read + Seek
23    let extension = path.extension().unwrap().to_str().unwrap();
24    let tag = Tag::read_from(extension, cursor).unwrap();
25    println!("{:#?}", tag.title());
26}
Source

pub fn read_from<R: Read + Seek>(extension: &str, f_in: R) -> Result<Self>

Attempts to read a set of tags from the given reader. The extension is necessary to determine which backend to use to decode the tags. extension must be one of [mp3, wav, aiff, flac, mp4, m4a, m4p, m4b, m4r, m4v, opus]

§Errors

This function can error if the given extension is not supported by this crate.

Lastly, an error will be raised if the file type is supported but the reading the tags fails for some reason other than missing tags. This could be, for example, that the given reader ended too early or that the tags were encoded improperly. Please inspect the debug output of the error for more information.

Examples found in repository?
examples/edit_tag.rs (line 19)
8fn main() {
9    let path = PathBuf::from(args().nth(1).unwrap());
10    let mut file = OpenOptions::new()
11        .read(true)
12        .write(true)
13        // .truncate(true)
14        .open(&path)
15        .unwrap();
16
17    let extension = path.extension().unwrap().to_str().unwrap();
18
19    let mut tag = Tag::read_from(extension, &file).unwrap();
20
21    file.rewind().unwrap();
22
23    let title = args().skip(2).collect::<Vec<String>>().join(" ");
24
25    tag.set_title(&title);
26    tag.write_to_file(&mut file).unwrap();
27}
More examples
Hide additional examples
examples/read_tag.rs (line 24)
8fn main() {
9    let path = PathBuf::from(args().nth(1).unwrap());
10
11    // Option 1: read from path
12    let tag = Tag::read_from_path(&path).unwrap();
13    println!("{:#?}", tag.title());
14
15    // Option 2: read from reader
16    let mut f = File::open(&path).unwrap();
17
18    let mut data = Vec::new();
19    f.read_to_end(&mut data).unwrap();
20
21    let cursor = Cursor::new(data);
22    // You can also just pass in f instead of creating a cursor since Files are Read + Seek
23    let extension = path.extension().unwrap().to_str().unwrap();
24    let tag = Tag::read_from(extension, cursor).unwrap();
25    println!("{:#?}", tag.title());
26}
examples/edit_tag_vec.rs (line 25)
10fn main() {
11    let path = PathBuf::from(args().nth(1).unwrap());
12    let mut file = OpenOptions::new()
13        .read(true)
14        .write(true)
15        // .truncate(true)
16        .open(&path)
17        .unwrap();
18
19    let extension = path.extension().unwrap().to_str().unwrap();
20
21    let mut data = Vec::new();
22    file.read_to_end(&mut data).unwrap();
23    let mut cursor = Cursor::new(&mut data);
24
25    let mut tag = Tag::read_from(extension, &mut cursor).unwrap();
26
27    cursor.rewind().unwrap();
28
29    let title = args().skip(2).collect::<Vec<String>>().join(" ");
30
31    tag.set_title(&title);
32    tag.write_to_vec(&mut data).unwrap();
33
34    file.rewind().unwrap();
35    file.write_all(&data).unwrap();
36}
Source

pub fn write_to_path<P: AsRef<Path>>(&mut self, path: P) -> Result<()>

Attempts to write the tags to the indicated path.

§Errors

This function will error if writing the tags fails in any way.

Source

pub fn write_to_file(&mut self, file: &mut File) -> Result<()>

Write to a file. The file should already contain valid data of the correct type (e.g. the file should already contain an opus stream in order to correctly write opus tags).

The file’s cursor should be at the beginning of the file, and it should be opened with read and write modes set (See OpenOptions for more info).

§Errors

This method can error if writing the tags fails, or if accessing the file fails (for example, if the modes are set wrong).

Examples found in repository?
examples/edit_tag.rs (line 26)
8fn main() {
9    let path = PathBuf::from(args().nth(1).unwrap());
10    let mut file = OpenOptions::new()
11        .read(true)
12        .write(true)
13        // .truncate(true)
14        .open(&path)
15        .unwrap();
16
17    let extension = path.extension().unwrap().to_str().unwrap();
18
19    let mut tag = Tag::read_from(extension, &file).unwrap();
20
21    file.rewind().unwrap();
22
23    let title = args().skip(2).collect::<Vec<String>>().join(" ");
24
25    tag.set_title(&title);
26    tag.write_to_file(&mut file).unwrap();
27}
Source

pub fn write_to_vec(&mut self, vec: &mut Vec<u8>) -> Result<()>

Write to a byte vector. The vector should already contain valid data of the correct type (e.g. the vector should already contain an opus stream in order to correctly write opus tags).

§Errors

This method can error if one of the internal write methods fails. If that happens, the inner error will contain more information.

Examples found in repository?
examples/edit_tag_vec.rs (line 32)
10fn main() {
11    let path = PathBuf::from(args().nth(1).unwrap());
12    let mut file = OpenOptions::new()
13        .read(true)
14        .write(true)
15        // .truncate(true)
16        .open(&path)
17        .unwrap();
18
19    let extension = path.extension().unwrap().to_str().unwrap();
20
21    let mut data = Vec::new();
22    file.read_to_end(&mut data).unwrap();
23    let mut cursor = Cursor::new(&mut data);
24
25    let mut tag = Tag::read_from(extension, &mut cursor).unwrap();
26
27    cursor.rewind().unwrap();
28
29    let title = args().skip(2).collect::<Vec<String>>().join(" ");
30
31    tag.set_title(&title);
32    tag.write_to_vec(&mut data).unwrap();
33
34    file.rewind().unwrap();
35    file.write_all(&data).unwrap();
36}
Source

pub fn new_empty_id3() -> Self

Creates an empty set of tags in the ID3 format.

Source

pub fn new_empty_flac() -> Self

Creates an empty set of tags in the FLAC format.

Source

pub fn new_empty_mp4() -> Self

Creates an empty set of tags in the MP4 format.

Source

pub fn new_empty_opus() -> Self

Creates an empty set of tags in the Opus format.

Source§

impl Tag

Source

pub fn get_album_info(&self) -> Option<Album>

Gets the album information. If the album or album_artist fields are not present in the audio file, this method returns None.

Source

pub fn set_album_info(&mut self, album: Album) -> Result<()>

Sets the album information of the audio track.

§Errors

This function will error if album.cover has an invalid or unsupported MIME type. Supported MIME types are: image/bmp, image/jpeg, image/png

Source

pub fn remove_all_album_info(&mut self)

Removes all album infofrom the audio track.

Source

pub fn title(&self) -> Option<&str>

Gets the title.

Examples found in repository?
examples/read_tag.rs (line 13)
8fn main() {
9    let path = PathBuf::from(args().nth(1).unwrap());
10
11    // Option 1: read from path
12    let tag = Tag::read_from_path(&path).unwrap();
13    println!("{:#?}", tag.title());
14
15    // Option 2: read from reader
16    let mut f = File::open(&path).unwrap();
17
18    let mut data = Vec::new();
19    f.read_to_end(&mut data).unwrap();
20
21    let cursor = Cursor::new(data);
22    // You can also just pass in f instead of creating a cursor since Files are Read + Seek
23    let extension = path.extension().unwrap().to_str().unwrap();
24    let tag = Tag::read_from(extension, cursor).unwrap();
25    println!("{:#?}", tag.title());
26}
Source

pub fn set_title(&mut self, title: &str)

Sets the title.

Examples found in repository?
examples/edit_tag.rs (line 25)
8fn main() {
9    let path = PathBuf::from(args().nth(1).unwrap());
10    let mut file = OpenOptions::new()
11        .read(true)
12        .write(true)
13        // .truncate(true)
14        .open(&path)
15        .unwrap();
16
17    let extension = path.extension().unwrap().to_str().unwrap();
18
19    let mut tag = Tag::read_from(extension, &file).unwrap();
20
21    file.rewind().unwrap();
22
23    let title = args().skip(2).collect::<Vec<String>>().join(" ");
24
25    tag.set_title(&title);
26    tag.write_to_file(&mut file).unwrap();
27}
More examples
Hide additional examples
examples/edit_tag_vec.rs (line 31)
10fn main() {
11    let path = PathBuf::from(args().nth(1).unwrap());
12    let mut file = OpenOptions::new()
13        .read(true)
14        .write(true)
15        // .truncate(true)
16        .open(&path)
17        .unwrap();
18
19    let extension = path.extension().unwrap().to_str().unwrap();
20
21    let mut data = Vec::new();
22    file.read_to_end(&mut data).unwrap();
23    let mut cursor = Cursor::new(&mut data);
24
25    let mut tag = Tag::read_from(extension, &mut cursor).unwrap();
26
27    cursor.rewind().unwrap();
28
29    let title = args().skip(2).collect::<Vec<String>>().join(" ");
30
31    tag.set_title(&title);
32    tag.write_to_vec(&mut data).unwrap();
33
34    file.rewind().unwrap();
35    file.write_all(&data).unwrap();
36}
Source

pub fn remove_title(&mut self)

Removes any title fields from the file.

Source

pub fn artist(&self) -> Option<String>

Gets the artist (note: NOT the album artist!) If multiple ARTIST tags are present, they will be joined with a ;

Source

pub fn set_artist(&mut self, artist: &str)

Sets the artist (note: NOT the album artist!)

Source

pub fn remove_artist(&mut self)

Removes the artist (note: NOT the album artist!)

Source

pub fn date(&self) -> Option<Timestamp>

Gets the date

§Format-specific

In id3, this method corresponds to the date_released field.

Source

pub fn set_date(&mut self, timestamp: Timestamp)

Sets the date

§Format-specific

In id3, this method corresponds to the date_released field.

Source

pub fn remove_date(&mut self)

Removes the date

§Format-specific

In id3, this method corresponds to the date_released field.

Source

pub fn copy_to(&self, other: &mut Self)

Copies the information of this Tag to another. The target Tag can be any of the supported formats.

Auto Trait Implementations§

§

impl Freeze for Tag

§

impl RefUnwindSafe for Tag

§

impl Send for Tag

§

impl Sync for Tag

§

impl Unpin for Tag

§

impl UnwindSafe for Tag

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.