Add functions to set/append artists and fix some doc comments.

Signed-off-by: Serial <69764315+Serial-ATA@users.noreply.github.com>
This commit is contained in:
Serial 2021-04-02 21:43:32 -04:00
parent 71fd86af4c
commit 070717fee5
4 changed files with 26 additions and 18 deletions

View file

@ -28,7 +28,15 @@ impl<'a> AnyTag<'a> {
pub fn artists(&self) -> Option<&[&str]> {
self.artists.as_deref()
}
// set_artists; add_artist TODO?
pub fn set_artists(&mut self, artists: Vec<&'a str>) {
self.artists = Some(artists)
}
pub fn add_artist(&mut self, artist: &'a str) {
self.artists = self.artists.clone().map(|mut a| {
a.push(artist);
a
});
}
pub fn year(&self) -> Option<i32> {
self.year
}

View file

@ -179,11 +179,11 @@ impl AudioTagEdit for Id3v2Tag {
}
impl AudioTagWrite for Id3v2Tag {
fn write_to(&mut self, file: &mut File) -> crate::Result<()> {
fn write_to(&mut self, file: &mut File) -> Result<()> {
self.0.write_to(file, id3::Version::Id3v24)?;
Ok(())
}
fn write_to_path(&mut self, path: &str) -> crate::Result<()> {
fn write_to_path(&mut self, path: &str) -> Result<()> {
self.0.write_to_path(path, id3::Version::Id3v24)?;
Ok(())
}

View file

@ -226,11 +226,11 @@ impl AudioTagEdit for Mp4Tag {
}
impl AudioTagWrite for Mp4Tag {
fn write_to(&mut self, file: &mut File) -> crate::Result<()> {
fn write_to(&mut self, file: &mut File) -> Result<()> {
self.0.write_to(file)?;
Ok(())
}
fn write_to_path(&mut self, path: &str) -> crate::Result<()> {
fn write_to_path(&mut self, path: &str) -> Result<()> {
self.0.write_to_path(path)?;
Ok(())
}

View file

@ -75,12 +75,12 @@ pub use std::convert::{TryFrom, TryInto};
/// ```
/// use lofty::{Tag, TagType};
///
/// // Guess the format by default
/// // Guess the format from the extension, in this case `mp3`
/// let mut tag = Tag::new().read_from_path("assets/a.mp3").unwrap();
/// tag.set_title("Foo");
/// // you can convert the tag type and save the metadata to another file.
/// // You can convert the tag type and save the metadata to another file.
/// tag.to_dyn_tag(TagType::Mp4).write_to_path("assets/a.m4a");
/// // you can specify the tag type (but when you want to do this, also consider directly using the concrete type)
/// // You can specify the tag type (but when you want to do this, also consider directly using the concrete type)
/// let tag = Tag::new().with_tag_type(TagType::Mp4).read_from_path("assets/a.m4a").unwrap();
/// assert_eq!(tag.title(), Some("Foo"));
/// ```
@ -89,26 +89,26 @@ pub use std::convert::{TryFrom, TryInto};
pub struct Tag(Option<TagType>);
impl Tag {
/// Initiate a new Tag (a builder for `Box<dyn AudioTag>`) with default configurations.
/// You can then optionally chain `with_tag_type` and/or `with_config`.
/// Finally, you `read_from_path`
/// Initiate a new Tag (a builder for `Box<dyn AudioTag>`).
/// You can then optionally chain `with_tag_type`.
/// Finally, you `read_from_path`.
pub fn new() -> Self {
Self::default()
}
/// Specify the tag type
/// Specify the TagType
pub fn with_tag_type(self, tag_type: TagType) -> Self {
Self(Some(tag_type))
}
/// Path of the file to read
pub fn read_from_path(&self, path: impl AsRef<Path>) -> crate::Result<Box<dyn AudioTag>> {
let extension = path.as_ref().extension().unwrap().to_str().unwrap();
match self.0.unwrap_or(TagType::try_from_ext(extension)?) {
TagType::Id3v2 => Ok(Box::new({ Id3v2Tag::read_from_path(path)? })),
TagType::Vorbis => Ok(Box::new({ VorbisTag::read_from_path(path)? })),
TagType::Opus => Ok(Box::new({ OpusTag::read_from_path(path)? })),
TagType::Flac => Ok(Box::new({ FlacTag::read_from_path(path)? })),
TagType::Mp4 => Ok(Box::new({ Mp4Tag::read_from_path(path)? })),
TagType::Id3v2 => Ok(Box::new(Id3v2Tag::read_from_path(path)?)),
TagType::Vorbis => Ok(Box::new(VorbisTag::read_from_path(path)?)),
TagType::Opus => Ok(Box::new(OpusTag::read_from_path(path)?)),
TagType::Flac => Ok(Box::new(FlacTag::read_from_path(path)?)),
TagType::Mp4 => Ok(Box::new(Mp4Tag::read_from_path(path)?)),
}
}
}