ailloli_ui_fs/error.rs
1//! Serializable, provider-neutral filesystem failures.
2
3use thiserror::Error;
4
5/// Error categories shared by filesystem providers and tree sources.
6///
7/// Payload strings are display-safe context chosen by the provider; this type
8/// intentionally does not retain a platform error source across serialization.
9///
10/// # Examples
11///
12/// ```
13/// use ailloli_ui_fs::FileError;
14/// assert_eq!(FileError::NotFound("file:///missing".into()).to_string(), "file not found: file:///missing");
15/// ```
16#[derive(Debug, Clone, PartialEq, Eq, Error, serde::Serialize, serde::Deserialize)]
17pub enum FileError {
18 /// URI syntax or a URI operation's precondition is invalid.
19 #[error("invalid file uri: {0}")]
20 InvalidUri(String),
21 /// The provider or local conversion does not support the URI scheme/authority.
22 #[error("unsupported file scheme: {0}")]
23 UnsupportedScheme(String),
24 /// The requested resource does not exist.
25 #[error("file not found: {0}")]
26 NotFound(String),
27 /// Host permissions deny the requested operation.
28 #[error("permission denied: {0}")]
29 PermissionDenied(String),
30 /// Creation/rename would collide with an existing resource.
31 #[error("file already exists: {0}")]
32 AlreadyExists(String),
33 /// An operation requiring a directory received another kind.
34 #[error("not a directory: {0}")]
35 NotDirectory(String),
36 /// An operation requiring a non-directory received a directory.
37 #[error("is a directory: {0}")]
38 IsDirectory(String),
39 /// I/O failure without a more specific portable category.
40 #[error("io error: {0}")]
41 Io(String),
42 /// The provider cannot perform the requested operation or entry kind.
43 #[error("unsupported operation: {0}")]
44 Unsupported(String),
45 /// Provider-specific failure not represented by another category.
46 #[error("{0}")]
47 Other(String),
48}
49
50impl FileError {
51 /// Maps a standard I/O error kind to the nearest portable category.
52 ///
53 /// The supplied context becomes the complete payload for mapped categories.
54 /// Unmapped errors become [`Self::Io`] with `"{context}: {error}"`.
55 /// [`std::io::ErrorKind::Unsupported`] maps to [`Self::Unsupported`].
56 ///
57 /// # Examples
58 ///
59 /// ```
60 /// use ailloli_ui_fs::FileError;
61 /// let io = std::io::Error::from(std::io::ErrorKind::NotFound);
62 /// assert_eq!(FileError::from_io(&io, "file:///missing"), FileError::NotFound("file:///missing".into()));
63 /// ```
64 pub fn from_io(error: &std::io::Error, context: impl Into<String>) -> Self {
65 let context = context.into();
66 match error.kind() {
67 std::io::ErrorKind::NotFound => Self::NotFound(context),
68 std::io::ErrorKind::PermissionDenied => Self::PermissionDenied(context),
69 std::io::ErrorKind::AlreadyExists => Self::AlreadyExists(context),
70 std::io::ErrorKind::NotADirectory => Self::NotDirectory(context),
71 std::io::ErrorKind::IsADirectory => Self::IsDirectory(context),
72 std::io::ErrorKind::Unsupported => Self::Unsupported(context),
73 _ => Self::Io(format!("{context}: {error}")),
74 }
75 }
76}