1+ use std:: fmt:: Display ;
12use std:: net:: IpAddr ;
23use std:: str:: FromStr ;
34use std:: time;
@@ -39,6 +40,7 @@ pub enum CheckType {
3940 Http ,
4041 IcmpV4 ,
4142 IcmpV6 ,
43+ Unknown ,
4244}
4345impl CheckType {
4446 /// Make a new [Check] of this type.
@@ -100,14 +102,19 @@ impl CheckType {
100102 }
101103}
102104
103- impl From < CheckType > for CheckFlag {
104- fn from ( value : CheckType ) -> Self {
105- match value {
106- CheckType :: Dns => CheckFlag :: TypeDns ,
107- CheckType :: Http => CheckFlag :: TypeHTTP ,
108- CheckType :: IcmpV4 => CheckFlag :: TypeIcmp ,
109- CheckType :: IcmpV6 => CheckFlag :: TypeIcmp ,
110- }
105+ impl Display for CheckType {
106+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
107+ write ! (
108+ f,
109+ "{}" ,
110+ match self {
111+ Self :: Dns => "DNS" ,
112+ Self :: Http => "HTTP(S)" ,
113+ Self :: IcmpV4 => "ICMPv4" ,
114+ Self :: IcmpV6 => "ICMPv6" ,
115+ Self :: Unknown => "Unknown" ,
116+ }
117+ )
111118 }
112119}
113120
@@ -149,13 +156,13 @@ impl Check {
149156 ///
150157 /// Ok means, it's a [Success](CheckFlag::Success), and has no weird anomalies (that this
151158 /// checks for).
152- pub fn is_ok ( & self ) -> bool {
159+ pub fn is_success ( & self ) -> bool {
153160 self . flags . contains ( CheckFlag :: Success )
154161 }
155162
156163 /// Returns the latency of this [`Check`].
157164 pub fn latency ( & self ) -> Option < u16 > {
158- if !self . is_ok ( ) {
165+ if !self . is_success ( ) {
159166 None
160167 } else {
161168 self . latency
@@ -181,6 +188,38 @@ impl Check {
181188 pub fn add_flag ( & mut self , flag : CheckFlag ) {
182189 self . flags |= flag
183190 }
191+
192+ /// Check the flags and infer the [CheckType]
193+ pub fn calc_type ( & self ) -> CheckType {
194+ if self . flags . contains ( CheckFlag :: TypeHTTP ) {
195+ CheckType :: Http
196+ } else if self . flags . contains ( CheckFlag :: TypeDns ) {
197+ CheckType :: Dns
198+ } else if self . flags . contains ( CheckFlag :: TypeIcmp ) {
199+ if self . flags . contains ( CheckFlag :: IPv4 ) {
200+ CheckType :: IcmpV4
201+ } else if self . flags . contains ( CheckFlag :: IPv6 ) {
202+ CheckType :: IcmpV6
203+ } else {
204+ eprintln ! ( "flag for ICMP is set, but not if ipv4 or ipv6 was used" ) ;
205+ CheckType :: Unknown
206+ }
207+ } else {
208+ CheckType :: Unknown
209+ }
210+ }
211+ }
212+
213+ impl Display for Check {
214+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
215+ writeln ! ( f, "Type: {}\n Ok: {}" , self . calc_type( ) , self . is_success( ) ) ?;
216+ writeln ! ( f, "Latency: {}" , {
217+ match self . latency( ) {
218+ Some ( l) => format!( "{l} ms" ) ,
219+ None => "(Error)" . to_string( ) ,
220+ }
221+ } )
222+ }
184223}
185224
186225#[ cfg( test) ]
0 commit comments