Skip to content

Commit ee7dd2d

Browse files
authored
Merge pull request #180 from michael-p/impl-core-error
Implement `core::error::Error` for all error types
2 parents f319527 + 9b50426 commit ee7dd2d

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ The format is based on [Keep a Changelog] and this project adheres to [Semantic
2727

2828
- `File` now implements the `embedded-io` `Read`, `Write` and `Seek` traits.
2929
- New `iterate_dir_lfn` method on `VolumeManager` and `Directory` - provides decoded Long File Names as `Option<&str>`
30+
- Implement `core::error::Error` for all error types (behind crate feature `core-error` as this raises the MSRV to 1.81)
3031

3132
### Removed
3233

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ sha2 = "0.10"
3333
default = ["log"]
3434
defmt-log = ["dep:defmt"]
3535
log = ["dep:log"]
36+
core-error = []

src/lib.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
6868
//! * `defmt-log`: By turning off the default features and enabling the
6969
//! `defmt-log` feature you can configure this crate to log messages over defmt
7070
//! instead.
71+
//! * `core-error`: Enables implementations of `core::error::Error` for all error
72+
//! types. This raises the Minimum Supported Rust Version to 1.81.
7173
//!
7274
//! You cannot enable both the `log` feature and the `defmt-log` feature.
7375
@@ -272,6 +274,51 @@ where
272274
}
273275
}
274276

277+
impl<E> core::fmt::Display for Error<E>
278+
where
279+
E: core::fmt::Debug + core::fmt::Display,
280+
{
281+
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
282+
match self {
283+
Error::DeviceError(e) => write!(f, "error from underlying block device: {e}"),
284+
Error::FormatError(s) => write!(f, "filesystem is badly formatted: {s}"),
285+
Error::NoSuchVolume => write!(f, "no such volume"),
286+
Error::FilenameError(_) => write!(f, "bad filename"),
287+
Error::TooManyOpenVolumes => write!(f, "too many open volumes"),
288+
Error::TooManyOpenDirs => write!(f, "too many open directories"),
289+
Error::TooManyOpenFiles => write!(f, "too many open files"),
290+
Error::BadHandle => write!(f, "bad handle"),
291+
Error::NotFound => write!(f, "file or directory does not exist"),
292+
Error::FileAlreadyOpen => write!(f, "file already open"),
293+
Error::DirAlreadyOpen => write!(f, "directory already open"),
294+
Error::OpenedDirAsFile => write!(f, "cannot open directory as file"),
295+
Error::OpenedFileAsDir => write!(f, "cannot open file as directory"),
296+
Error::DeleteDirAsFile => write!(f, "cannot delete directory as file"),
297+
Error::VolumeStillInUse => write!(f, "volume is still in use"),
298+
Error::VolumeAlreadyOpen => write!(f, "cannot open volume twice"),
299+
Error::Unsupported => write!(f, "unsupported operation"),
300+
Error::EndOfFile => write!(f, "end of file"),
301+
Error::BadCluster => write!(f, "bad cluster"),
302+
Error::ConversionError => write!(f, "type conversion failed"),
303+
Error::NotEnoughSpace => write!(f, "not enough space on device"),
304+
Error::AllocationError => write!(f, "cluster not properly allocated"),
305+
Error::UnterminatedFatChain => write!(f, "FAT chain unterminated"),
306+
Error::ReadOnly => write!(f, "file is read-only"),
307+
Error::FileAlreadyExists => write!(f, "file already exists"),
308+
Error::BadBlockSize(size) => {
309+
write!(f, "bad block size: {size} (only 512 byte blocks supported)")
310+
}
311+
Error::InvalidOffset => write!(f, "invalid seek offset"),
312+
Error::DiskFull => write!(f, "disk full"),
313+
Error::DirAlreadyExists => write!(f, "directory already exists"),
314+
Error::LockError => write!(f, "already locked"),
315+
}
316+
}
317+
}
318+
319+
#[cfg(feature = "core-error")]
320+
impl<E> core::error::Error for Error<E> where E: core::fmt::Debug + core::fmt::Display {}
321+
275322
/// A handle to a volume.
276323
///
277324
/// A volume is a partition with a filesystem within it.

src/sdcard/mod.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,33 @@ pub enum Error {
624624
GpioError,
625625
}
626626

627+
impl core::fmt::Display for Error {
628+
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
629+
match self {
630+
Error::Transport => write!(f, "error from SPI peripheral"),
631+
Error::CantEnableCRC => write!(f, "failed to enable CRC checking"),
632+
Error::TimeoutReadBuffer => write!(f, "timeout when reading data"),
633+
Error::TimeoutWaitNotBusy => write!(f, "timeout when waiting for card to not be busy"),
634+
Error::TimeoutCommand(command) => write!(f, "timeout when executing command {command}"),
635+
Error::TimeoutACommand(command) => write!(
636+
f,
637+
"timeout when executing application-specific command {command}"
638+
),
639+
Error::Cmd58Error => write!(f, "bad response from command 58"),
640+
Error::RegisterReadError => write!(f, "failed to read Card Specific Data register"),
641+
Error::CrcError(_, _) => write!(f, "CRC mismatch"),
642+
Error::ReadError => write!(f, "read error"),
643+
Error::WriteError => write!(f, "write error"),
644+
Error::BadState => write!(f, "cannot perform operation with card in thiis state"),
645+
Error::CardNotFound => write!(f, "card not found"),
646+
Error::GpioError => write!(f, "cannot set GPIO pin"),
647+
}
648+
}
649+
}
650+
651+
#[cfg(feature = "core-error")]
652+
impl core::error::Error for Error {}
653+
627654
/// The different types of card we support.
628655
#[cfg_attr(feature = "defmt-log", derive(defmt::Format))]
629656
#[derive(Debug, Copy, Clone, PartialEq, Eq)]

0 commit comments

Comments
 (0)