Existing distant-net tests now compile and pass (need to add more)

pull/146/head
Chip Senkbeil 2 years ago
parent d54e624841
commit 9bdec51080
No known key found for this signature in database
GPG Key ID: 35EF1F8EC72A4131

@ -109,7 +109,7 @@ impl FromStr for Host {
/// ### Examples
///
/// ```
/// # use distant_core::Host;
/// # use distant_net::common::Host;
/// # use std::net::{Ipv4Addr, Ipv6Addr};
/// // IPv4 address
/// assert_eq!("127.0.0.1".parse(), Ok(Host::Ipv4(Ipv4Addr::new(127, 0, 0, 1))));

@ -198,6 +198,13 @@ impl<'de> Deserialize<'de> for Map {
}
}
/// Generates a new [`Map`] of key/value pairs based on literals.
///
/// ```
/// use distant_net::map;
///
/// let _map = map!("key" -> "value", "key2" -> "value2");
/// ```
#[macro_export]
macro_rules! map {
($($key:literal -> $value:literal),* $(,)?) => {{

@ -314,6 +314,7 @@ mod tests {
use super::*;
use crate::common::{FramedTransport, Transport};
use crate::server::ServerReply;
use crate::{boxed_connect_handler, boxed_launch_handler};
use tokio::sync::mpsc;
fn test_config() -> Config {
@ -370,7 +371,7 @@ mod tests {
async fn launch_should_fail_if_handler_tied_to_scheme_fails() {
let mut config = test_config();
let handler: Box<dyn LaunchHandler> = Box::new(|_: &_, _: &_, _: &mut _| async {
let handler = boxed_launch_handler!(|_a, _b, _c| {
Err(io::Error::new(io::ErrorKind::Other, "test failure"))
});
@ -391,11 +392,9 @@ mod tests {
async fn launch_should_return_new_destination_on_success() {
let mut config = test_config();
let handler: Box<dyn LaunchHandler> = {
Box::new(|_: &_, _: &_, _: &mut _| async {
Ok("scheme2://host2".parse::<Destination>().unwrap())
})
};
let handler = boxed_launch_handler!(|_a, _b, _c| {
Ok("scheme2://host2".parse::<Destination>().unwrap())
});
config.launch_handlers.insert("scheme".to_string(), handler);
@ -430,7 +429,7 @@ mod tests {
async fn connect_should_fail_if_handler_tied_to_scheme_fails() {
let mut config = test_config();
let handler: Box<dyn ConnectHandler> = Box::new(|_: &_, _: &_, _: &mut _| async {
let handler = boxed_connect_handler!(|_a, _b, _c| {
Err(io::Error::new(io::ErrorKind::Other, "test failure"))
});
@ -453,8 +452,7 @@ mod tests {
async fn connect_should_return_id_of_new_connection_on_success() {
let mut config = test_config();
let handler: Box<dyn ConnectHandler> =
Box::new(|_: &_, _: &_, _: &mut _| async { Ok(detached_framed_transport()) });
let handler = boxed_connect_handler!(|_a, _b, _c| { Ok(detached_framed_transport()) });
config
.connect_handlers

@ -39,6 +39,47 @@ where
}
}
/// Generates a new [`LaunchHandler`] for the provided anonymous function in the form of
///
/// ```
/// use distant_net::boxed_launch_handler;
///
/// let _handler = boxed_launch_handler!(|destination, options, authenticator| {
/// todo!("Implement handler logic.");
/// });
///
/// let _handler = boxed_launch_handler!(|destination, options, authenticator| async {
/// todo!("We support async within as well regardless of the keyword!");
/// });
///
/// let _handler = boxed_launch_handler!(move |destination, options, authenticator| {
/// todo!("You can also explicitly mark to move into the closure");
/// });
/// ```
#[macro_export]
macro_rules! boxed_launch_handler {
(|$destination:ident, $options:ident, $authenticator:ident| $(async)? $body:block) => {{
let x: $crate::manager::BoxedLaunchHandler = Box::new(
|$destination: &$crate::common::Destination,
$options: &$crate::common::Map,
$authenticator: &mut dyn $crate::common::authentication::Authenticator| async {
$body
},
);
x
}};
(move |$destination:ident, $options:ident, $authenticator:ident| $(async)? $body:block) => {{
let x: $crate::manager::BoxedLaunchHandler = Box::new(
move |$destination: &$crate::common::Destination,
$options: &$crate::common::Map,
$authenticator: &mut dyn $crate::common::authentication::Authenticator| async move {
$body
},
);
x
}};
}
/// Represents an interface to perform a connection to some remote `destination`.
///
/// * `destination` is the location of the server to connect to.
@ -71,3 +112,200 @@ where
self(destination, options, authenticator).await
}
}
/// Generates a new [`ConnectHandler`] for the provided anonymous function in the form of
///
/// ```
/// use distant_net::boxed_connect_handler;
///
/// let _handler = boxed_connect_handler!(|destination, options, authenticator| {
/// todo!("Implement handler logic.");
/// });
///
/// let _handler = boxed_connect_handler!(|destination, options, authenticator| async {
/// todo!("We support async within as well regardless of the keyword!");
/// });
///
/// let _handler = boxed_connect_handler!(move |destination, options, authenticator| {
/// todo!("You can also explicitly mark to move into the closure");
/// });
/// ```
#[macro_export]
macro_rules! boxed_connect_handler {
(|$destination:ident, $options:ident, $authenticator:ident| $(async)? $body:block) => {{
let x: $crate::manager::BoxedConnectHandler = Box::new(
|$destination: &$crate::common::Destination,
$options: &$crate::common::Map,
$authenticator: &mut dyn $crate::common::authentication::Authenticator| async {
$body
},
);
x
}};
(move |$destination:ident, $options:ident, $authenticator:ident| $(async)? $body:block) => {{
let x: $crate::manager::BoxedConnectHandler = Box::new(
move |$destination: &$crate::common::Destination,
$options: &$crate::common::Map,
$authenticator: &mut dyn $crate::common::authentication::Authenticator| async move {
$body
},
);
x
}};
}
#[cfg(test)]
mod tests {
use super::*;
use crate::common::FramedTransport;
use test_log::test;
#[inline]
fn test_destination() -> Destination {
"scheme://host:1234".parse().unwrap()
}
#[inline]
fn test_options() -> Map {
Map::default()
}
#[inline]
fn test_authenticator() -> impl Authenticator {
FramedTransport::pair(1).0
}
#[test(tokio::test)]
async fn boxed_launch_handler_should_generate_valid_boxed_launch_handler() {
let handler = boxed_launch_handler!(|_destination, _options, _authenticator| {
Err(io::Error::from(io::ErrorKind::Other))
});
assert_eq!(
handler
.launch(
&test_destination(),
&test_options(),
&mut test_authenticator()
)
.await
.unwrap_err()
.kind(),
io::ErrorKind::Other
);
let handler = boxed_launch_handler!(|_destination, _options, _authenticator| async {
Err(io::Error::from(io::ErrorKind::Other))
});
assert_eq!(
handler
.launch(
&test_destination(),
&test_options(),
&mut test_authenticator()
)
.await
.unwrap_err()
.kind(),
io::ErrorKind::Other
);
let handler = boxed_launch_handler!(move |_destination, _options, _authenticator| {
Err(io::Error::from(io::ErrorKind::Other))
});
assert_eq!(
handler
.launch(
&test_destination(),
&test_options(),
&mut test_authenticator()
)
.await
.unwrap_err()
.kind(),
io::ErrorKind::Other
);
let handler = boxed_launch_handler!(move |_destination, _options, _authenticator| async {
Err(io::Error::from(io::ErrorKind::Other))
});
assert_eq!(
handler
.launch(
&test_destination(),
&test_options(),
&mut test_authenticator()
)
.await
.unwrap_err()
.kind(),
io::ErrorKind::Other
);
}
#[test(tokio::test)]
async fn boxed_connect_handler_should_generate_valid_boxed_connect_handler() {
let handler = boxed_connect_handler!(|_destination, _options, _authenticator| {
Err(io::Error::from(io::ErrorKind::Other))
});
assert_eq!(
handler
.connect(
&test_destination(),
&test_options(),
&mut test_authenticator()
)
.await
.unwrap_err()
.kind(),
io::ErrorKind::Other
);
let handler = boxed_connect_handler!(|_destination, _options, _authenticator| async {
Err(io::Error::from(io::ErrorKind::Other))
});
assert_eq!(
handler
.connect(
&test_destination(),
&test_options(),
&mut test_authenticator()
)
.await
.unwrap_err()
.kind(),
io::ErrorKind::Other
);
let handler = boxed_connect_handler!(move |_destination, _options, _authenticator| {
Err(io::Error::from(io::ErrorKind::Other))
});
assert_eq!(
handler
.connect(
&test_destination(),
&test_options(),
&mut test_authenticator()
)
.await
.unwrap_err()
.kind(),
io::ErrorKind::Other
);
let handler = boxed_connect_handler!(move |_destination, _options, _authenticator| async {
Err(io::Error::from(io::ErrorKind::Other))
});
assert_eq!(
handler
.connect(
&test_destination(),
&test_options(),
&mut test_authenticator()
)
.await
.unwrap_err()
.kind(),
io::ErrorKind::Other
);
}
}

Loading…
Cancel
Save