Skip to content

Commit d4de591

Browse files
refactor(iroh)!: rename ProtocolError to AcceptError (#3339)
## Description To me, this reads much nicer in the examples etc. ## Breaking Changes - `iroh::endpoint::ProtocolError` -> `AcceptError` ## Notes & open questions <!-- Any notes, remarks or open questions you have to make about the PR. --> ## Change checklist <!-- Remove any that are not relevant. --> - [x] Self-review. - [ ] 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. - [ ] Tests if relevant. - [x] All breaking changes documented. - [x] List all breaking changes in the above "Breaking Changes" section. - [ ] Open an issue or PR on any number0 repos that are affected by this breaking change. Give guidance on how the updates should be handled or do the actual updates themselves. The major ones are: - [ ] [`quic-rpc`](https://github.com/n0-computer/quic-rpc) - [ ] [`iroh-gossip`](https://github.com/n0-computer/iroh-gossip) - [ ] [`iroh-blobs`](https://github.com/n0-computer/iroh-blobs) - [ ] [`dumbpipe`](https://github.com/n0-computer/dumbpipe) - [ ] [`sendme`](https://github.com/n0-computer/sendme) --------- Co-authored-by: Friedel Ziegelmayer <me@dignifiedquire.com>
1 parent fad99ab commit d4de591

File tree

3 files changed

+29
-32
lines changed

3 files changed

+29
-32
lines changed

iroh/examples/echo.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
99
use iroh::{
1010
endpoint::Connection,
11-
protocol::{ProtocolError, ProtocolHandler, Router},
11+
protocol::{AcceptError, ProtocolHandler, Router},
1212
watcher::Watcher as _,
1313
Endpoint, NodeAddr,
1414
};
@@ -83,7 +83,7 @@ impl ProtocolHandler for Echo {
8383
///
8484
/// The returned future runs on a newly spawned tokio task, so it can run as long as
8585
/// the connection lasts.
86-
async fn accept(&self, connection: Connection) -> Result<(), ProtocolError> {
86+
async fn accept(&self, connection: Connection) -> Result<(), AcceptError> {
8787
// We can get the remote's node id from the connection.
8888
let node_id = connection.remote_node_id()?;
8989
println!("accepted connection from {node_id}");

iroh/examples/search.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use std::{collections::BTreeSet, sync::Arc};
3434
use clap::Parser;
3535
use iroh::{
3636
endpoint::Connection,
37-
protocol::{ProtocolError, ProtocolHandler, Router},
37+
protocol::{AcceptError, ProtocolHandler, Router},
3838
Endpoint, NodeId,
3939
};
4040
use n0_snafu::{Result, ResultExt};
@@ -126,7 +126,7 @@ impl ProtocolHandler for BlobSearch {
126126
///
127127
/// The returned future runs on a newly spawned tokio task, so it can run as long as
128128
/// the connection lasts.
129-
async fn accept(&self, connection: Connection) -> Result<(), ProtocolError> {
129+
async fn accept(&self, connection: Connection) -> Result<(), AcceptError> {
130130
// We can get the remote's node id from the connection.
131131
let node_id = connection.remote_node_id()?;
132132
println!("accepted connection from {node_id}");
@@ -136,21 +136,18 @@ impl ProtocolHandler for BlobSearch {
136136
let (mut send, mut recv) = connection.accept_bi().await?;
137137

138138
// We read the query from the receive stream, while enforcing a max query length.
139-
let query_bytes = recv
140-
.read_to_end(64)
141-
.await
142-
.map_err(ProtocolError::from_err)?;
139+
let query_bytes = recv.read_to_end(64).await.map_err(AcceptError::from_err)?;
143140

144141
// Now, we can perform the actual query on our local database.
145-
let query = String::from_utf8(query_bytes).map_err(ProtocolError::from_err)?;
142+
let query = String::from_utf8(query_bytes).map_err(AcceptError::from_err)?;
146143
let num_matches = self.query_local(&query).await;
147144

148145
// We want to return a list of hashes. We do the simplest thing possible, and just send
149146
// one hash after the other. Because the hashes have a fixed size of 32 bytes, this is
150147
// very easy to parse on the other end.
151148
send.write_all(&num_matches.to_le_bytes())
152149
.await
153-
.map_err(ProtocolError::from_err)?;
150+
.map_err(AcceptError::from_err)?;
154151

155152
// By calling `finish` on the send stream we signal that we will not send anything
156153
// further, which makes the receive stream on the other end terminate.

iroh/src/protocol.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! ## Example
44
//!
55
//! ```no_run
6-
//! # use iroh::{endpoint::{Connection, BindError}, protocol::{ProtocolHandler, Router, ProtocolError}, Endpoint, NodeAddr};
6+
//! # use iroh::{endpoint::{Connection, BindError}, protocol::{AcceptError, ProtocolHandler, Router}, Endpoint, NodeAddr};
77
//! #
88
//! # async fn test_compile() -> Result<(), BindError> {
99
//! let endpoint = Endpoint::builder().discovery_n0().bind().await?;
@@ -19,7 +19,7 @@
1919
//! struct Echo;
2020
//!
2121
//! impl ProtocolHandler for Echo {
22-
//! async fn accept(&self, connection: Connection) -> Result<(), ProtocolError> {
22+
//! async fn accept(&self, connection: Connection) -> Result<(), AcceptError> {
2323
//! let (mut send, mut recv) = connection.accept_bi().await?;
2424
//!
2525
//! // Echo any bytes received back directly.
@@ -98,16 +98,16 @@ pub struct RouterBuilder {
9898
#[allow(missing_docs)]
9999
#[derive(Debug, Snafu)]
100100
#[non_exhaustive]
101-
pub enum ProtocolError {
101+
pub enum AcceptError {
102102
#[snafu(transparent)]
103-
Connect {
103+
Connection {
104104
source: crate::endpoint::ConnectionError,
105105
backtrace: Option<Backtrace>,
106106
#[snafu(implicit)]
107107
span_trace: n0_snafu::SpanTrace,
108108
},
109109
#[snafu(transparent)]
110-
RemoteNodeId { source: RemoteNodeIdError },
110+
MissingRemoteNodeId { source: RemoteNodeIdError },
111111
#[snafu(display("Not allowed."))]
112112
NotAllowed {},
113113

@@ -117,7 +117,7 @@ pub enum ProtocolError {
117117
},
118118
}
119119

120-
impl ProtocolError {
120+
impl AcceptError {
121121
/// Creates a new user error from an arbitrary error type.
122122
pub fn from_err<T: std::error::Error + Send + Sync + 'static>(value: T) -> Self {
123123
Self::User {
@@ -126,13 +126,13 @@ impl ProtocolError {
126126
}
127127
}
128128

129-
impl From<std::io::Error> for ProtocolError {
129+
impl From<std::io::Error> for AcceptError {
130130
fn from(err: std::io::Error) -> Self {
131131
Self::from_err(err)
132132
}
133133
}
134134

135-
impl From<quinn::ClosedStream> for ProtocolError {
135+
impl From<quinn::ClosedStream> for AcceptError {
136136
fn from(err: quinn::ClosedStream) -> Self {
137137
Self::from_err(err)
138138
}
@@ -158,7 +158,7 @@ pub trait ProtocolHandler: Send + Sync + std::fmt::Debug + 'static {
158158
fn on_connecting(
159159
&self,
160160
connecting: Connecting,
161-
) -> impl Future<Output = Result<Connection, ProtocolError>> + Send {
161+
) -> impl Future<Output = Result<Connection, AcceptError>> + Send {
162162
async move {
163163
let conn = connecting.await?;
164164
Ok(conn)
@@ -177,7 +177,7 @@ pub trait ProtocolHandler: Send + Sync + std::fmt::Debug + 'static {
177177
fn accept(
178178
&self,
179179
connection: Connection,
180-
) -> impl Future<Output = Result<(), ProtocolError>> + Send;
180+
) -> impl Future<Output = Result<(), AcceptError>> + Send;
181181

182182
/// Called when the router shuts down.
183183
///
@@ -191,11 +191,11 @@ pub trait ProtocolHandler: Send + Sync + std::fmt::Debug + 'static {
191191
}
192192

193193
impl<T: ProtocolHandler> ProtocolHandler for Arc<T> {
194-
async fn on_connecting(&self, conn: Connecting) -> Result<Connection, ProtocolError> {
194+
async fn on_connecting(&self, conn: Connecting) -> Result<Connection, AcceptError> {
195195
self.as_ref().on_connecting(conn).await
196196
}
197197

198-
async fn accept(&self, conn: Connection) -> Result<(), ProtocolError> {
198+
async fn accept(&self, conn: Connection) -> Result<(), AcceptError> {
199199
self.as_ref().accept(conn).await
200200
}
201201

@@ -205,11 +205,11 @@ impl<T: ProtocolHandler> ProtocolHandler for Arc<T> {
205205
}
206206

207207
impl<T: ProtocolHandler> ProtocolHandler for Box<T> {
208-
async fn on_connecting(&self, conn: Connecting) -> Result<Connection, ProtocolError> {
208+
async fn on_connecting(&self, conn: Connecting) -> Result<Connection, AcceptError> {
209209
self.as_ref().on_connecting(conn).await
210210
}
211211

212-
async fn accept(&self, conn: Connection) -> Result<(), ProtocolError> {
212+
async fn accept(&self, conn: Connection) -> Result<(), AcceptError> {
213213
self.as_ref().accept(conn).await
214214
}
215215

@@ -227,7 +227,7 @@ pub(crate) trait DynProtocolHandler: Send + Sync + std::fmt::Debug + 'static {
227227
fn on_connecting(
228228
&self,
229229
connecting: Connecting,
230-
) -> Pin<Box<dyn Future<Output = Result<Connection, ProtocolError>> + Send + '_>> {
230+
) -> Pin<Box<dyn Future<Output = Result<Connection, AcceptError>> + Send + '_>> {
231231
Box::pin(async move {
232232
let conn = connecting.await?;
233233
Ok(conn)
@@ -238,7 +238,7 @@ pub(crate) trait DynProtocolHandler: Send + Sync + std::fmt::Debug + 'static {
238238
fn accept(
239239
&self,
240240
connection: Connection,
241-
) -> Pin<Box<dyn Future<Output = Result<(), ProtocolError>> + Send + '_>>;
241+
) -> Pin<Box<dyn Future<Output = Result<(), AcceptError>> + Send + '_>>;
242242

243243
/// See [`ProtocolHandler::shutdown`].
244244
fn shutdown(&self) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
@@ -250,14 +250,14 @@ impl<P: ProtocolHandler> DynProtocolHandler for P {
250250
fn accept(
251251
&self,
252252
connection: Connection,
253-
) -> Pin<Box<dyn Future<Output = Result<(), ProtocolError>> + Send + '_>> {
253+
) -> Pin<Box<dyn Future<Output = Result<(), AcceptError>> + Send + '_>> {
254254
Box::pin(<Self as ProtocolHandler>::accept(self, connection))
255255
}
256256

257257
fn on_connecting(
258258
&self,
259259
connecting: Connecting,
260-
) -> Pin<Box<dyn Future<Output = Result<Connection, ProtocolError>> + Send + '_>> {
260+
) -> Pin<Box<dyn Future<Output = Result<Connection, AcceptError>> + Send + '_>> {
261261
Box::pin(<Self as ProtocolHandler>::on_connecting(self, connecting))
262262
}
263263

@@ -519,11 +519,11 @@ impl<P: ProtocolHandler + Clone> ProtocolHandler for AccessLimit<P> {
519519
fn on_connecting(
520520
&self,
521521
conn: Connecting,
522-
) -> impl Future<Output = Result<Connection, ProtocolError>> + Send {
522+
) -> impl Future<Output = Result<Connection, AcceptError>> + Send {
523523
self.proto.on_connecting(conn)
524524
}
525525

526-
async fn accept(&self, conn: Connection) -> Result<(), ProtocolError> {
526+
async fn accept(&self, conn: Connection) -> Result<(), AcceptError> {
527527
let remote = conn.remote_node_id()?;
528528
let is_allowed = (self.limiter)(remote);
529529
if !is_allowed {
@@ -572,7 +572,7 @@ mod tests {
572572
const ECHO_ALPN: &[u8] = b"/iroh/echo/1";
573573

574574
impl ProtocolHandler for Echo {
575-
async fn accept(&self, connection: Connection) -> Result<(), ProtocolError> {
575+
async fn accept(&self, connection: Connection) -> Result<(), AcceptError> {
576576
println!("accepting echo");
577577
let (mut send, mut recv) = connection.accept_bi().await?;
578578

@@ -619,7 +619,7 @@ mod tests {
619619
const TEST_ALPN: &[u8] = b"/iroh/test/1";
620620

621621
impl ProtocolHandler for TestProtocol {
622-
async fn accept(&self, connection: Connection) -> Result<(), ProtocolError> {
622+
async fn accept(&self, connection: Connection) -> Result<(), AcceptError> {
623623
self.connections.lock().expect("poisoned").push(connection);
624624
Ok(())
625625
}

0 commit comments

Comments
 (0)