use std::process::Command;
use regex::Regex;
use crate::round_trip_statistics::RoundTripStatistics;
pub fn ping_command(args: &[&str]) -> std::io::Result<String> {
let output = Command::new("ping")
.args(args)
.output()?;
if output.status.success() {
return Ok(String::from_utf8_lossy(&output.stdout).into())
}
return Err(
std::io::Error::new(
std::io::ErrorKind::Other,
"Ping command output is not a success."
)
)
}
pub fn ping_command_into_round_trip_statistics(args: &[&str]) -> std::io::Result<RoundTripStatistics> {
let result = ping_command(args)?;
return Ok(RoundTripStatistics.from_str(&result))
}
pub fn ping_command_args(host: &str) -> Vec<&str> {
if cfg!(target_family = "unix") {
vec!["-c", "1", "-W", "5", host]
}
else
if cfg!(target_family = "windows") {
vec!["-n", "1", "-w", "5000", host]
}
else {
vec![]
}
}
#[cfg(test)]
mod tests {
use super::*;
mod ping_command_args {
use super::*;
#[test]
fn test_ping_command_args() {
let x = ping_command_args("localhost");
if cfg!(target_family = "unix") {
assert_eq!(x, ["-c", "1", "-W", "5", host]);
}
else
if cfg!(target_family = "windows") {
assert_eq!(x, "-n", "1", "-w", "5000", host);
}
else {
unreachable!()
}
}
}
}