TagExt: Add a default impl for save_to_path

This commit is contained in:
Serial 2023-04-15 20:27:59 -04:00
parent acb7b2482c
commit a1b7fb7e16
No known key found for this signature in database
GPG key ID: DA95198DC17C4568
8 changed files with 15 additions and 61 deletions

View file

@ -10,7 +10,7 @@ use crate::traits::{Accessor, MergeTag, SplitTag, TagExt};
use std::borrow::Cow; use std::borrow::Cow;
use std::convert::TryInto; use std::convert::TryInto;
use std::fs::{File, OpenOptions}; use std::fs::File;
use std::io::Write; use std::io::Write;
use std::ops::Deref; use std::ops::Deref;
use std::path::Path; use std::path::Path;
@ -289,16 +289,6 @@ impl TagExt for ApeTag {
self.items.is_empty() self.items.is_empty()
} }
/// Writes the tag to a path
///
/// # Errors
///
/// * `path` does not exist
/// * See [`ApeTag::save_to`]
fn save_to_path<P: AsRef<Path>>(&self, path: P) -> std::result::Result<(), Self::Err> {
self.save_to(&mut OpenOptions::new().read(true).write(true).open(path)?)
}
/// Write an `APE` tag to a file /// Write an `APE` tag to a file
/// ///
/// # Errors /// # Errors

View file

@ -5,7 +5,7 @@ use crate::tag::{Tag, TagType};
use crate::traits::{Accessor, MergeTag, SplitTag, TagExt}; use crate::traits::{Accessor, MergeTag, SplitTag, TagExt};
use std::borrow::Cow; use std::borrow::Cow;
use std::fs::{File, OpenOptions}; use std::fs::File;
use std::io::Write; use std::io::Write;
use std::path::Path; use std::path::Path;
@ -225,10 +225,6 @@ impl TagExt for ID3v1Tag {
&& self.genre.is_none() && self.genre.is_none()
} }
fn save_to_path<P: AsRef<Path>>(&self, path: P) -> std::result::Result<(), Self::Err> {
self.save_to(&mut OpenOptions::new().read(true).write(true).open(path)?)
}
fn save_to(&self, file: &mut File) -> std::result::Result<(), Self::Err> { fn save_to(&self, file: &mut File) -> std::result::Result<(), Self::Err> {
Into::<Id3v1TagRef<'_>>::into(self).write_to(file) Into::<Id3v1TagRef<'_>>::into(self).write_to(file)
} }

View file

@ -17,7 +17,7 @@ use crate::util::text::{decode_text, TextEncoding};
use std::borrow::Cow; use std::borrow::Cow;
use std::convert::TryInto; use std::convert::TryInto;
use std::fmt::Display; use std::fmt::Display;
use std::fs::{File, OpenOptions}; use std::fs::File;
use std::io::{Cursor, Write}; use std::io::{Cursor, Write};
use std::ops::Deref; use std::ops::Deref;
use std::path::Path; use std::path::Path;
@ -574,10 +574,6 @@ impl TagExt for ID3v2Tag {
self.frames.is_empty() self.frames.is_empty()
} }
fn save_to_path<P: AsRef<Path>>(&self, path: P) -> std::result::Result<(), Self::Err> {
self.save_to(&mut OpenOptions::new().read(true).write(true).open(path)?)
}
/// Writes the tag to a file /// Writes the tag to a file
/// ///
/// # Errors /// # Errors

View file

@ -7,7 +7,7 @@ use crate::traits::{Accessor, MergeTag, SplitTag, TagExt};
use std::borrow::Cow; use std::borrow::Cow;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::fs::{File, OpenOptions}; use std::fs::File;
use std::io::{Read, Seek, SeekFrom, Write}; use std::io::{Read, Seek, SeekFrom, Write};
use std::path::Path; use std::path::Path;
@ -186,16 +186,6 @@ impl TagExt for AIFFTextChunks {
) )
} }
/// Writes the tag to a path
///
/// # Errors
///
/// * `path` does not exist
/// * See [`AIFFTextChunks::save_to`]
fn save_to_path<P: AsRef<Path>>(&self, path: P) -> std::result::Result<(), Self::Err> {
self.save_to(&mut OpenOptions::new().read(true).write(true).open(path)?)
}
fn save_to(&self, file: &mut File) -> std::result::Result<(), Self::Err> { fn save_to(&self, file: &mut File) -> std::result::Result<(), Self::Err> {
AiffTextChunksRef { AiffTextChunksRef {
name: self.name.as_deref(), name: self.name.as_deref(),

View file

@ -7,7 +7,7 @@ use crate::tag::{try_parse_year, Tag, TagType};
use crate::traits::{Accessor, MergeTag, SplitTag, TagExt}; use crate::traits::{Accessor, MergeTag, SplitTag, TagExt};
use std::borrow::Cow; use std::borrow::Cow;
use std::fs::{File, OpenOptions}; use std::fs::File;
use std::io::Write; use std::io::Write;
use std::path::Path; use std::path::Path;
@ -199,10 +199,6 @@ impl TagExt for RIFFInfoList {
self.items.is_empty() self.items.is_empty()
} }
fn save_to_path<P: AsRef<Path>>(&self, path: P) -> std::result::Result<(), Self::Err> {
self.save_to(&mut OpenOptions::new().read(true).write(true).open(path)?)
}
fn save_to(&self, file: &mut File) -> std::result::Result<(), Self::Err> { fn save_to(&self, file: &mut File) -> std::result::Result<(), Self::Err> {
RIFFInfoListRef::new(self.items.iter().map(|(k, v)| (k.as_str(), v.as_str()))) RIFFInfoListRef::new(self.items.iter().map(|(k, v)| (k.as_str(), v.as_str())))
.write_to(file) .write_to(file)

View file

@ -10,7 +10,7 @@ use crate::tag::{try_parse_year, Tag, TagType};
use crate::traits::{Accessor, MergeTag, SplitTag, TagExt}; use crate::traits::{Accessor, MergeTag, SplitTag, TagExt};
use std::borrow::Cow; use std::borrow::Cow;
use std::fs::{File, OpenOptions}; use std::fs::File;
use std::io::Write; use std::io::Write;
use std::ops::Deref; use std::ops::Deref;
use std::path::Path; use std::path::Path;
@ -339,16 +339,6 @@ impl TagExt for VorbisComments {
self.items.is_empty() && self.pictures.is_empty() self.items.is_empty() && self.pictures.is_empty()
} }
/// Writes the tag to a path
///
/// # Errors
///
/// * `path` does not exist
/// * See [`VorbisComments::save_to`]
fn save_to_path<P: AsRef<Path>>(&self, path: P) -> std::result::Result<(), Self::Err> {
self.save_to(&mut OpenOptions::new().read(true).write(true).open(path)?)
}
/// Writes the tag to a file /// Writes the tag to a file
/// ///
/// # Errors /// # Errors

View file

@ -537,17 +537,6 @@ impl TagExt for Tag {
self.items.is_empty() && self.pictures.is_empty() self.items.is_empty() && self.pictures.is_empty()
} }
/// Save the `Tag` to a path
///
/// # Errors
///
/// * Path doesn't exist
/// * Path is not writable
/// * See [`Tag::save_to`]
fn save_to_path<P: AsRef<Path>>(&self, path: P) -> std::result::Result<(), Self::Err> {
self.save_to(&mut OpenOptions::new().read(true).write(true).open(path)?)
}
/// Save the `Tag` to a [`File`](std::fs::File) /// Save the `Tag` to a [`File`](std::fs::File)
/// ///
/// # Errors /// # Errors

View file

@ -132,7 +132,7 @@ use std::path::Path;
/// This can be implemented downstream to provide a familiar interface for custom tags. /// This can be implemented downstream to provide a familiar interface for custom tags.
pub trait TagExt: Accessor + Into<Tag> + Sized { pub trait TagExt: Accessor + Into<Tag> + Sized {
/// The associated error which can be returned from IO operations /// The associated error which can be returned from IO operations
type Err; type Err: From<std::io::Error>;
/// The type of key used in the tag for non-mutating functions /// The type of key used in the tag for non-mutating functions
type RefKey<'a> type RefKey<'a>
where where
@ -195,7 +195,14 @@ pub trait TagExt: Accessor + Into<Tag> + Sized {
/// * Path doesn't exist /// * Path doesn't exist
/// * Path is not writable /// * Path is not writable
/// * See [`TagExt::save_to`] /// * See [`TagExt::save_to`]
fn save_to_path<P: AsRef<Path>>(&self, path: P) -> std::result::Result<(), Self::Err>; fn save_to_path<P: AsRef<Path>>(&self, path: P) -> std::result::Result<(), Self::Err> {
self.save_to(
&mut std::fs::OpenOptions::new()
.read(true)
.write(true)
.open(path)?,
)
}
/// Save the tag to a [`File`] /// Save the tag to a [`File`]
/// ///