From a1b7fb7e16708782c8bb9888a6f35cea915a8983 Mon Sep 17 00:00:00 2001 From: Serial <69764315+Serial-ATA@users.noreply.github.com> Date: Sat, 15 Apr 2023 20:27:59 -0400 Subject: [PATCH] TagExt: Add a default impl for `save_to_path` --- src/ape/tag/mod.rs | 12 +----------- src/id3/v1/tag.rs | 6 +----- src/id3/v2/tag.rs | 6 +----- src/iff/aiff/tag.rs | 12 +----------- src/iff/wav/tag/mod.rs | 6 +----- src/ogg/tag.rs | 12 +----------- src/tag/mod.rs | 11 ----------- src/traits.rs | 11 +++++++++-- 8 files changed, 15 insertions(+), 61 deletions(-) diff --git a/src/ape/tag/mod.rs b/src/ape/tag/mod.rs index 973df671..51661de7 100644 --- a/src/ape/tag/mod.rs +++ b/src/ape/tag/mod.rs @@ -10,7 +10,7 @@ use crate::traits::{Accessor, MergeTag, SplitTag, TagExt}; use std::borrow::Cow; use std::convert::TryInto; -use std::fs::{File, OpenOptions}; +use std::fs::File; use std::io::Write; use std::ops::Deref; use std::path::Path; @@ -289,16 +289,6 @@ impl TagExt for ApeTag { self.items.is_empty() } - /// Writes the tag to a path - /// - /// # Errors - /// - /// * `path` does not exist - /// * See [`ApeTag::save_to`] - fn save_to_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 /// /// # Errors diff --git a/src/id3/v1/tag.rs b/src/id3/v1/tag.rs index e671dc55..3e4c795e 100644 --- a/src/id3/v1/tag.rs +++ b/src/id3/v1/tag.rs @@ -5,7 +5,7 @@ use crate::tag::{Tag, TagType}; use crate::traits::{Accessor, MergeTag, SplitTag, TagExt}; use std::borrow::Cow; -use std::fs::{File, OpenOptions}; +use std::fs::File; use std::io::Write; use std::path::Path; @@ -225,10 +225,6 @@ impl TagExt for ID3v1Tag { && self.genre.is_none() } - fn save_to_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> { Into::>::into(self).write_to(file) } diff --git a/src/id3/v2/tag.rs b/src/id3/v2/tag.rs index 2aacf145..f829ebfb 100644 --- a/src/id3/v2/tag.rs +++ b/src/id3/v2/tag.rs @@ -17,7 +17,7 @@ use crate::util::text::{decode_text, TextEncoding}; use std::borrow::Cow; use std::convert::TryInto; use std::fmt::Display; -use std::fs::{File, OpenOptions}; +use std::fs::File; use std::io::{Cursor, Write}; use std::ops::Deref; use std::path::Path; @@ -574,10 +574,6 @@ impl TagExt for ID3v2Tag { self.frames.is_empty() } - fn save_to_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 /// /// # Errors diff --git a/src/iff/aiff/tag.rs b/src/iff/aiff/tag.rs index 11ed9c81..7360d02b 100644 --- a/src/iff/aiff/tag.rs +++ b/src/iff/aiff/tag.rs @@ -7,7 +7,7 @@ use crate::traits::{Accessor, MergeTag, SplitTag, TagExt}; use std::borrow::Cow; use std::convert::TryFrom; -use std::fs::{File, OpenOptions}; +use std::fs::File; use std::io::{Read, Seek, SeekFrom, Write}; 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>(&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> { AiffTextChunksRef { name: self.name.as_deref(), diff --git a/src/iff/wav/tag/mod.rs b/src/iff/wav/tag/mod.rs index 8cc0ff51..8b8342d5 100644 --- a/src/iff/wav/tag/mod.rs +++ b/src/iff/wav/tag/mod.rs @@ -7,7 +7,7 @@ use crate::tag::{try_parse_year, Tag, TagType}; use crate::traits::{Accessor, MergeTag, SplitTag, TagExt}; use std::borrow::Cow; -use std::fs::{File, OpenOptions}; +use std::fs::File; use std::io::Write; use std::path::Path; @@ -199,10 +199,6 @@ impl TagExt for RIFFInfoList { self.items.is_empty() } - fn save_to_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> { RIFFInfoListRef::new(self.items.iter().map(|(k, v)| (k.as_str(), v.as_str()))) .write_to(file) diff --git a/src/ogg/tag.rs b/src/ogg/tag.rs index 2c54e60e..cbc4ffc9 100644 --- a/src/ogg/tag.rs +++ b/src/ogg/tag.rs @@ -10,7 +10,7 @@ use crate::tag::{try_parse_year, Tag, TagType}; use crate::traits::{Accessor, MergeTag, SplitTag, TagExt}; use std::borrow::Cow; -use std::fs::{File, OpenOptions}; +use std::fs::File; use std::io::Write; use std::ops::Deref; use std::path::Path; @@ -339,16 +339,6 @@ impl TagExt for VorbisComments { 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>(&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 /// /// # Errors diff --git a/src/tag/mod.rs b/src/tag/mod.rs index f964c27e..3d14ef7e 100644 --- a/src/tag/mod.rs +++ b/src/tag/mod.rs @@ -537,17 +537,6 @@ impl TagExt for Tag { 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>(&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) /// /// # Errors diff --git a/src/traits.rs b/src/traits.rs index f5e8055e..53e6eeb7 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -132,7 +132,7 @@ use std::path::Path; /// This can be implemented downstream to provide a familiar interface for custom tags. pub trait TagExt: Accessor + Into + Sized { /// The associated error which can be returned from IO operations - type Err; + type Err: From; /// The type of key used in the tag for non-mutating functions type RefKey<'a> where @@ -195,7 +195,14 @@ pub trait TagExt: Accessor + Into + Sized { /// * Path doesn't exist /// * Path is not writable /// * See [`TagExt::save_to`] - fn save_to_path>(&self, path: P) -> std::result::Result<(), Self::Err>; + fn save_to_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`] ///