Skip to content

Commit a8eeeb3

Browse files
Merge pull request #10 from brummer-simon/fix-clippy-lints
Fix lints found by clippy and some minor cleanup
2 parents 7b38dfd + 708e7de commit a8eeeb3

File tree

6 files changed

+39
-36
lines changed

6 files changed

+39
-36
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "reachable"
3-
version = "0.2.1"
3+
version = "0.2.2"
44
edition = "2021"
55
authors = ["Simon Brummer <[email protected]>"]
66
description = "Check if a Target (ICMP, TCP, custom) is reachable."

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ an async task executor to perform availability checks of "Targets" on a regular
77
## Usage
88

99
With this crate you can easily check if a computer is currently reachable over the network.
10-
Since all targets are implementations of trait "Target" the availabilty check behavior is customizable.
10+
Since all targets are implementations of trait "Target" the availability check behavior is customizable.
1111
For example, it is easy to implement a custom Target to check if a Process is
1212
running or not.
1313

@@ -19,7 +19,7 @@ use std::str::FromStr;
1919
use reachable::*;
2020

2121
fn main() {
22-
// Construct ICMP Target and if its availabile
22+
// Construct ICMP Target check if the target is availabile
2323
let icmp_target = IcmpTarget::from_str("www.google.de").unwrap();
2424
match icmp_target.check_availability() {
2525
Ok(status) => println!("{} is {}", icmp_target.get_id(), status),

examples/usage/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::str::FromStr;
99
use reachable::*;
1010

1111
fn main() {
12-
// Construct ICMP Target and if its availability
12+
// Construct ICMP Target check if the target is availabile
1313
let icmp_target = IcmpTarget::from_str("www.google.de").unwrap();
1414
match icmp_target.check_availability() {
1515
Ok(status) => println!("{} is {}", icmp_target.get_id(), status),

src/async_target.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@ use tokio::time::{self};
2323
/// check and the current availability check
2424
pub type OldStatus = Status;
2525

26+
/// Type for a boxed trait object implementing [Target]
27+
type BoxedTarget<'a> = Box<dyn Target + Send + 'a>;
28+
29+
/// Type containing a boxed trait object implementing [FnMut] that is called with each async check.
30+
type BoxedHandler<'a> = Box<dyn FnMut(&dyn Target, Status, OldStatus, Option<CheckTargetError>) + Send + 'a>;
31+
2632
/// Struct storing all data used during asynchronous execution.
2733
///
2834
/// For async check execution, wrap the instances of [Target] in [AsyncTarget] and hand them to
2935
/// [AsyncTargetExecutor::start].
3036
pub struct AsyncTarget<'a> {
31-
target: Box<dyn Target + Send + 'a>,
32-
check_handler: Box<dyn FnMut(&dyn Target, Status, OldStatus, Option<CheckTargetError>) + Send + 'a>,
37+
target: BoxedTarget<'a>,
38+
check_handler: BoxedHandler<'a>,
3339
check_interval: Duration,
3440
status: Status,
3541
}
@@ -44,15 +50,11 @@ impl<'a> AsyncTarget<'a> {
4450
///
4551
/// # Returns
4652
/// Instance of [AsyncTarget].
47-
pub fn new(
48-
target: Box<dyn Target + Send + 'a>,
49-
check_handler: Box<dyn FnMut(&dyn Target, Status, OldStatus, Option<CheckTargetError>) + Send + 'a>,
50-
check_interval: Duration,
51-
) -> Self {
53+
pub fn new(target: BoxedTarget<'a>, check_handler: BoxedHandler<'a>, check_interval: Duration) -> Self {
5254
AsyncTarget {
53-
target: target,
54-
check_handler: check_handler,
55-
check_interval: check_interval,
55+
target,
56+
check_handler,
57+
check_interval,
5658
status: Status::Unknown,
5759
}
5860
}
@@ -152,13 +154,19 @@ impl AsyncTargetExecutor {
152154
}
153155
}
154156

157+
impl Default for AsyncTargetExecutor {
158+
fn default() -> Self {
159+
AsyncTargetExecutor::new()
160+
}
161+
}
162+
155163
impl Drop for AsyncTargetExecutor {
156164
fn drop(&mut self) {
157165
self.stop()
158166
}
159167
}
160168

161-
async fn check_target_periodically(mut target: AsyncTarget<'static>, mut teardown_recv: Receiver<()>) -> () {
169+
async fn check_target_periodically(mut target: AsyncTarget<'static>, mut teardown_recv: Receiver<()>) {
162170
loop {
163171
target = select! {
164172
// Teardown message was not received. Perform next check.
@@ -170,7 +178,7 @@ async fn check_target_periodically(mut target: AsyncTarget<'static>, mut teardow
170178
}
171179
}
172180

173-
async fn check_target(mut target: AsyncTarget<'static>) -> AsyncTarget<'_> {
181+
async fn check_target(mut target: AsyncTarget<'static>) -> AsyncTarget<'static> {
174182
// Setup sleep timer to wait, to prevent further execution before the check_interval elapsed.
175183
let sleep = time::sleep(target.check_interval);
176184

src/resolve_policy.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ impl ResolvePolicy {
3737
///
3838
/// # Returns
3939
/// * On success, vector containing all ip addresses the fqhn resolved to.
40-
/// * On failure, a [ResolveTargetError]. Either failed the name resolution itself or all
41-
/// addresses were filtered out according to [ResolvePolicy].
40+
/// * On failure, a [ResolveTargetError]. Either failed the name resolution itself or all addresses were filtered
41+
/// out according to [ResolvePolicy].
4242
///
4343
/// # Example
4444
/// ```

src/target.rs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ pub trait Target {
5050
///
5151
/// # Returns
5252
/// * On success, the current [Status] of this [Target].
53-
/// * On failure, a [CheckTargetError]. This error should be returned in case some internal
54-
/// error occurred.
53+
/// * On failure, a [CheckTargetError]. This error should be returned in case some internal error occurred.
5554
///
5655
/// # Notes
5756
/// This method should be implemented in a non-blocking way to improve performance then used
@@ -119,8 +118,8 @@ impl IcmpTarget {
119118
/// For more convenience use the implementations of trait "From" and "FromStr".
120119
pub fn new(fqhn: Fqhn, resolve_policy: ResolvePolicy) -> Self {
121120
IcmpTarget {
122-
fqhn: fqhn,
123-
resolve_policy: resolve_policy,
121+
fqhn,
122+
resolve_policy,
124123
}
125124
}
126125

@@ -248,10 +247,10 @@ impl TcpTarget {
248247
/// For more convenience use the implementations of trait "From" and "FromStr".
249248
pub fn new(fqhn: Fqhn, port: Port, connect_timeout: Duration, resolve_policy: ResolvePolicy) -> Self {
250249
TcpTarget {
251-
fqhn: fqhn,
252-
port: port,
253-
connect_timeout: connect_timeout,
254-
resolve_policy: resolve_policy,
250+
fqhn,
251+
port,
252+
connect_timeout,
253+
resolve_policy,
255254
}
256255
}
257256

@@ -300,16 +299,12 @@ impl Target for TcpTarget {
300299
// Network services should be able to deal with this behavior.
301300

302301
// Resolve and construct address/port pairs
303-
let addrs = self.resolve_policy.resolve(&self.fqhn)?;
304-
let sock_addrs: Vec<SocketAddr> = addrs
305-
.into_iter()
306-
.map(|addr| SocketAddr::from((addr, self.port)))
307-
.collect();
308-
309302
// Try for each address/port pair to establish a connection.
310303
// Occurring errors are treated as a sign of target is not available.
311-
let available = sock_addrs
304+
let addrs = self.resolve_policy.resolve(&self.fqhn)?;
305+
let available = addrs
312306
.into_iter()
307+
.map(|addr| SocketAddr::from((addr, self.port)))
313308
.any(|addr| TcpStream::connect_timeout(&addr, self.connect_timeout).is_ok());
314309

315310
if available {
@@ -388,8 +383,8 @@ impl FromStr for TcpTarget {
388383
let maybe_port = &s[index + 1..];
389384
match maybe_port.parse() as Result<u16, ParseIntError> {
390385
Ok(port) => {
391-
if port <= 0 {
392-
Err(ParseTargetError::from("Invalid Portnumber 0 found"))
386+
if port == 0 {
387+
Err(ParseTargetError::from("Invalid Portnumber '0' found"))
393388
} else {
394389
Ok(TcpTarget::new(
395390
fqhn,
@@ -607,7 +602,7 @@ mod tests {
607602
// Expectency: The TcpTarget returns an error if portnumber is 0 (invalid port).
608603
assert_eq!(
609604
format!("{}", TcpTarget::from_str("foo:0").unwrap_err()),
610-
"Invalid Portnumber 0 found"
605+
"Invalid Portnumber '0' found"
611606
);
612607
}
613608

0 commit comments

Comments
 (0)