VorbisComment

Struct VorbisComment 

Source
pub struct VorbisComment {
    pub vendor_string: String,
    pub fields: Vec<String>,
}
Expand description

A VORBIS_COMMENT metadata block

This block contains metadata such as track name, artist name, album name, etc. Its contents are UTF-8 encoded, =-delimited text fields with a field name followed by value, such as:

TITLE=Track Title

Field names are case-insensitive and may occur multiple times within the same comment (a track may have multiple artists and choose to store an “ARTIST” field for each one).

Commonly-used fields are available in the fields module.

This block may occur only once in a FLAC file.

§Byte Order

Unlike the rest of a FLAC file, the Vorbis comment’s length fields are stored in little-endian byte order.

BitsFieldMeaning
32vendor string lenlength of vendor string, in bytes
vendor string len×8vendor_stringvendor string, in UTF-8
32field countnumber of vendor string fields
32field₀ lenlength of field₀, in bytes
field₀ len×8fields₀first field value, in UTF-8
32field₁ lenlength of field₁, in bytes
field₁ len×8fields₁second field value, in UTF-8

§Example

use bitstream_io::{BitReader, BitRead, LittleEndian};
use flac_codec::metadata::VorbisComment;
use flac_codec::metadata::fields::{TITLE, ALBUM, ARTIST};

let data: &[u8] = &[
    0x20, 0x00, 0x00, 0x00,  // 32 byte vendor string
    0x72, 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63,
    0x65, 0x20, 0x6c, 0x69, 0x62, 0x46, 0x4c, 0x41,
    0x43, 0x20, 0x31, 0x2e, 0x34, 0x2e, 0x33, 0x20,
    0x32, 0x30, 0x32, 0x33, 0x30, 0x36, 0x32, 0x33,
    0x02, 0x00, 0x00, 0x00,  // 2 fields
    0x0d, 0x00, 0x00, 0x00,  // 13 byte field 1
    0x54, 0x49, 0x54, 0x4c, 0x45, 0x3d, 0x54, 0x65,
    0x73, 0x74, 0x69, 0x6e, 0x67,
    0x10, 0x00, 0x00, 0x00,  // 16 byte field 2
    0x41, 0x4c, 0x42, 0x55, 0x4d, 0x3d, 0x54, 0x65,
    0x73, 0x74, 0x20, 0x41, 0x6c, 0x62, 0x75, 0x6d,
];

let mut r = BitReader::endian(data, LittleEndian);
let comment = r.parse::<VorbisComment>().unwrap();

assert_eq!(
    &comment,
    &VorbisComment {
        vendor_string: "reference libFLAC 1.4.3 20230623".to_string(),
        fields: vec![
             "TITLE=Testing".to_string(),
             "ALBUM=Test Album".to_string(),
        ],
    },
);

assert_eq!(comment.get(TITLE), Some("Testing"));
assert_eq!(comment.get(ALBUM), Some("Test Album"));
assert_eq!(comment.get(ARTIST), None);

Fields§

§vendor_string: String

The vendor string

§fields: Vec<String>

The individual metadata comment strings

Implementations§

Source§

impl VorbisComment

Source

pub fn get(&self, field: &str) -> Option<&str>

Given a field name, returns first matching value, if any

Fields are matched case-insensitively

§Example
use flac_codec::metadata::{VorbisComment, fields::{ARTIST, TITLE}};

let comment = VorbisComment {
    fields: vec![
        "ARTIST=Artist 1".to_owned(),
        "ARTIST=Artist 2".to_owned(),
    ],
    ..VorbisComment::default()
};

assert_eq!(comment.get(ARTIST), Some("Artist 1"));
assert_eq!(comment.get(TITLE), None);
Source

pub fn set<S>(&mut self, field: &str, value: S)
where S: Display,

Replaces any instances of the given field with value

Fields are matched case-insensitively

§Panics

Panics if field contains the = character.

§Example
use flac_codec::metadata::{VorbisComment, fields::ARTIST};

let mut comment = VorbisComment {
    fields: vec![
        "ARTIST=Artist 1".to_owned(),
        "ARTIST=Artist 2".to_owned(),
    ],
    ..VorbisComment::default()
};

comment.set(ARTIST, "Artist 3");

assert_eq!(
    comment.all(ARTIST).collect::<Vec<_>>(),
    vec!["Artist 3"],
);
Source

pub fn all(&self, field: &str) -> impl Iterator<Item = &str>

Given a field name, iterates over any matching values

Fields are matched case-insensitively

§Example
use flac_codec::metadata::{VorbisComment, fields::ARTIST};

let comment = VorbisComment {
    fields: vec![
        "ARTIST=Artist 1".to_owned(),
        "ARTIST=Artist 2".to_owned(),
    ],
    ..VorbisComment::default()
};

assert_eq!(
    comment.all(ARTIST).collect::<Vec<_>>(),
    vec!["Artist 1", "Artist 2"],
);
Source

pub fn insert<S>(&mut self, field: &str, value: S)
where S: Display,

Adds new instance of field with the given value

§Panics

Panics if field contains the = character.

§Example
use flac_codec::metadata::{VorbisComment, fields::ARTIST};

let mut comment = VorbisComment {
    fields: vec![
        "ARTIST=Artist 1".to_owned(),
        "ARTIST=Artist 2".to_owned(),
    ],
    ..VorbisComment::default()
};

comment.insert(ARTIST, "Artist 3");

assert_eq!(
    comment.all(ARTIST).collect::<Vec<_>>(),
    vec!["Artist 1", "Artist 2", "Artist 3"],
);
Source

pub fn remove(&mut self, field: &str)

Removes any matching instances of the given field

Fields are matched case-insensitively

§Panics

Panics if field contains the = character.

§Example
use flac_codec::metadata::{VorbisComment, fields::ARTIST};

let mut comment = VorbisComment {
    fields: vec![
        "ARTIST=Artist 1".to_owned(),
        "ARTIST=Artist 2".to_owned(),
    ],
    ..VorbisComment::default()
};

comment.remove(ARTIST);

assert_eq!(comment.get(ARTIST), None);
Source

pub fn replace<S: Display>( &mut self, field: &str, replacements: impl IntoIterator<Item = S>, )

Replaces any instances of the given field with the given values

Fields are matched case-insensitively

§Panics

Panics if field contains the = character

§Example
use flac_codec::metadata::{VorbisComment, fields::ARTIST};

let mut comment = VorbisComment {
    fields: vec![
        "ARTIST=Artist 1".to_owned(),
        "ARTIST=Artist 2".to_owned(),
    ],
    ..VorbisComment::default()
};

comment.replace(ARTIST, ["Artist 3", "Artist 4"]);

assert_eq!(
    comment.all(ARTIST).collect::<Vec<_>>(),
    vec!["Artist 3", "Artist 4"],
);

// reminder that Option also implements IntoIterator
comment.replace(ARTIST, Some("Artist 5"));

assert_eq!(
    comment.all(ARTIST).collect::<Vec<_>>(),
    vec!["Artist 5"],
);
Source

pub fn replace_with<S: Display>( &mut self, field: &str, f: impl FnMut(&str) -> S, )

Replaces instances of the given field with a closure’s result

Closure takes the field’s current value and returns something Display-able such as a String.

The closure is called for each maching field, if any.

Fields are matched case-insensitively.

§Panics

Panics if field contains the = character

§Example
use flac_codec::metadata::{VorbisComment, fields::ARTIST};

let mut comment = VorbisComment {
    fields: vec![
        "ARTIST=some artist".to_owned(),
    ],
    ..VorbisComment::default()
};

comment.replace_with(ARTIST, |s| s.to_ascii_uppercase());

assert_eq!(comment.get(ARTIST), Some("SOME ARTIST"));

Trait Implementations§

Source§

impl AsBlockRef for VorbisComment

Source§

fn as_block_ref(&self) -> BlockRef<'_>

Returns fresh reference to ourself.
Source§

impl Clone for VorbisComment

Source§

fn clone(&self) -> VorbisComment

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for VorbisComment

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for VorbisComment

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<VorbisComment> for Block

Source§

fn from(b: VorbisComment) -> Self

Converts to this type from the input type.
Source§

impl FromBitStream for VorbisComment

Source§

type Error = Error

Error generated during parsing, such as io::Error
Source§

fn from_reader<R: BitRead + ?Sized>(r: &mut R) -> Result<Self, Self::Error>

Parse Self from reader
Source§

impl MetadataBlock for VorbisComment

Source§

const TYPE: BlockType = BlockType::VorbisComment

The metadata block’s type
Source§

const MULTIPLE: bool = false

Whether the block can occur multiple times in a file
Source§

fn bytes(&self) -> Option<BlockSize>

Size of block, in bytes, not including header
Source§

fn total_size(&self) -> Option<BlockSize>

Size of block, in bytes, including block header
Source§

impl OptionalMetadataBlock for VorbisComment

Source§

const OPTIONAL_TYPE: OptionalBlockType = OptionalBlockType::VorbisComment

Our optional block type
Source§

impl PartialEq for VorbisComment

Source§

fn eq(&self, other: &VorbisComment) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl ToBitStream for VorbisComment

Source§

type Error = Error

Error generated during building, such as io::Error
Source§

fn to_writer<W: BitWrite + ?Sized>(&self, w: &mut W) -> Result<(), Self::Error>

Generate self to writer
Source§

fn bits<C>(&self) -> Result<C, Self::Error>
where C: Counter, Self: Sized,

Returns length of self in bits, if possible
Source§

fn bits_len<C, E>(&self) -> Result<C, Self::Error>
where C: Counter, E: Endianness, Self: Sized,

👎Deprecated since 4.0.0: use of bits() is preferred
Returns total length of self, if possible
Source§

impl TryFrom<Block> for VorbisComment

Source§

type Error = ()

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

fn try_from(block: Block) -> Result<Self, ()>

Performs the conversion.
Source§

impl Eq for VorbisComment

Source§

impl PortableMetadataBlock for VorbisComment

Source§

impl StructuralPartialEq for VorbisComment

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.