Skip to content

Commit 82b8509

Browse files
Santiago Cingolaniinorick
Santiago Cingolani
authored andcommitted
Implement Display for errors
1 parent cca2eea commit 82b8509

File tree

6 files changed

+37
-17
lines changed

6 files changed

+37
-17
lines changed

heimlig/Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

heimlig/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ chacha20poly1305 = { version = "0.10.1", default-features = false }
1919
cmac = { version = "0.7.2", default-features = false }
2020
critical-section = { version = "1.1.2", default-features = false }
2121
dbl = { version = "0.3.2", default-features = false }
22+
displaydoc = { version = "0.2.4", default-features = false }
2223
ecdsa = { version = "0.16.8", default-features = false }
2324
ed25519-dalek = { version = "2.1.1", default-features = false, features = ["zeroize"] }
2425
elliptic-curve = { version = "0.13.5", default-features = false }
@@ -47,4 +48,4 @@ ed25519-dalek = { version = "2.1.1", default-features = false, features = ["zero
4748
cbindgen = { version = "0.26.0", default-features = false }
4849

4950
[lints.clippy]
50-
undocumented_unsafe_blocks = "warn"
51+
undocumented_unsafe_blocks = "warn"

heimlig/src/common/jobs.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
use displaydoc::Display;
2+
13
use crate::hsm::keystore;
24
use crate::hsm::keystore::{Curve, KeyId};
35

4-
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
6+
#[derive(Copy, Clone, Debug, Eq, PartialEq, Display)]
57
pub enum Error {
68
/// No worker found for received request type.
79
NoWorkerForRequest,
@@ -13,11 +15,11 @@ pub enum Error {
1315
NoKeyStore,
1416
/// Failed to send through channel.
1517
Send,
16-
/// Futures Stream was terminated
18+
/// Futures Stream was terminated.
1719
StreamTerminated,
18-
/// A cryptographic error occurred.
20+
/// A cryptographic error occurred: {0}
1921
Crypto(crate::crypto::Error),
20-
/// A key store error occurred.
22+
/// A key store error occurred: {0}
2123
KeyStore(keystore::Error),
2224
}
2325

heimlig/src/crypto/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use displaydoc::Display;
2+
13
pub mod aes;
24
pub mod chacha20poly1305;
35
pub mod ecc;
@@ -10,7 +12,7 @@ pub mod rng;
1012
pub mod x25519;
1113

1214
/// Common errors.
13-
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
15+
#[derive(Copy, Clone, Debug, Eq, PartialEq, Display)]
1416
pub enum Error {
1517
/// Error during encryption.
1618
Encrypt,

heimlig/src/hsm/core.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ use crate::hsm::keystore;
44
use core::future::poll_fn;
55
use core::ops::DerefMut;
66
use core::pin::Pin;
7+
use displaydoc::Display;
78
use embassy_futures::select::select_slice;
89
use embassy_sync::blocking_mutex::raw::RawMutex;
910
use embassy_sync::mutex::Mutex;
1011
use futures::{FutureExt, Sink, SinkExt, Stream, StreamExt};
1112
use heapless::Vec;
1213

13-
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
14+
#[derive(Copy, Clone, Debug, Eq, PartialEq, Display)]
1415
pub enum Error {
1516
/// Error sending message through queue
1617
Send,
@@ -26,24 +27,24 @@ pub enum Error {
2627
ChannelForRequestExists,
2728
/// Maximum number of request types for a single worker exceeded
2829
TooManyRequestTypes,
29-
/// An internal error occurred
30+
/// An internal error occurred: {0}
3031
Internal(InternalError),
3132
}
3233

3334
/// Internal errors that a client should not be able to trigger.
34-
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
35+
#[derive(Copy, Clone, Debug, Eq, PartialEq, Display)]
3536
pub enum InternalError {
36-
// The internal client ID was invalid for the internal list of client channels.
37+
/// The internal client ID ({0:?}) was invalid for the internal list of client channels.
3738
InvalidClientId(ClientId),
38-
// The internal worker ID was invalid for the internal list of worker channels.
39+
/// The internal worker ID ({0:?}) was invalid for the internal list of worker channels.
3940
InvalidWorkerId(WorkerId),
40-
/// An empty client request queue was encountered even though a previous check made sure that it was non-empty.
41+
/// An empty client ({0:?}) request queue was encountered even though a previous check made sure that it was non-empty.
4142
EmptyClientRequestQueue(ClientId),
42-
/// An empty worker response queue was encountered even though a previous check made sure that it was non-empty.
43+
/// An empty worker ({0:?}) response queue was encountered even though a previous check made sure that it was non-empty.
4344
EmptyWorkerResponseQueue(WorkerId),
44-
/// The core encountered a request type it cannot handle
45+
/// The core encountered a request ({0:?}) type it cannot handle.
4546
UnexpectedCoreRequest(RequestType),
46-
// The client ID of the response that was determined to be processed next did not match the one in the response queue.
47+
/// The client ID of the response that was determined to be processed next ({0:?}) did not match the one in the response queue ({1:?}).
4748
ClientIdMismatch(ClientId, ClientId),
4849
}
4950

heimlig/src/hsm/keystore.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
use displaydoc::Display;
2+
13
/// Identifier to reference HSM keys
24
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd)]
35
pub struct KeyId(pub u32);
46

5-
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
7+
#[derive(Copy, Clone, Debug, Eq, PartialEq, Display)]
68
pub enum Error {
79
/// The operation is not permitted
810
NotAllowed,
911
/// The requested key was not found.
1012
KeyNotFound,
11-
/// The operation attempted to overwrite an existing key when it was not permitted
13+
/// The operation attempted to overwrite an existing key when it was not permitted.
1214
KeyAlreadyExists,
1315
/// The key store cannot handle the number of requested keys.
1416
KeyStoreTooSmall,

0 commit comments

Comments
 (0)