@@ -50,6 +50,17 @@ use tracing::error;
50
50
51
51
use crate :: errors:: StoreError ;
52
52
53
+ /// Type of [IpAddr]
54
+ ///
55
+ /// This enum can be used to work with just abstract IP versions, not whole [Ip Addresses](IpAddr).
56
+ #[ derive( Debug , PartialEq , Eq , Hash , Deserialize , Serialize , Clone , Copy , DeepSizeOf ) ]
57
+ pub enum IpType {
58
+ /// Type is IPv4
59
+ V4 ,
60
+ /// Type is IPv6
61
+ V6 ,
62
+ }
63
+
53
64
/// List of target IP addresses used for connectivity checks.
54
65
///
55
66
/// # Warning
@@ -63,8 +74,7 @@ flags! {
63
74
///
64
75
/// Uses a bitflag system to efficiently store multiple properties:
65
76
/// - Result flags (bits 0-7): Success, failure reasons
66
- /// - Protocol flags (bits 8-11): IPv4/IPv6
67
- /// - Type flags (bits 12-15): Check type (HTTP, ICMP, DNS)
77
+ /// - Type flags (bits 8-15): Check type (HTTP, ICMP, DNS)
68
78
#[ derive( Hash , Deserialize , Serialize ) ]
69
79
pub enum CheckFlag : u16 {
70
80
/// If this is not set, the check will be considered failed
@@ -74,11 +84,6 @@ flags! {
74
84
/// Failure because the destination is unreachable
75
85
Unreachable = 0b0000_0000_0000_0100 ,
76
86
77
- /// The Check used IPv4
78
- IPv4 = 0b0000_0001_0000_0000 ,
79
- /// The Check used IPv6
80
- IPv6 = 0b0000_0010_0000_0000 ,
81
-
82
87
/// The Check used HTTP/HTTPS
83
88
TypeHTTP = 0b0001_0000_0000_0000 ,
84
89
/// Check type was ICMP (ping)
@@ -136,11 +141,6 @@ impl CheckType {
136
141
remote,
137
142
) ;
138
143
139
- match remote {
140
- IpAddr :: V4 ( _) => check. add_flag ( CheckFlag :: IPv4 ) ,
141
- IpAddr :: V6 ( _) => check. add_flag ( CheckFlag :: IPv6 ) ,
142
- }
143
-
144
144
match self {
145
145
#[ cfg( feature = "http" ) ]
146
146
Self :: Http => {
@@ -365,57 +365,13 @@ impl Check {
365
365
366
366
/// Determines whether the check used IPv4 or IPv6.
367
367
///
368
- /// Examines the check's flags to determine which IP version was used.
369
- /// A check should have either IPv4 or IPv6 flag set, but not both.
368
+ /// Examines the [check's](Check) [target](Check::target) to determine which IP version was used.
370
369
///
371
370
/// # Returns
372
371
///
373
- /// * `CheckFlag::IPv4` - Check used IPv4
374
- /// * `CheckFlag::IPv6` - Check used IPv6
375
- ///
376
- /// # Errors
377
- ///
378
- /// * Returns [`StoreError::AmbiguousFlags`] if both IPv4 and IPv6 flags are set,
379
- /// as this represents an invalid state that should never occur.
380
- ///
381
- /// * Returns [`StoreError::MissingFlag`] if neither IPv4 or IPv6 flags are set,
382
- /// as this represents an invalid state that should never occur.
383
- ///
384
- /// # Examples
385
- ///
386
- /// ```rust
387
- /// use netpulse::records::{Check, CheckFlag};
388
- /// use flagset::FlagSet;
389
- ///
390
- /// let mut check = Check::new(std::time::SystemTime::now(), FlagSet::default(), None, "1.1.1.1".parse().unwrap());
391
- ///
392
- /// assert!(check.ip_type().is_err()); // we haven't set the IP flags! We need to set either IPv4 or IPv6
393
- ///
394
- /// check.add_flag(CheckFlag::IPv4);
395
- ///
396
- /// match check.ip_type().unwrap() {
397
- /// CheckFlag::IPv4 => println!("IPv4 check"),
398
- /// CheckFlag::IPv6 => println!("IPv6 check"),
399
- /// _ => unreachable!()
400
- /// }
401
- ///
402
- /// check.add_flag(CheckFlag::IPv6); // But what if we now also add IPv6?
403
- ///
404
- /// assert!(check.ip_type().is_err()); // Oh no! Now it's ambiguos
405
- /// ```
406
- pub fn ip_type ( & self ) -> Result < CheckFlag , StoreError > {
407
- let flags = self . flags ( ) ;
408
- if flags. contains ( CheckFlag :: IPv4 ) && flags. contains ( CheckFlag :: IPv6 ) {
409
- Err ( StoreError :: AmbiguousFlags (
410
- CheckFlag :: IPv4 | CheckFlag :: IPv6 ,
411
- ) )
412
- } else if flags. contains ( CheckFlag :: IPv4 ) {
413
- Ok ( CheckFlag :: IPv4 )
414
- } else if flags. contains ( CheckFlag :: IPv6 ) {
415
- Ok ( CheckFlag :: IPv6 )
416
- } else {
417
- Err ( StoreError :: MissingFlag ( CheckFlag :: IPv4 | CheckFlag :: IPv6 ) )
418
- }
372
+ /// The [IpType] that was used
373
+ pub fn ip_type ( & self ) -> IpType {
374
+ IpType :: from ( self . target )
419
375
}
420
376
}
421
377
@@ -437,6 +393,15 @@ impl Display for Check {
437
393
}
438
394
}
439
395
396
+ impl From < IpAddr > for IpType {
397
+ fn from ( value : IpAddr ) -> Self {
398
+ match value {
399
+ IpAddr :: V4 ( _) => Self :: V4 ,
400
+ IpAddr :: V6 ( _) => Self :: V6 ,
401
+ }
402
+ }
403
+ }
404
+
440
405
/// Display a formatted list of checks.
441
406
///
442
407
/// Each check is formatted with:
0 commit comments