Skip to main content

pinger/
macos.rs

1use crate::bsd::parse_bsd;
2use crate::{PingCreationError, PingOptions, PingResult, Pinger};
3use lazy_regex::*;
4
5pub static RE: Lazy<Regex> = lazy_regex!(r"time=(?:(?P<ms>[0-9]+).(?P<ns>[0-9]+)\s+ms)");
6
7pub struct MacOSPinger {
8    options: PingOptions,
9}
10
11impl Pinger for MacOSPinger {
12    fn from_options(options: PingOptions) -> Result<Self, PingCreationError>
13    where
14        Self: Sized,
15    {
16        Ok(Self { options })
17    }
18
19    fn parse_fn(&self) -> fn(String) -> Option<PingResult> {
20        parse_bsd
21    }
22
23    fn ping_args(&self) -> (&str, Vec<String>) {
24        let cmd = if self.options.target.is_ipv6() {
25            "ping6"
26        } else {
27            "ping"
28        };
29        let mut args = vec![
30            format!(
31                "-i{:.1}",
32                self.options.interval.as_millis() as f32 / 1_000_f32
33            ),
34            self.options.target.to_string(),
35        ];
36        if let Some(interface) = &self.options.interface {
37            args.push("-b".into());
38            args.push(interface.clone());
39        }
40
41        if let Some(raw_args) = &self.options.raw_arguments {
42            args.extend(raw_args.iter().cloned());
43        }
44
45        (cmd, args)
46    }
47}