fatfs/
error.rs

1#[cfg(feature = "std")]
2use std::error::Error;
3
4use crate::{core::fmt, io};
5
6#[derive(fmt::Debug)]
7pub enum FatfsNumericError {
8    NotPowerOfTwo,
9    TooLarge(usize),
10    TooSmall(usize),
11}
12
13impl fmt::Display for FatfsNumericError {
14    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
15        match self {
16            FatfsNumericError::NotPowerOfTwo => write!(f, "not power of two"),
17            FatfsNumericError::TooLarge(max) => write!(f, "value > {}", max),
18            FatfsNumericError::TooSmall(min) => write!(f, "value < {}", min),
19        }
20    }
21}
22
23#[derive(fmt::Debug)]
24/// Error type returned as the inner error for errors with ErrorKind::Other.
25pub enum FatfsError {
26    BackupBootSectorInvalid,
27    BadDiskSize,
28    ClusterFatMismatch,
29    DirectoryNotEmpty,
30    FileNameBadCharacter,
31    FileNameEmpty,
32    FileNameTooLong,
33    FsInfoInvalid,
34    InvalidBootSectorSig,
35    InvalidBytesPerSector(FatfsNumericError),
36    InvalidClusterNumber,
37    InvalidFatType,
38    InvalidFats,
39    InvalidFatEntries,
40    InvalidLeadSig,
41    InvalidNumClusters,
42    InvalidReservedSectors,
43    InvalidSectorsPerCluster(FatfsNumericError),
44    InvalidSectorsPerFat,
45    InvalidStrucSig,
46    InvalidTrailSig,
47    IsDirectory,
48    NoSpace,
49    NonZeroRootEntries,
50    NonZeroTotalSectors,
51    NotDirectory,
52    TooManyClusters,
53    TooManyReservedSectors,
54    TooManySectors,
55    TotalSectorsTooSmall,
56    UnknownVersion,
57    VolumeTooSmall,
58    ZeroRootEntries,
59    ZeroTotalSectors,
60}
61
62#[cfg(feature = "std")]
63impl Error for FatfsError {
64    fn source(&self) -> Option<&(dyn Error + 'static)> {
65        None
66    }
67}
68
69impl fmt::Display for FatfsError {
70    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
71        write!(f, "{}", String::from(self))
72    }
73}
74
75impl From<FatfsError> for io::Error {
76    fn from(error: FatfsError) -> io::Error {
77        io::Error::new(io::ErrorKind::Other, error)
78    }
79}
80
81impl From<&FatfsError> for String {
82    fn from(error: &FatfsError) -> String {
83        match error {
84            FatfsError::BackupBootSectorInvalid => "Invalid BPB (backup boot-sector not in a reserved region)".to_owned(),
85            FatfsError::BadDiskSize => "Cannot select FAT type - unfortunate disk size".to_owned(),
86            FatfsError::ClusterFatMismatch => "Total number of clusters and FAT type does not match. Try other volume size".to_owned(),
87            FatfsError::DirectoryNotEmpty => "Directory not empty".to_owned(),
88            FatfsError::FileNameBadCharacter => "File name contains unsupported characters".to_owned(),
89            FatfsError::FileNameEmpty => "File name is empty".to_owned(),
90            FatfsError::FileNameTooLong => "File name too long".to_owned(),
91            FatfsError::FsInfoInvalid => "Invalid BPB (FSInfo sector not in a reserved region)".to_owned(),
92            FatfsError::InvalidBootSectorSig => "Invalid boot sector signature".to_owned(),
93            FatfsError::InvalidBytesPerSector(what) => format!("Invalid bytes_per_sector value in BPB ({})", what),
94            FatfsError::InvalidClusterNumber => "Cluster number is invalid".to_owned(),
95            FatfsError::InvalidFatEntries => "Invalid number of fat entries".to_owned(),
96            FatfsError::InvalidFatType => "Invalid FAT type".to_owned(),
97            FatfsError::InvalidFats => "Invalid fats value in BPB".to_owned(),
98            FatfsError::InvalidLeadSig => "Invalid lead_sig in FsInfo sector".to_owned(),
99            FatfsError::InvalidNumClusters => "Invalid BPB (result of FAT32 determination from total number of clusters and sectors_per_fat_16 field differs)".to_owned(),
100            FatfsError::InvalidReservedSectors => "Invalid reserved_sectors value in BPB".to_owned(),
101            FatfsError::InvalidSectorsPerCluster(what) => format!("Invalid sectors_per_cluster value in BPB ({})", what),
102            FatfsError::InvalidSectorsPerFat => "Invalid sectors_per_fat_32 value in BPB (should be non-zero for FAT32)".to_owned(),
103            FatfsError::InvalidStrucSig => "Invalid struc_sig in FsInfo sector".to_owned(),
104            FatfsError::InvalidTrailSig => "Invalid trail_sig in FsInfo sector".to_owned(),
105            FatfsError::IsDirectory => "Is a directory".to_owned(),
106            FatfsError::NoSpace => "No space left on device".to_owned(),
107            FatfsError::NonZeroRootEntries => "Invalid root_entries value in BPB (should be zero for FAT32)".to_owned(),
108            FatfsError::NonZeroTotalSectors => "Invalid BPB (total_sectors_16 or total_sectors_32 should be non-zero)".to_owned(),
109            FatfsError::NotDirectory => "Not a directory".to_owned(),
110            FatfsError::TooManyClusters => "Too many clusters".to_owned(),
111            FatfsError::TooManyReservedSectors => "Too many reserved sectors".to_owned(),
112            FatfsError::TooManySectors => "Volume has too many sectors".to_owned(),
113            FatfsError::TotalSectorsTooSmall => "Invalid BPB (total_sectors field value is too small)".to_owned(),
114            FatfsError::UnknownVersion => "Unknown FS version".to_owned(),
115            FatfsError::VolumeTooSmall =>  "Volume is too small".to_owned(),
116            FatfsError::ZeroRootEntries => "Empty root directory region defined in FAT12/FAT16 BPB".to_owned(),
117            FatfsError::ZeroTotalSectors => "Invalid total_sectors_16 value in BPB (should be zero for FAT32)".to_owned(),
118        }
119    }
120}