1use derive_more::From;
2use pipe_trait::Pipe;
3use std::{io, path::PathBuf};
4use thiserror::Error;
5
6#[derive(Debug, From, Error)]
7#[error("{0}")]
8pub enum Error {
9 TagReadFailure(TagReadFailure),
10 TagWriteFailure(TagWriteFailure),
11 CommentNotFound(CommentNotFound),
12 AmbiguousCommentChoices(AmbiguousCommentChoices),
13 PictureTypeNotFound(PictureTypeNotFound),
14 PictureNotFound(PictureNotFound),
15 AmbiguousPictureChoices(AmbiguousPictureChoices),
16 DeserializationFailure(DeserializationFailure),
17 PictureFileWriteFailure(PictureFileWriteFailure),
18 OutputDirCreationFailure(OutputDirCreationFailure),
19 InvalidFilePath(InvalidFilePath),
20 FileReadFailure(FileReadFailure),
21 DirCreationFailure(DirCreationFailure),
22 BackupFailure(BackupFailure),
23}
24
25#[derive(Debug, From, Error)]
26#[error("Failed to read tag: {error}")]
27pub struct TagReadFailure {
28 #[source]
29 pub error: id3::Error,
30}
31
32#[derive(Debug, From, Error)]
33#[error("Failed to write tag: {error}")]
34pub struct TagWriteFailure {
35 #[source]
36 pub error: id3::Error,
37}
38
39#[derive(Debug, Error)]
40#[error("Comment not found")]
41pub struct CommentNotFound;
42
43#[derive(Debug, Error)]
44#[error("Too many comments to choose from")]
45pub struct AmbiguousCommentChoices;
46
47#[derive(Debug, Error)]
48#[error("Specified picture type not found")]
49pub struct PictureTypeNotFound;
50
51#[derive(Debug, Error)]
52#[error("Picture not found")]
53pub struct PictureNotFound;
54
55#[derive(Debug, Error)]
56#[error("Too many pictures to choose from")]
57pub struct AmbiguousPictureChoices;
58
59#[derive(Debug, From, Error)]
60#[error("Failed to deserialize: {0}")]
61pub enum DeserializationFailure {
62 Json(serde_json::Error),
63 Yaml(serde_yaml::Error),
64}
65
66#[derive(Debug, From, Error)]
67#[error("Failed to write picture to file: {error}")]
68pub struct PictureFileWriteFailure {
69 #[source]
70 pub error: io::Error,
71}
72
73#[derive(Debug, From, Error)]
74#[error("Failed to create output directory: {error}")]
75pub struct OutputDirCreationFailure {
76 #[source]
77 pub error: io::Error,
78}
79
80#[derive(Debug, From, Error)]
81#[error("Provided path is not a file")]
82pub struct InvalidFilePath;
83
84#[derive(Debug, Error)]
85#[error("Failed to read {file:?}: {error}")]
86pub struct FileReadFailure {
87 pub file: PathBuf,
88 #[source]
89 pub error: io::Error,
90}
91
92#[derive(Debug, Error)]
93#[error("Failed to create a directory at {dir:?}: {error}")]
94pub struct DirCreationFailure {
95 pub dir: PathBuf,
96 #[source]
97 pub error: io::Error,
98}
99
100#[derive(Debug, Error)]
101#[error("Failed to create a backup of {src:?} at {dest:?}: {error}")]
102pub struct BackupFailure {
103 pub src: PathBuf,
104 pub dest: PathBuf,
105 #[source]
106 pub error: io::Error,
107}
108
109macro_rules! indirect_convert {
110 ($source:ty, $intermediate:ident) => {
111 impl From<$source> for Error {
112 fn from(source: $source) -> Self {
113 source.pipe($intermediate::from).into()
114 }
115 }
116 };
117}
118
119indirect_convert!(serde_json::Error, DeserializationFailure);
120indirect_convert!(serde_yaml::Error, DeserializationFailure);