Skip to content

Commit 5ea31ba

Browse files
committed
add missing impl Errors
use `thiserror` to do this as it's already used in the crate. this updates it from v1 to v2 and also uses it in `no_std` environments as that's now supported by `thiserror`. this was missing and thus it wasn't possible to use e.g. `HostErr` in a `#[from]` with `thiserror`.
1 parent c0a567b commit 5ea31ba

File tree

7 files changed

+67
-57
lines changed

7 files changed

+67
-57
lines changed

example/workbook-host/Cargo.lock

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

source/postcard-rpc-test/Cargo.lock

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

source/postcard-rpc/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ version = "0.3.69"
6464
optional = true
6565

6666
[dependencies.thiserror]
67-
version = "1.0"
68-
optional = true
67+
version = "2"
68+
default-features = false
6969

7070
[dependencies.web-sys]
7171

@@ -195,7 +195,7 @@ use-std = [
195195
"dep:tokio",
196196
"postcard/use-std",
197197
"postcard-schema/use-std",
198-
"dep:thiserror",
198+
"thiserror/std",
199199
"dep:tracing",
200200
"dep:trait-variant",
201201
"dep:ssmarshal",

source/postcard-rpc/src/host_client/mod.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use std::{
1313
Arc, RwLock,
1414
},
1515
};
16-
1716
use thiserror::Error;
1817

1918
use maitake_sync::{
@@ -52,28 +51,26 @@ pub(crate) mod util;
5251
pub mod test_channels;
5352

5453
/// Host Error Kind
55-
#[derive(Debug, PartialEq)]
54+
#[derive(Debug, PartialEq, Error)]
5655
pub enum HostErr<WireErr> {
5756
/// An error of the user-specified wire error type
57+
#[error("a wire error occurred")]
5858
Wire(WireErr),
5959
/// We got a response that didn't match the expected value or the
6060
/// user specified wire error type
6161
///
6262
/// This is also (misused) to report when duplicate sequence numbers
6363
/// in-flight at the same time are detected.
64+
#[error("the response received didn't match the expected value or the wire error type")]
6465
BadResponse,
6566
/// Deserialization of the message failed
66-
Postcard(postcard::Error),
67+
#[error("message deserialization failed")]
68+
Postcard(#[from] postcard::Error),
6769
/// The interface has been closed, and no further messages are possible
70+
#[error("the interface has been closed, and no further messages are possible")]
6871
Closed,
6972
}
7073

71-
impl<T> From<postcard::Error> for HostErr<T> {
72-
fn from(value: postcard::Error) -> Self {
73-
Self::Postcard(value)
74-
}
75-
}
76-
7774
impl<T> From<WaitError> for HostErr<T> {
7875
fn from(_: WaitError) -> Self {
7976
Self::Closed
@@ -230,17 +227,23 @@ where
230227
}
231228

232229
/// Errors related to retrieving the schema
233-
#[derive(Debug)]
230+
#[derive(Debug, Error)]
234231
pub enum SchemaError<WireErr> {
235232
/// Some kind of communication error occurred
236-
Comms(HostErr<WireErr>),
233+
#[error("A communication error occurred")]
234+
Comms(#[from] HostErr<WireErr>),
237235
/// An error occurred internally. Please open an issue.
236+
#[error("An error occurred internally. Please open an issue.")]
238237
TaskError,
239238
/// Invalid report data was received, including endpoints or
240239
/// tasks that referred to unknown types. Please open an issue
240+
#[error("Invalid report data was received. Please open an issue.")]
241241
InvalidReportData,
242242
/// Data was lost while transmitting. If a retry does not solve
243243
/// this, please open an issue.
244+
#[error(
245+
"Data was lost while transmitting. If a retry does not solve this, please open an issue."
246+
)]
244247
LostData,
245248
}
246249

source/postcard-rpc/src/server/impls/embassy_usb_v0_3.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,10 @@ pub mod fake {
750750
}
751751

752752
impl EndpointIn for FakeEpIn {
753-
async fn write(&mut self, _buf: &[u8]) -> Result<(), embassy_usb_driver_0_1::EndpointError> {
753+
async fn write(
754+
&mut self,
755+
_buf: &[u8],
756+
) -> Result<(), embassy_usb_driver_0_1::EndpointError> {
754757
todo!()
755758
}
756759
}
@@ -824,7 +827,10 @@ pub mod fake {
824827
todo!()
825828
}
826829

827-
fn endpoint_is_stalled(&mut self, _ep_addr: embassy_usb_driver_0_1::EndpointAddress) -> bool {
830+
fn endpoint_is_stalled(
831+
&mut self,
832+
_ep_addr: embassy_usb_driver_0_1::EndpointAddress,
833+
) -> bool {
828834
todo!()
829835
}
830836

source/postcard-rpc/src/server/impls/embassy_usb_v0_4.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,13 @@ impl<M: RawMutex + 'static, D: Driver<'static> + 'static> WireTx for EUsbWireTx<
455455
// Calculate the TOTAL amount
456456
let act_used = ttl_len - remain;
457457

458-
send_all::<D>(ep_in, &tx_buf[..act_used], pending_frame, *timeout_ms_per_frame).await
458+
send_all::<D>(
459+
ep_in,
460+
&tx_buf[..act_used],
461+
pending_frame,
462+
*timeout_ms_per_frame,
463+
)
464+
.await
459465
}
460466
}
461467

@@ -798,7 +804,10 @@ pub mod fake {
798804
}
799805

800806
impl EndpointIn for FakeEpIn {
801-
async fn write(&mut self, _buf: &[u8]) -> Result<(), embassy_usb_driver_0_1::EndpointError> {
807+
async fn write(
808+
&mut self,
809+
_buf: &[u8],
810+
) -> Result<(), embassy_usb_driver_0_1::EndpointError> {
802811
todo!()
803812
}
804813
}
@@ -872,7 +881,10 @@ pub mod fake {
872881
todo!()
873882
}
874883

875-
fn endpoint_is_stalled(&mut self, _ep_addr: embassy_usb_driver_0_1::EndpointAddress) -> bool {
884+
fn endpoint_is_stalled(
885+
&mut self,
886+
_ep_addr: embassy_usb_driver_0_1::EndpointAddress,
887+
) -> bool {
876888
todo!()
877889
}
878890

source/postcard-rpc/src/server/mod.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ pub mod impls;
2929

3030
use core::{fmt::Arguments, ops::DerefMut};
3131

32-
use postcard_schema::Schema;
33-
use serde::Serialize;
34-
3532
use crate::{
3633
header::{VarHeader, VarKey, VarKeyKind, VarSeq},
3734
DeviceMap, Key, TopicDirection,
3835
};
36+
use postcard_schema::Schema;
37+
use serde::Serialize;
38+
use thiserror::Error;
3939

4040
//////////////////////////////////////////////////////////////////////////////
4141
// TX
@@ -77,21 +77,24 @@ pub trait WireTx {
7777
}
7878

7979
/// The base [`WireTx`] Error Kind
80-
#[derive(Debug, Clone, Copy)]
80+
#[derive(Debug, Clone, Copy, Error)]
8181
#[non_exhaustive]
8282
pub enum WireTxErrorKind {
8383
/// The connection has been closed, and is unlikely to succeed until
8484
/// the connection is re-established. This will cause the Server run
8585
/// loop to terminate.
86+
#[error("connection closed")]
8687
ConnectionClosed,
8788
/// Other unspecified errors
89+
#[error("other")]
8890
Other,
8991
/// Timeout (WireTx impl specific) reached
92+
#[error("timeout reached")]
9093
Timeout,
9194
}
9295

9396
/// A conversion trait to convert a user error into a base Kind type
94-
pub trait AsWireTxErrorKind {
97+
pub trait AsWireTxErrorKind: core::error::Error {
9598
/// Convert the error type into a base type
9699
fn as_kind(&self) -> WireTxErrorKind;
97100
}
@@ -127,21 +130,24 @@ pub trait WireRx {
127130
}
128131

129132
/// The base [`WireRx`] Error Kind
130-
#[derive(Debug, Clone, Copy)]
133+
#[derive(Debug, Clone, Copy, Error)]
131134
#[non_exhaustive]
132135
pub enum WireRxErrorKind {
133136
/// The connection has been closed, and is unlikely to succeed until
134137
/// the connection is re-established. This will cause the Server run
135138
/// loop to terminate.
139+
#[error("connection closed")]
136140
ConnectionClosed,
137141
/// The received message was too large for the server to handle
142+
#[error("the received message was too large for the server to handle")]
138143
ReceivedMessageTooLarge,
139144
/// Other message kinds
145+
#[error("other")]
140146
Other,
141147
}
142148

143149
/// A conversion trait to convert a user error into a base Kind type
144-
pub trait AsWireRxErrorKind {
150+
pub trait AsWireRxErrorKind: core::error::Error {
145151
/// Convert the error type into a base type
146152
fn as_kind(&self) -> WireRxErrorKind;
147153
}
@@ -384,15 +390,18 @@ where
384390
}
385391

386392
/// A type representing the different errors [`Server::run()`] may return
393+
#[derive(Debug, Error)]
387394
pub enum ServerError<Tx, Rx>
388395
where
389396
Tx: WireTx,
390397
Rx: WireRx,
391398
{
392399
/// A fatal error occurred with the [`WireTx::send()`] implementation
393-
TxFatal(Tx::Error),
400+
#[error("A fatal error occurred while transmitting")]
401+
TxFatal(#[source] Tx::Error),
394402
/// A fatal error occurred with the [`WireRx::receive()`] implementation
395-
RxFatal(Rx::Error),
403+
#[error("A fatal error occurred while receiving")]
404+
RxFatal(#[source] Rx::Error),
396405
}
397406

398407
impl<Tx, Rx, Buf, D> Server<Tx, Rx, Buf, D>

0 commit comments

Comments
 (0)