Skip to content

Commit 1859de3

Browse files
ramfox“ramfox”
andauthored
feat: add methods to create variants of the iroh-base::ticket::ParseError enum. (#3362)
## Description Adds a `From` impl for `postcard::Error`, and adds `ParseError::verification_failed` and `ParseError::wrong_prefix` methods. ## Open Questions Should we add a variant to `ParseError` for a more "open ended" error, like we do for `protocol::AcceptError`? Something like `ParseError::Custom`? ## Change checklist <!-- Remove any that are not relevant. --> - [x] Self-review. - [x] Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant. --------- Co-authored-by: “ramfox” <“kasey@n0.computer”>
1 parent 7f2cdd1 commit 1859de3

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

iroh-base/src/ticket.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@ pub trait Ticket: Sized {
6969
#[snafu(visibility(pub(crate)))]
7070
#[non_exhaustive]
7171
pub enum ParseError {
72-
/// Found a ticket of with the wrong prefix, indicating the wrong kind.
72+
/// Found a ticket with the wrong prefix, indicating the wrong kind.
7373
#[snafu(display("wrong prefix, expected {expected}"))]
7474
Kind {
7575
/// The expected prefix.
7676
expected: &'static str,
7777
},
7878
/// This looks like a ticket, but postcard deserialization failed.
79-
#[snafu(display("deserialization failed"))]
79+
#[snafu(transparent)]
8080
Postcard { source: postcard::Error },
8181
/// This looks like a ticket, but base32 decoding failed.
8282
#[snafu(transparent)]
@@ -86,6 +86,22 @@ pub enum ParseError {
8686
Verify { message: &'static str },
8787
}
8888

89+
impl ParseError {
90+
/// Returns a [`ParseError`] that indicates the given ticket has the wrong
91+
/// prefix.
92+
///
93+
/// Indicate the expected prefix.
94+
pub fn wrong_prefix(expected: &'static str) -> Self {
95+
KindSnafu { expected }.build()
96+
}
97+
98+
/// Return a `ParseError` variant that indicates verification of the
99+
/// deserialized bytes failed.
100+
pub fn verification_failed(message: &'static str) -> Self {
101+
VerifySnafu { message }.build()
102+
}
103+
}
104+
89105
#[derive(Serialize, Deserialize)]
90106
struct Variant0NodeAddr {
91107
node_id: NodeId,

iroh-base/src/ticket/node.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use std::str::FromStr;
44

55
use serde::{Deserialize, Serialize};
6-
use snafu::ResultExt;
76

87
use super::{Variant0AddrInfo, Variant0NodeAddr};
98
use crate::{
@@ -63,7 +62,7 @@ impl Ticket for NodeTicket {
6362
}
6463

6564
fn from_bytes(bytes: &[u8]) -> Result<Self, ParseError> {
66-
let res: TicketWireFormat = postcard::from_bytes(bytes).context(ticket::PostcardSnafu)?;
65+
let res: TicketWireFormat = postcard::from_bytes(bytes)?;
6766
let TicketWireFormat::Variant0(Variant0NodeTicket { node }) = res;
6867
Ok(Self {
6968
node: NodeAddr {

0 commit comments

Comments
 (0)