fs_more/error/
file.rs

1use std::path::PathBuf;
2
3use thiserror::Error;
4
5
6/// An error that can occur when copying or moving a file.
7#[derive(Error, Debug)]
8pub enum FileError {
9    /// The provided source file path does not exist.
10    #[error("source file does not exist: {}", .path.display())]
11    SourceFileNotFound {
12        /// The path that does not exist.
13        path: PathBuf,
14    },
15
16    /// The provided source file path exists, but is not a file.
17    #[error("source path exists, but is not a file: {}", .path.display())]
18    SourcePathNotAFile {
19        /// The path that exists, but is not a file.
20        path: PathBuf,
21    },
22
23    /// The source file cannot be accessed or canonicalized, for example due to missing permissions.
24    ///
25    /// The inner [`std::io::Error`] will likely describe the real cause of this error.
26    #[error("unable to access source file: {}", .path.display())]
27    UnableToAccessSourceFile {
28        /// File path that could not be accessed.
29        path: PathBuf,
30
31        /// Underlying IO error describing why the source file could not be accessed.
32        #[source]
33        error: std::io::Error,
34    },
35
36    /// The destination file already exists.
37    ///
38    /// Certain file copy and move options can disable this error:
39    /// - [`FileCopyOptions.colliding_file_behaviour`],
40    /// - [`FileCopyWithProgressOptions.colliding_file_behaviour`],
41    /// - [`FileMoveOptions.colliding_file_behaviour`], and
42    /// - [`FileMoveWithProgressOptions.colliding_file_behaviour`].
43    ///
44    ///
45    /// [`FileCopyOptions.colliding_file_behaviour`]: crate::file::FileCopyOptions::colliding_file_behaviour
46    /// [`FileCopyWithProgressOptions.colliding_file_behaviour`]: crate::file::FileCopyWithProgressOptions::colliding_file_behaviour
47    /// [`FileMoveOptions.colliding_file_behaviour`]: crate::file::FileMoveOptions::colliding_file_behaviour
48    /// [`FileMoveWithProgressOptions.colliding_file_behaviour`]: crate::file::FileMoveWithProgressOptions::colliding_file_behaviour
49    #[error("destination path already exists: {}", .path.display())]
50    DestinationPathAlreadyExists {
51        /// Destination file path that already exists.
52        path: PathBuf,
53    },
54
55    /// The destination file cannot be accessed, written to, or its path canonicalized,
56    /// for example due to missing permissions.
57    ///
58    /// The inner [`std::io::Error`] will likely describe the real cause of this error.
59    #[error("unable to access destination file: {}", .path.display())]
60    UnableToAccessDestinationFile {
61        /// Destination file path that could not be accessed.
62        path: PathBuf,
63
64        /// Underlying IO error describing why the destination file could not be accessed.
65        #[source]
66        error: std::io::Error,
67    },
68
69    /// The source and destination file paths point to the same canonical file.
70    #[error("source and destination file path are the same file: {}", .path.display())]
71    SourceAndDestinationAreTheSame {
72        /// The conflicting source and destination path.
73        path: PathBuf,
74    },
75
76    /// Some other [`std::io::Error`] was encountered.
77    #[error("uncategorized std::io::Error")]
78    OtherIoError {
79        /// IO error describing the cause of the outer error.
80        #[source]
81        error: std::io::Error,
82    },
83}
84
85
86/// An error that can occur when removing a file.
87#[derive(Error, Debug)]
88pub enum FileRemoveError {
89    /// The provided source file path does not exist.
90    #[error("source file does not exist: {}", .path.display())]
91    NotFound {
92        /// The path that does not exist.
93        path: PathBuf,
94    },
95
96    /// The provided source file path exists, but is not a file.
97    #[error("source path exists, but is not a file: {}", .path.display())]
98    NotAFile {
99        /// The path that exists, but is not a file.
100        path: PathBuf,
101    },
102
103    /// The file cannot be accessed, for example due to missing permissions.
104    ///
105    /// The inner [`std::io::Error`] will likely describe the real cause of this error.
106    #[error("unable to access file: {}", .path.display())]
107    UnableToAccessFile {
108        /// Path to the file that could not be accessed.
109        path: PathBuf,
110
111        /// Underlying IO error describing why the file could not be accessed.
112        #[source]
113        error: std::io::Error,
114    },
115
116    /// Uncategorized IO error.
117    ///
118    /// The inner [`std::io::Error`] will likely describe the real cause of this error.
119    #[error("uncategorized IO error")]
120    OtherIoError {
121        /// IO error describing the cause of the error.
122        #[source]
123        error: std::io::Error,
124    },
125}
126
127/// An error that can occur when querying the size of a file.
128#[derive(Error, Debug)]
129pub enum FileSizeError {
130    /// The source file does not exist.
131    #[error("file does not exist: {}", .path.display())]
132    NotFound {
133        /// Path to the file that does not exist.
134        path: PathBuf,
135    },
136
137    /// The source path exists, but is not a file nor a symbolic link to one.
138    #[error("provided path exists, but is not a file nor a symbolic link to one: {}", .path.display())]
139    NotAFile {
140        /// Path that exists, but is not a file.
141        path: PathBuf,
142    },
143
144    /// The file cannot be accessed, for example due to missing permissions.
145    ///
146    /// The inner [`std::io::Error`] will likely describe the real cause of this error.
147    #[error("unable to access file: {}", .file_path.display())]
148    UnableToAccessFile {
149        /// Path to the file that could not be accessed.
150        file_path: PathBuf,
151
152        /// Underlying IO error describing why the file could not be accessed.
153        #[source]
154        error: std::io::Error,
155    },
156
157    /// Uncategorized IO error.
158    ///
159    /// The inner [`std::io::Error`] will likely describe the real cause of this error.
160    #[error("uncategorized IO error")]
161    OtherIoError {
162        /// IO error describing the cause of the error.
163        #[source]
164        error: std::io::Error,
165    },
166}