Skip to content

Commit eb857e4

Browse files
committed
Documented swimos_remote crate.
1 parent efc4ca9 commit eb857e4

File tree

14 files changed

+72
-51
lines changed

14 files changed

+72
-51
lines changed

client/fixture/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use swimos_recon::print_recon;
2121
use swimos_remote::dns::{BoxDnsResolver, DnsResolver};
2222
use swimos_remote::websocket::{RatchetError, WebsocketClient, WebsocketServer, WsOpenFuture};
2323
use swimos_remote::{
24-
ClientConnections, ConnResult, ConnectionError, Listener, ListenerError, Scheme,
24+
ClientConnections, ConnectionError, ConnectionResult, Listener, ListenerError, Scheme,
2525
};
2626
use tokio::io::{AsyncRead, AsyncWrite, DuplexStream};
2727
use tokio::sync::mpsc;
@@ -71,7 +71,7 @@ impl ClientConnections for MockClientConnections {
7171
_scheme: Scheme,
7272
_host: Option<&str>,
7373
addr: SocketAddr,
74-
) -> BoxFuture<'_, ConnResult<Self::ClientSocket>> {
74+
) -> BoxFuture<'_, ConnectionResult<Self::ClientSocket>> {
7575
async move {
7676
self.inner
7777
.lock()

client/runtime/src/commander/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use ratchet::{
2222
};
2323
use swimos_form::write::StructuralWritable;
2424
use swimos_recon::print_recon_compact;
25-
use swimos_remote::{BadUrl, SchemeHostPort};
25+
use swimos_remote::{BadWarpUrl, SchemeHostPort};
2626
use thiserror::Error;
2727
use tokio::net::TcpStream;
2828

@@ -31,7 +31,7 @@ pub enum CommandError {
3131
#[error("Failed to send command: {0}")]
3232
Ratchet(#[from] ratchet::Error),
3333
#[error("Invalid URL: {0}")]
34-
Url(#[from] BadUrl),
34+
Url(#[from] BadWarpUrl),
3535
}
3636

3737
impl From<std::io::Error> for CommandError {

runtime/swimos_remote/src/dns.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ pub trait DnsResolver {
3131
fn resolve(&self, host: String, port: u16) -> Self::ResolveFuture;
3232
}
3333

34+
#[doc(hidden)]
3435
pub type DnsFut = BoxFuture<'static, io::Result<Vec<SocketAddr>>>;
36+
37+
#[doc(hidden)]
3538
pub type BoxDnsResolver = Box<dyn DnsResolver<ResolveFuture = DnsFut> + Send + 'static>;
3639

3740
impl<R> DnsResolver for Box<R>
@@ -72,6 +75,8 @@ impl DnsResolver for GetAddressInfoResolver {
7275
}
7376
}
7477

78+
/// The default DNS resolver. If the `trust-dns` feature flag is enabled, this will use the `trust_dns`
79+
/// implementation, otherwise it will use the operating system's built-in DNS support.
7580
#[derive(Debug, Clone)]
7681
pub struct Resolver {
7782
#[cfg(not(feature = "trust-dns"))]

runtime/swimos_remote/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ mod ws;
3838
pub use task::RemoteTask;
3939

4040
pub use net::{
41-
BadUrl, ClientConnections, ConnResult, ConnectionError, ExternalConnections, Listener,
42-
ListenerError, ListenerResult, Scheme, SchemeHostPort, ServerConnections,
41+
BadWarpUrl, ClientConnections, ConnectionError, ExternalConnections, Listener, ListenerError,
42+
Scheme, SchemeHostPort, ServerConnections,
4343
};
44+
#[doc(hidden)]
45+
pub use net::{ConnectionResult, ListenerResult};
4446

4547
/// Bindings to use the [`ratchet`] web-sockets crate with the networking abstraction in this crate.
4648
pub mod websocket {

runtime/swimos_remote/src/net/mod.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ use crate::dns::BoxDnsResolver;
3232
#[cfg(test)]
3333
mod tests;
3434

35-
pub type ConnResult<T> = Result<T, ConnectionError>;
35+
#[doc(hidden)]
36+
pub type ConnectionResult<T> = Result<T, ConnectionError>;
3637

3738
/// Error to indicate why attempting to open a new connection failed.
3839
#[derive(Debug, Error)]
@@ -85,6 +86,7 @@ impl From<std::io::Error> for ListenerError {
8586
}
8687
}
8788

89+
#[doc(hidden)]
8890
pub type ListenerResult<T> = Result<T, ListenerError>;
8991

9092
/// Trait for servers that listen for incoming remote connections. This is primarily used to
@@ -106,7 +108,7 @@ pub trait ClientConnections: Clone + Send + Sync + 'static {
106108
scheme: Scheme,
107109
host: Option<&str>,
108110
addr: SocketAddr,
109-
) -> BoxFuture<'_, ConnResult<Self::ClientSocket>>;
111+
) -> BoxFuture<'_, ConnectionResult<Self::ClientSocket>>;
110112

111113
fn dns_resolver(&self) -> BoxDnsResolver;
112114
fn lookup(
@@ -127,7 +129,7 @@ where
127129
scheme: Scheme,
128130
host: Option<&str>,
129131
addr: SocketAddr,
130-
) -> BoxFuture<'_, ConnResult<Self::ClientSocket>> {
132+
) -> BoxFuture<'_, ConnectionResult<Self::ClientSocket>> {
131133
(**self).try_open(scheme, host, addr)
132134
}
133135

@@ -152,7 +154,7 @@ pub trait ServerConnections: Clone + Send + Sync + 'static {
152154
fn bind(
153155
&self,
154156
addr: SocketAddr,
155-
) -> BoxFuture<'static, ConnResult<(SocketAddr, Self::ListenerType)>>;
157+
) -> BoxFuture<'static, ConnectionResult<(SocketAddr, Self::ListenerType)>>;
156158
}
157159

158160
impl<C> ServerConnections for Arc<C>
@@ -166,7 +168,7 @@ where
166168
fn bind(
167169
&self,
168170
addr: SocketAddr,
169-
) -> BoxFuture<'static, ConnResult<(SocketAddr, Self::ListenerType)>> {
171+
) -> BoxFuture<'static, ConnectionResult<(SocketAddr, Self::ListenerType)>> {
170172
(**self).bind(addr)
171173
}
172174
}
@@ -180,13 +182,13 @@ pub trait ExternalConnections: Clone + Send + Sync + 'static {
180182
fn bind(
181183
&self,
182184
addr: SocketAddr,
183-
) -> BoxFuture<'static, ConnResult<(SocketAddr, Self::ListenerType)>>;
185+
) -> BoxFuture<'static, ConnectionResult<(SocketAddr, Self::ListenerType)>>;
184186
fn try_open(
185187
&self,
186188
scheme: Scheme,
187189
host: Option<&str>,
188190
addr: SocketAddr,
189-
) -> BoxFuture<'_, ConnResult<Self::Socket>>;
191+
) -> BoxFuture<'_, ConnectionResult<Self::Socket>>;
190192

191193
fn dns_resolver(&self) -> BoxDnsResolver;
192194
fn lookup(
@@ -207,7 +209,7 @@ where
207209
fn bind(
208210
&self,
209211
addr: SocketAddr,
210-
) -> BoxFuture<'static, ConnResult<(SocketAddr, Self::ListenerType)>> {
212+
) -> BoxFuture<'static, ConnectionResult<(SocketAddr, Self::ListenerType)>> {
211213
<Self as ServerConnections>::bind(self, addr)
212214
}
213215

@@ -216,7 +218,7 @@ where
216218
scheme: Scheme,
217219
host: Option<&str>,
218220
addr: SocketAddr,
219-
) -> BoxFuture<'_, ConnResult<Self::Socket>> {
221+
) -> BoxFuture<'_, ConnectionResult<Self::Socket>> {
220222
<Self as ClientConnections>::try_open(self, scheme, host, addr)
221223
}
222224

@@ -233,10 +235,11 @@ where
233235
}
234236
}
235237

238+
/// Error type indicating that host URL is not valid for the Warp protocol.
236239
#[derive(Debug, PartialEq, Eq, Error)]
237-
pub enum BadUrl {
240+
pub enum BadWarpUrl {
238241
#[error("Malformed URL: {0}")]
239-
BadUrl(String),
242+
MalformedUrl(String),
240243
#[error("A WARP URL must have a scheme.")]
241244
MissingScheme,
242245
#[error("{0} is not a valid WARP scheme.")]
@@ -245,9 +248,9 @@ pub enum BadUrl {
245248
NoHost,
246249
}
247250

248-
impl From<InvalidUri> for BadUrl {
251+
impl From<InvalidUri> for BadWarpUrl {
249252
fn from(value: InvalidUri) -> Self {
250-
BadUrl::BadUrl(value.to_string())
253+
BadWarpUrl::MalformedUrl(value.to_string())
251254
}
252255
}
253256

@@ -259,13 +262,13 @@ pub enum Scheme {
259262
}
260263

261264
impl TryFrom<&str> for Scheme {
262-
type Error = BadUrl;
265+
type Error = BadWarpUrl;
263266

264267
fn try_from(value: &str) -> Result<Self, Self::Error> {
265268
match value {
266269
"ws" | "swimos" | "warp" => Ok(Scheme::Ws),
267270
"wss" | "swims" | "warps" => Ok(Scheme::Wss),
268-
_ => Err(BadUrl::BadScheme(value.to_owned())),
271+
_ => Err(BadWarpUrl::BadScheme(value.to_owned())),
269272
}
270273
}
271274
}
@@ -331,15 +334,16 @@ impl Display for SchemeHostPort {
331334
}
332335

333336
impl FromStr for SchemeHostPort {
334-
type Err = BadUrl;
337+
type Err = BadWarpUrl;
335338

336339
fn from_str(s: &str) -> Result<Self, Self::Err> {
337340
let uri = s.parse::<Uri>()?;
338341

339342
let scheme = if let Some(scheme_part) = uri.scheme_str() {
340-
Scheme::try_from(scheme_part).map_err(|_| BadUrl::BadScheme(scheme_part.to_string()))?
343+
Scheme::try_from(scheme_part)
344+
.map_err(|_| BadWarpUrl::BadScheme(scheme_part.to_string()))?
341345
} else {
342-
return Err(BadUrl::MissingScheme);
346+
return Err(BadWarpUrl::MissingScheme);
343347
};
344348

345349
match (uri.host(), uri.port_u16()) {
@@ -354,7 +358,7 @@ impl FromStr for SchemeHostPort {
354358
default_port,
355359
))
356360
}
357-
_ => Err(BadUrl::NoHost),
361+
_ => Err(BadWarpUrl::NoHost),
358362
}
359363
}
360364
}

runtime/swimos_remote/src/net/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use crate::net::{BadUrl, Scheme, SchemeHostPort};
15+
use crate::net::{BadWarpUrl, Scheme, SchemeHostPort};
1616

1717
#[test]
1818
fn parse_insecure_warp_url() {
@@ -51,11 +51,11 @@ fn parse_secure_warp_url() {
5151
#[test]
5252
fn parse_unqualified_warp_url() {
5353
let result = "localhost:8080".parse::<SchemeHostPort>();
54-
assert_eq!(result, Err(BadUrl::MissingScheme));
54+
assert_eq!(result, Err(BadWarpUrl::MissingScheme));
5555
}
5656

5757
#[test]
5858
fn parse_bad_warp_url_scheme() {
5959
let result = "ftp://localhost:8080".parse::<SchemeHostPort>();
60-
assert_eq!(result, Err(BadUrl::BadScheme("ftp".to_string())));
60+
assert_eq!(result, Err(BadWarpUrl::BadScheme("ftp".to_string())));
6161
}

runtime/swimos_remote/src/plain.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use std::sync::Arc;
2727
use tokio::net::{TcpListener, TcpStream};
2828

2929
use crate::dns::BoxDnsResolver;
30-
use crate::net::{ClientConnections, ConnResult, ListenerResult, ServerConnections};
30+
use crate::net::{ClientConnections, ConnectionResult, ListenerResult, ServerConnections};
3131

3232
/// Implementation of [`ServerConnections`] and [`ClientConnections`], using [`TcpListener`]
3333
/// and [`TcpStream`] from Tokio.
@@ -42,7 +42,7 @@ impl TokioPlainTextNetworking {
4242
}
4343
}
4444

45-
async fn bind_to(addr: SocketAddr) -> ConnResult<(SocketAddr, TcpListener)> {
45+
async fn bind_to(addr: SocketAddr) -> ConnectionResult<(SocketAddr, TcpListener)> {
4646
let listener = TcpListener::bind(addr).await?;
4747
let addr = listener.local_addr()?;
4848
Ok((addr, listener))
@@ -58,7 +58,7 @@ impl ClientConnections for TokioPlainTextNetworking {
5858
scheme: Scheme,
5959
_host: Option<&str>,
6060
addr: SocketAddr,
61-
) -> BoxFuture<'static, ConnResult<Self::ClientSocket>> {
61+
) -> BoxFuture<'static, ConnectionResult<Self::ClientSocket>> {
6262
async move {
6363
match scheme {
6464
Scheme::Ws => Ok(TcpStream::connect(addr).await?),
@@ -89,16 +89,18 @@ impl ServerConnections for TokioPlainTextNetworking {
8989
fn bind(
9090
&self,
9191
addr: SocketAddr,
92-
) -> BoxFuture<'static, ConnResult<(SocketAddr, Self::ListenerType)>> {
92+
) -> BoxFuture<'static, ConnectionResult<(SocketAddr, Self::ListenerType)>> {
9393
bind_to(addr).boxed()
9494
}
9595
}
9696

97+
/// Wrapper around [`TcpListener`] that attaches the [`Scheme`] used for the incoming connection
98+
/// to the stream and peer address.
9799
#[pin_project]
98100
#[derive(Debug)]
99-
pub struct WithPeer(#[pin] TcpListener);
101+
pub struct WithScheme(#[pin] TcpListener);
100102

101-
impl Stream for WithPeer {
103+
impl Stream for WithScheme {
102104
type Item = ListenerResult<(TcpStream, Scheme, SocketAddr)>;
103105

104106
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
@@ -110,9 +112,9 @@ impl Stream for WithPeer {
110112
}
111113

112114
impl Listener<TcpStream> for TcpListener {
113-
type AcceptStream = WithPeer;
115+
type AcceptStream = WithScheme;
114116

115117
fn into_stream(self) -> Self::AcceptStream {
116-
WithPeer(self)
118+
WithScheme(self)
117119
}
118120
}

runtime/swimos_remote/src/tls/config/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub enum CertFormat {
1818
Der,
1919
}
2020

21-
/// An (unvalidated)) TLS certificate (or list of certificates for a PEM file).
21+
/// An unvalidated TLS certificate (or list of certificates for a PEM file).
2222
pub struct CertificateFile {
2323
pub format: CertFormat,
2424
pub body: Vec<u8>,
@@ -41,7 +41,7 @@ impl CertificateFile {
4141
/// A chain of TLS certificates (starting with the server certificate and ending with the CA).
4242
pub struct CertChain(pub Vec<CertificateFile>);
4343

44-
/// An (unvalidated) private key for a server.
44+
/// An unvalidated private key for a server.
4545
pub struct PrivateKey {
4646
pub format: CertFormat,
4747
pub body: Vec<u8>,

runtime/swimos_remote/src/tls/errors.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,22 @@
1414

1515
use thiserror::Error;
1616

17+
/// Error type indicating that establishing a TLS connection failed.
1718
#[derive(Debug, Error)]
1819
pub enum TlsError {
20+
/// A certificate PEM file was invalid.
1921
#[error("Error reading PEM file: {0}")]
2022
InvalidPem(std::io::Error),
23+
/// The provided private key is invalid.
2124
#[error("The provided input did not contain a valid private key.")]
2225
InvalidPrivateKey,
26+
/// Certificate validation failed.
2327
#[error("Invalid certificate: {0}")]
2428
BadCertificate(#[from] webpki::Error),
29+
/// The provided host name was invalid.
2530
#[error("Invalid DNS host name.")]
2631
BadHostName,
32+
/// Performing the TLS handshake failed.
2733
#[error("TLS handshake failed: {0}")]
2834
HandshakeFailed(std::io::Error),
2935
}

runtime/swimos_remote/src/tls/maybe/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use tokio::{
2525
};
2626
use tokio_rustls::TlsStream;
2727

28-
/// Either a simple, unencrypted [`tokio::TcpStream`] or a [`TlsStream`].
28+
/// Either a simple, unencrypted [`TcpStream`] or a [`TlsStream`].
2929
#[pin_project(project = MaybeTlsProj)]
3030
pub enum MaybeTlsStream {
3131
Plain(#[pin] TcpStream),

0 commit comments

Comments
 (0)