simple_fatfs/
error.rs

1use bincode::error::{DecodeError, EncodeError};
2use embedded_io::*;
3
4use crate::*;
5
6/// An error type that denotes that there is something wrong
7/// with the filesystem's structure itself (perhaps the FS itself is malformed/corrupted)
8#[non_exhaustive]
9#[derive(Debug)]
10pub enum InternalFSError {
11    /// The storage medium isn't large enough to accompany a FAT filesystem
12    StorageTooSmall,
13    /// Invalid boot sector signature. Perhaps this isn't a FAT filesystem?
14    InvalidBPBSig,
15    /**
16     Invalid FAT32 FSInfo signature.
17     Perhaps the FSInfo structure or the FAT32 Ebr's fat_info field is malformed?
18    */
19    InvalidFSInfoSig,
20    /**
21     The FAT and it's copies do not much.
22     This is either the result of some bad FAT library that chose to ignore the FAT copies
23     or perhaps the storage medium has been corrupted (most likely).
24     Either way, we are not handling this FileSystem
25    */
26    MismatchingFATTables,
27    /// Encountered a malformed cluster chain
28    MalformedClusterChain,
29    /// Encountered a malformed directory entry chain
30    MalformedEntryChain,
31}
32
33/// An error indicating that a filesystem-related operation has failed
34#[non_exhaustive]
35#[derive(Debug)]
36pub enum FSError<I>
37where
38    I: Error,
39{
40    /// An internal FS error occured
41    InternalFSError(InternalFSError),
42    /**
43     The [Path](`crate::Path`) provided is malformed.
44
45     This usually means that a path you provided isn't a valid [`Utf8WindowsPath`](typed_path::Utf8WindowsPath)
46
47     If you are 100% that your path is valid (`path.is_valid()`), then perhaps you have encountered a bug.
48     File a bug report here: <https://github.com/Oakchris1955/simple-fatfs/issues>
49    */
50    MalformedPath,
51    /**
52     [`bincode`] errored out while (de)serializing
53
54     This error variant should NEVER be raised.
55     If you get this error, open an issue: <https://github.com/Oakchris1955/simple-fatfs/issues>
56    */
57    BincodeError(BincodeError),
58    /// Expected a directory
59    NotADirectory,
60    /// Found a directory when we expected a file
61    IsADirectory,
62    /// Expected an empty directory
63    DirectoryNotEmpty,
64    /// This file cannot be modified, as it is read-only
65    ReadOnlyFile,
66    /// A file or directory wasn't found
67    NotFound,
68    /// An entity already exists
69    AlreadyExists,
70    /// The operation lacked the necessary privileges to complete.
71    PermissionDenied,
72    /// A parameter was incorrect.
73    InvalidInput,
74    /// The underlying storage is full.
75    StorageFull,
76    /**
77     There aren't enough free entries on the root directory to perform
78     this operation. Consider performing this operation on a subdirectory instead
79    */
80    RootDirectoryFull,
81    /**
82     The entry limit for this directory (2^16 - 1) has been reached.
83     Consider performing this operation on a subdirectory instead
84    */
85    DirEntryLimitReached,
86    /**
87     The filesystem provided is not supported (e.g. ExFAT).
88    */
89    UnsupportedFS,
90    /// Unexpected EOF
91    UnexpectedEof,
92    /// An IO error occured
93    IOError(I),
94}
95
96/// An encode/decode-related error
97///
98/// This error enum should NEVER be raised.
99/// If you get it, file an issue: <https://github.com/Oakchris1955/simple-fatfs/issues>
100#[derive(Debug)]
101pub enum BincodeError {
102    /// A decode-related error
103    DecodeError(DecodeError),
104    /// An encode-related error
105    EncodeError(EncodeError),
106}
107
108impl<I> From<I> for FSError<I>
109where
110    I: Error,
111{
112    #[inline]
113    fn from(value: I) -> Self {
114        FSError::IOError(value)
115    }
116}
117
118impl<I> From<ReadExactError<I>> for FSError<I>
119where
120    I: Error,
121{
122    #[inline]
123    fn from(value: ReadExactError<I>) -> Self {
124        match value {
125            ReadExactError::UnexpectedEof => FSError::UnexpectedEof,
126            ReadExactError::Other(e) => FSError::IOError(e),
127        }
128    }
129}
130
131impl<I> From<RWFileError<I>> for FSError<I>
132where
133    I: Error,
134{
135    fn from(value: RWFileError<I>) -> Self {
136        match value {
137            RWFileError::StorageFull => FSError::StorageFull,
138            RWFileError::IOError(e) => FSError::IOError(e),
139        }
140    }
141}
142
143/// An alias for a [`Result`] with a [`FSError`] error type
144pub type FSResult<T, E> = Result<T, FSError<E>>;