craydate/
error.rs

1use alloc::string::String;
2
3/// An error performing an operation on a filesystem path.
4pub struct FilePathError {
5  /// The path of the file operation.
6  pub path: String,
7  /// The error string reported from Playdate.
8  pub playdate: String,
9}
10
11/// An error when trying to rename a file or folder, which comes with additional context.
12pub struct RenameFilePathError {
13  /// The path of the file being renamed.
14  pub from_path: String,
15  /// The path the file was meant to be moved to.
16  pub to_path: String,
17  /// The error string reported from Playdate.
18  pub playdate: String,
19}
20
21/// The Error type for all errors in the craydate crate.
22pub enum Error {
23  /// A general error which is described by the contained string.
24  String(String),
25  /// Indicates a file or resource was not found.
26  NotFoundError,
27  /// An error when trying to use a path, which comes with additional context.
28  FilePathError(FilePathError),
29  /// An error when trying to rename a file, which comes with additional context.
30  RenameFilePathError(RenameFilePathError),
31  /// Attempting to load a MIDI file was unsuccessful.
32  LoadMidiFileError,
33  /// A SoundChannel or SoundSource was already attached and can not be attached again.
34  AlreadyAttachedError,
35  /// Bitmap dimentions are required to match but they failed to.
36  DimensionsDoNotMatch,
37  /// An error occured trying to read from a file to play it as audio.
38  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}