1use alloc::string::String;
2
3pub struct FilePathError {
5 pub path: String,
7 pub playdate: String,
9}
10
11pub struct RenameFilePathError {
13 pub from_path: String,
15 pub to_path: String,
17 pub playdate: String,
19}
20
21pub enum Error {
23 String(String),
25 NotFoundError,
27 FilePathError(FilePathError),
29 RenameFilePathError(RenameFilePathError),
31 LoadMidiFileError,
33 AlreadyAttachedError,
35 DimensionsDoNotMatch,
37 PlayFileError,
39}
40impl From<String> for Error {
41 fn from(s: String) -> Self {
42 Error::String(s)
43 }
44}
45impl From<&str> for Error {
46 fn from(s: &str) -> Self {
47 Error::String(s.into())
48 }
49}
50impl From<&mut str> for Error {
51 fn from(s: &mut str) -> Self {
52 Error::String(s.into())
53 }
54}
55impl From<FilePathError> for Error {
56 fn from(e: FilePathError) -> Self {
57 Error::FilePathError(e)
58 }
59}
60impl From<RenameFilePathError> for Error {
61 fn from(e: RenameFilePathError) -> Self {
62 Error::RenameFilePathError(e)
63 }
64}
65
66impl core::fmt::Debug for FilePathError {
67 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
68 write!(
69 f,
70 "FilePathError(path: \"{}\", playdate: \"{}\")",
71 self.path, self.playdate
72 )
73 }
74}
75impl core::fmt::Debug for RenameFilePathError {
76 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
77 write!(
78 f,
79 "RenameFilePathError(from: \"{}\", to: \"{}\", playdate: \"{}\")",
80 self.from_path, self.to_path, self.playdate
81 )
82 }
83}
84
85impl core::fmt::Debug for Error {
86 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
87 match self {
88 Error::NotFoundError => write!(f, "Error::NotFoundError"),
89 Error::FilePathError(file_err) => write!(f, "Error::FilePathError({:?})", file_err,),
90 Error::RenameFilePathError(rename_err) => {
91 write!(f, "Error::RenameFilePathError({:?})", rename_err)
92 }
93 Error::AlreadyAttachedError => write!(f, "Error::AlreadyAttachedError"),
94 Error::LoadMidiFileError => write!(f, "Error::LoadMidiFileError"),
95 Error::DimensionsDoNotMatch => write!(f, "Error::DimensionsDoNotMatch"),
96 Error::PlayFileError => write!(f, "Error::PlayFileError"),
97 Error::String(e) => write!(f, "Error::String({:?})", e),
98 }
99 }
100}
101
102impl core::fmt::Display for FilePathError {
103 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
104 write!(
105 f,
106 "error operating on the path '{}' (Playdate: {})",
107 self.path, self.playdate
108 )
109 }
110}
111impl core::fmt::Display for RenameFilePathError {
112 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
113 write!(
114 f,
115 "error renaming the path '{}' to '{}' (Playdate: {})",
116 self.from_path, self.to_path, self.playdate
117 )
118 }
119}
120
121impl core::fmt::Display for Error {
122 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
123 match self {
124 Error::NotFoundError => write!(f, "not found"),
125 Error::FilePathError(file_err) => file_err.fmt(f),
126 Error::RenameFilePathError(rename_err) => rename_err.fmt(f),
127 Error::AlreadyAttachedError => write!(f, "already attached"),
128 Error::LoadMidiFileError => write!(f, "MIDI file failed to load"),
129 Error::DimensionsDoNotMatch => write!(f, "dimensions to not match"),
130 Error::PlayFileError => write!(f, "failed to read file to play it as audio"),
131 Error::String(e) => e.fmt(f),
132 }
133 }
134}