scratch_io/
errors.rs

1pub use crate::itch_api::errors::*;
2
3use std::path::PathBuf;
4use thiserror::Error;
5
6#[derive(Error, Debug)]
7pub enum FilesystemError {
8  #[error(
9    "An IO filesystem error occured!
10{kind}
11{error}"
12  )]
13  IOError {
14    kind: FilesystemIOErrorKind,
15    #[source]
16    error: std::io::Error,
17  },
18
19  #[error(
20    "A filesystem error occured!
21{0}"
22  )]
23  OtherError(#[from] OtherFilesystemErrorKind),
24}
25
26// TODO: This is temporary while more custom errors aren't implemented
27impl From<FilesystemError> for String {
28  fn from(value: FilesystemError) -> Self {
29    value.to_string()
30  }
31}
32
33#[derive(Error, Debug)]
34pub enum FilesystemIOErrorKind {
35  #[error("Couldn't check if the path exists: \"{0}\"")]
36  CouldntCheckIfExists(PathBuf),
37
38  #[error("Couldn't read directory elements: \"{0}\"")]
39  CouldntReadDirectory(PathBuf),
40
41  #[error("Couldn't read directory next element: \"{0}\"")]
42  CouldntReadDirectoryNextEntry(PathBuf),
43
44  #[error("Couldn't get directory element file type: \"{0}\"")]
45  CouldntGetFileType(PathBuf),
46
47  #[error(
48    "Couldn't get the canonical (absolute) form of the path. Maybe it doesn't exist: \"{0}\""
49  )]
50  CouldntGetCanonical(PathBuf),
51
52  #[error("Couldn't create the folder: \"{0}\"")]
53  CouldntCreateDirectory(PathBuf),
54
55  #[error(
56    r#"Couldn't copy file:
57  Source: "{from}"
58  Destination: "{to}""#
59  )]
60  CouldntCopyFile { from: PathBuf, to: PathBuf },
61
62  #[error(
63    r#"Couldn't move file/directory:
64  Source: "{from}"
65  Destination: "{to}""#
66  )]
67  CouldntMove { from: PathBuf, to: PathBuf },
68
69  #[error("Couldn't remove file: \"{0}\"")]
70  CouldntRemoveFile(PathBuf),
71
72  #[error("Couldn't remove a empty folder: \"{0}\"")]
73  CouldntRemoveEmptyDir(PathBuf),
74
75  #[error("Couldn't remove a folder and its contents: \"{0}\"")]
76  CouldntRemoveDirWithContents(PathBuf),
77
78  #[error("Couldn't read the metadata of the path: \"{0}\"")]
79  CouldntReadPathMetadata(PathBuf),
80
81  #[error("Couldn't read the metadata of an open file!")]
82  CouldntReadFileMetadata,
83
84  #[error("Couldn't set the permissions of: \"{0}\"")]
85  CouldntSetPermissions(PathBuf),
86
87  #[error("Couldn't open the file: \"{0}\"")]
88  CouldntOpenFile(PathBuf),
89
90  #[error("Couldn't set file length to: {0}")]
91  SetFileLength(u64),
92
93  #[error("Couldn't sync file data to disk!")]
94  SyncFile,
95
96  #[error("Couldn't fill an async buffer!")]
97  CouldntFillAsyncBuffer,
98
99  #[error("Couldn't write a buffer to an async writer!")]
100  CouldntWriteBuffer,
101
102  #[error("Couldn't spawn the child process!")]
103  CouldnSpawnProcess,
104
105  #[error("Error while awaiting for child exit!")]
106  CouldntWaitForChild,
107}
108
109impl FilesystemIOErrorKind {
110  /// Returns a closure that attaches this [`FilesystemIOErrorKind`] to a [`std::io::Error`] and returns a [`FilesystemError`]
111  pub fn attach(self) -> impl FnOnce(std::io::Error) -> FilesystemError {
112    move |error| FilesystemError::IOError { kind: self, error }
113  }
114}
115
116#[derive(Error, Debug)]
117pub enum OtherFilesystemErrorKind {
118  #[error("The path contains invalid unicode: \"{}\"", .0.to_string_lossy())]
119  InvalidUnicodeOsStr(std::ffi::OsString),
120
121  #[error("The following path doesn't have a filename: \"{0}\"")]
122  PathWithoutFilename(PathBuf),
123
124  #[error("The following path doesn't have an extension: \"{0}\"")]
125  PathWithoutExtension(PathBuf),
126
127  #[error("The following path doesn't have a parent: \"{0}\"")]
128  PathWithoutParent(PathBuf),
129
130  #[error("The following path should be a folder but it is not: \"{0}\"")]
131  ShouldBeAFolder(PathBuf),
132
133  #[error("The following path should be an empty folder or not exist but it does: \"{0}\"")]
134  ShouldBeEmpty(PathBuf),
135
136  #[error("Couldn't determine the home directory")]
137  MissingHomeDirectory,
138
139  #[error("Refusing to remove folder because it is an important path!: \"{0}\"")]
140  RefusingToRemoveFolder(PathBuf),
141}
142
143impl OtherFilesystemErrorKind {
144  /// Returns a closure that moves this [`OtherFilesystemErrorKind`] into a [`FilesystemError`]
145  pub fn attach(self) -> impl FnOnce() -> FilesystemError {
146    move || FilesystemError::OtherError(self)
147  }
148}