1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
use crate::{Parser, PingResult, Pinger}; use regex::Regex; lazy_static! { static ref RE: Regex = Regex::new( r"(?ix-u) \s?[0-9]* # Bytes of data \sbytes\sfrom\s # bytes from \d+\.\d+\.\d+\.\d+: \s+icmp_seq=\d+ # icmp_seq \s+ttl=\d+ # ttl \s+time=(?:(?P<time>[0-9\.]+)\s+ms) # capture time" ) .unwrap(); } #[derive(Default)] pub struct MacOSPinger {} impl Pinger for MacOSPinger { fn ping_args(&self, target: String) -> Vec<String> { vec!["-i0.2".to_string(), target] } } #[derive(Default)] pub struct MacOSParser {} impl Parser for MacOSParser { fn parse(&self, line: String) -> Option<PingResult> { if line.starts_with("PING ") { return None; } if line.starts_with("Request timeout") { return Some(PingResult::Timeout); } self.extract_regex(&RE, line) } }