better error handling

This commit is contained in:
Tianyi 2020-10-26 23:19:27 +00:00
parent 276302b607
commit 0f4d6eeab5
5 changed files with 30 additions and 30 deletions

View file

@ -45,7 +45,7 @@ impl Default for FlacTag {
}
impl FlacTag {
pub fn read_from_path(path: impl AsRef<Path>) -> AudioTagsResult<Self> {
pub fn read_from_path(path: impl AsRef<Path>) -> crate::Result<Self> {
Ok(Self {
inner: metaflac::Tag::read_from_path(path)?,
})
@ -216,11 +216,11 @@ impl AudioTagIo for FlacTag {
fn remove_total_discs(&mut self) {
self.remove("TOTALDISCS");
}
fn write_to(&mut self, file: &mut File) -> AudioTagsResult<()> {
fn write_to(&mut self, file: &mut File) -> crate::Result<()> {
self.inner.write_to(file)?;
Ok(())
}
fn write_to_path(&mut self, path: &str) -> AudioTagsResult<()> {
fn write_to_path(&mut self, path: &str) -> crate::Result<()> {
self.inner.write_to_path(path)?;
Ok(())
}

View file

@ -19,7 +19,7 @@ impl Id3v2Tag {
inner: id3::Tag::default(),
}
}
pub fn read_from_path(path: impl AsRef<Path>) -> AudioTagsResult<Self> {
pub fn read_from_path(path: impl AsRef<Path>) -> crate::Result<Self> {
Ok(Self {
inner: id3::Tag::read_from_path(path)?,
})
@ -97,8 +97,8 @@ impl<'a> From<AnyTag<'a>> for id3::Tag {
}
impl<'a> std::convert::TryFrom<&'a id3::frame::Picture> for Picture<'a> {
type Error = crate::AudioTagsError;
fn try_from(inp: &'a id3::frame::Picture) -> AudioTagsResult<Self> {
type Error = crate::crate::Error;
fn try_from(inp: &'a id3::frame::Picture) -> crate::Result<Self> {
let &id3::frame::Picture {
ref mime_type,
ref data,
@ -113,8 +113,8 @@ impl<'a> std::convert::TryFrom<&'a id3::frame::Picture> for Picture<'a> {
}
impl<'a> std::convert::TryFrom<id3::frame::Picture> for Picture<'a> {
type Error = crate::AudioTagsError;
fn try_from(inp: id3::frame::Picture) -> AudioTagsResult<Self> {
type Error = crate::crate::Error;
fn try_from(inp: id3::frame::Picture) -> crate::Result<Self> {
let id3::frame::Picture {
mime_type, data, ..
} = inp;
@ -244,11 +244,11 @@ impl AudioTagIo for Id3v2Tag {
self.inner.remove_total_discs();
}
fn write_to(&mut self, file: &mut File) -> AudioTagsResult<()> {
fn write_to(&mut self, file: &mut File) -> crate::Result<()> {
self.inner.write_to(file, id3::Version::Id3v24)?;
Ok(())
}
fn write_to_path(&mut self, path: &str) -> AudioTagsResult<()> {
fn write_to_path(&mut self, path: &str) -> crate::Result<()> {
self.inner.write_to_path(path, id3::Version::Id3v24)?;
Ok(())
}

View file

@ -48,7 +48,7 @@ pub use flac_tag::FlacTag;
pub use mp4_tag::Mp4Tag;
pub mod utils;
pub use utils::{AudioTagsError, AudioTagsResult};
pub use utils::{Error, Result};
use std::convert::From;
use std::fs::File;
@ -93,12 +93,12 @@ pub enum TagType {
#[rustfmt::skip]
impl TagType {
fn try_from_ext(ext: &str) -> AudioTagsResult<Self> {
fn try_from_ext(ext: &str) -> crate::Result<Self> {
match ext {
"mp3" => Ok(Self::Id3v2),
"m4a" | "m4b" | "m4p" | "m4v" | "isom" | "mp4" => Ok(Self::Mp4),
"flac" => Ok(Self::Flac),
p @ _ => Err(AudioTagsError::UnsupportedFormat(p.to_owned())),
p @ _ => Err(crate::Error::UnsupportedFormat(p.to_owned())),
}
}
}
@ -115,7 +115,7 @@ impl Tag {
}
}
pub fn read_from_path(&self, path: impl AsRef<Path>) -> AudioTagsResult<Box<dyn AudioTagIo>> {
pub fn read_from_path(&self, path: impl AsRef<Path>) -> crate::Result<Box<dyn AudioTagIo>> {
match self.tag_type.unwrap_or(TagType::try_from_ext(
path.as_ref()
.extension()
@ -148,15 +148,15 @@ pub enum MimeType {
}
impl TryFrom<&str> for MimeType {
type Error = AudioTagsError;
fn try_from(inp: &str) -> AudioTagsResult<Self> {
type Error = crate::Error;
fn try_from(inp: &str) -> crate::Result<Self> {
Ok(match inp {
"image/jpeg" => MimeType::Jpeg,
"image/png" => MimeType::Png,
"image/tiff" => MimeType::Tiff,
"image/bmp" => MimeType::Bmp,
"image/gif" => MimeType::Gif,
_ => return Err(AudioTagsError::UnsupportedMimeType(inp.to_owned())),
_ => return Err(crate::Error::UnsupportedMimeType(inp.to_owned())),
})
}
}
@ -278,8 +278,8 @@ impl<'a> AnyTag<'a> {
}
pub trait TagIo {
fn read_from_path(path: &str) -> AudioTagsResult<AnyTag>;
fn write_to_path(path: &str) -> AudioTagsResult<()>;
fn read_from_path(path: &str) -> crate::Result<AnyTag>;
fn write_to_path(path: &str) -> crate::Result<()>;
}
// impl<'a> AnyTag<'a> {
@ -392,9 +392,9 @@ pub trait AudioTagIo {
fn set_total_discs(&mut self, total_discs: u16);
fn remove_total_discs(&mut self);
fn write_to(&mut self, file: &mut File) -> AudioTagsResult<()>;
fn write_to(&mut self, file: &mut File) -> crate::Result<()>;
// cannot use impl AsRef<Path>
fn write_to_path(&mut self, path: &str) -> AudioTagsResult<()>;
fn write_to_path(&mut self, path: &str) -> crate::Result<()>;
fn into_anytag(&self) -> AnyTag<'_>;
}

View file

@ -6,7 +6,7 @@ pub struct Mp4Tag {
}
impl Mp4Tag {
pub fn read_from_path(path: impl AsRef<Path>) -> AudioTagsResult<Self> {
pub fn read_from_path(path: impl AsRef<Path>) -> crate::Result<Self> {
Ok(Self {
inner: mp4ameta::Tag::read_from_path(path)?,
})
@ -63,8 +63,8 @@ impl<'a> From<AnyTag<'a>> for mp4ameta::Tag {
}
impl<'a> std::convert::TryFrom<&'a mp4ameta::Data> for Picture<'a> {
type Error = crate::AudioTagsError;
fn try_from(inp: &'a mp4ameta::Data) -> AudioTagsResult<Self> {
type Error = crate::Error;
fn try_from(inp: &'a mp4ameta::Data) -> crate::Result<Self> {
Ok(match *inp {
mp4ameta::Data::Png(ref data) => Self {
data: Cow::borrowed(data),
@ -74,7 +74,7 @@ impl<'a> std::convert::TryFrom<&'a mp4ameta::Data> for Picture<'a> {
data: Cow::borrowed(data),
mime_type: MimeType::Jpeg,
},
_ => return Err(AudioTagsError::NotAPicture),
_ => return Err(crate::Error::NotAPicture),
})
}
}
@ -206,11 +206,11 @@ impl AudioTagIo for Mp4Tag {
self.inner.remove_total_discs();
}
fn write_to(&mut self, file: &mut File) -> AudioTagsResult<()> {
fn write_to(&mut self, file: &mut File) -> crate::Result<()> {
self.inner.write_to(file)?;
Ok(())
}
fn write_to_path(&mut self, path: &str) -> AudioTagsResult<()> {
fn write_to_path(&mut self, path: &str) -> crate::Result<()> {
self.inner.write_to_path(path)?;
Ok(())
}

View file

@ -2,8 +2,8 @@
use thiserror::Error;
/// Error types that could occur in this library.
#[derive(Error, Debug)]
pub enum AudioTagsError {
#[derive(thiserror::Error, Debug)]
pub enum Error {
/// Fail to guess the metadata format based on the file extension.
#[error("Fail to guess the metadata format based on the file extension.")]
UnknownFileExtension(String),
@ -33,4 +33,4 @@ pub enum AudioTagsError {
Id3TagError(#[from] id3::Error),
}
pub type AudioTagsResult<T> = Result<T, AudioTagsError>;
pub type Result<T> = std::result::Result<T, Error>;