Skip to content

Commit ad280bb

Browse files
authored
Merge pull request #22 from PlexSheep/devel
Refactor: remove ipv4/v6 flags
2 parents 5b78be8 + c628125 commit ad280bb

File tree

4 files changed

+37
-86
lines changed

4 files changed

+37
-86
lines changed

src/analyze.rs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@
3030
//! - Store metadata (hashes, versions)
3131
3232
use deepsize::DeepSizeOf;
33-
use tracing::error;
3433

3534
use crate::errors::AnalysisError;
36-
use crate::records::{Check, CheckFlag, CheckType};
35+
use crate::records::{Check, CheckType, IpType};
3736
use crate::store::Store;
3837

3938
use std::fmt::{Display, Write};
@@ -139,9 +138,9 @@ pub fn analyze(store: &Store) -> Result<String, AnalysisError> {
139138
barrier(&mut f, "ICMP")?;
140139
generic_type_analyze(store, &mut f, CheckType::Icmp)?;
141140
barrier(&mut f, "IPv4")?;
142-
gereric_ip_analyze(store, &mut f, CheckFlag::IPv4)?;
141+
gereric_ip_analyze(store, &mut f, IpType::V4)?;
143142
barrier(&mut f, "IPv6")?;
144-
gereric_ip_analyze(store, &mut f, CheckFlag::IPv6)?;
143+
gereric_ip_analyze(store, &mut f, IpType::V6)?;
145144
barrier(&mut f, "Outages")?;
146145
outages(store, &mut f)?;
147146
barrier(&mut f, "Store Metadata")?;
@@ -337,25 +336,11 @@ fn generalized(store: &Store, f: &mut String) -> Result<(), AnalysisError> {
337336
/// Prints warning to stderr if:
338337
/// - Check has both IPv4 and IPv6 flags set
339338
/// - Check has no IP version flags set
340-
fn gereric_ip_analyze(
341-
store: &Store,
342-
f: &mut String,
343-
ip_check_flag: CheckFlag,
344-
) -> Result<(), AnalysisError> {
345-
if ![CheckFlag::IPv4, CheckFlag::IPv6].contains(&ip_check_flag) {
346-
panic!("check flag is not IPv4 or IPv6: {ip_check_flag:?}");
347-
}
339+
fn gereric_ip_analyze(store: &Store, f: &mut String, ip_type: IpType) -> Result<(), AnalysisError> {
348340
let all: Vec<&Check> = store
349341
.checks()
350342
.iter()
351-
.filter(|c| match c.ip_type() {
352-
Ok(ip) => ip,
353-
Err(err) => {
354-
error!("check '{}' has bad flags: {err}", c.get_hash());
355-
return false;
356-
}
357-
} == ip_check_flag
358-
)
343+
.filter(|c| c.ip_type() == ip_type)
359344
.collect();
360345
let successes: Vec<&Check> = all.clone().into_iter().filter(|c| c.is_success()).collect();
361346
analyze_check_type_set(f, &all, &successes)?;

src/bins/daemon.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ fn wakeup(store: &mut Store) -> Result<(), RunError> {
103103

104104
let mut buf = String::new();
105105
display_group(&store.make_checks(), &mut buf)?;
106-
info!("{buf}");
106+
info!("Made checks\n{buf}");
107107

108108
if let Err(err) = store.save() {
109109
error!("error while saving to file: {err:}");

src/records.rs

Lines changed: 25 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ use tracing::error;
5050

5151
use crate::errors::StoreError;
5252

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+
5364
/// List of target IP addresses used for connectivity checks.
5465
///
5566
/// # Warning
@@ -63,8 +74,7 @@ flags! {
6374
///
6475
/// Uses a bitflag system to efficiently store multiple properties:
6576
/// - 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)
6878
#[derive(Hash, Deserialize, Serialize)]
6979
pub enum CheckFlag: u16 {
7080
/// If this is not set, the check will be considered failed
@@ -74,11 +84,6 @@ flags! {
7484
/// Failure because the destination is unreachable
7585
Unreachable = 0b0000_0000_0000_0100,
7686

77-
/// The Check used IPv4
78-
IPv4 = 0b0000_0001_0000_0000,
79-
/// The Check used IPv6
80-
IPv6 = 0b0000_0010_0000_0000,
81-
8287
/// The Check used HTTP/HTTPS
8388
TypeHTTP = 0b0001_0000_0000_0000,
8489
/// Check type was ICMP (ping)
@@ -136,11 +141,6 @@ impl CheckType {
136141
remote,
137142
);
138143

139-
match remote {
140-
IpAddr::V4(_) => check.add_flag(CheckFlag::IPv4),
141-
IpAddr::V6(_) => check.add_flag(CheckFlag::IPv6),
142-
}
143-
144144
match self {
145145
#[cfg(feature = "http")]
146146
Self::Http => {
@@ -365,57 +365,13 @@ impl Check {
365365

366366
/// Determines whether the check used IPv4 or IPv6.
367367
///
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.
370369
///
371370
/// # Returns
372371
///
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)
419375
}
420376
}
421377

@@ -437,6 +393,15 @@ impl Display for Check {
437393
}
438394
}
439395

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+
440405
/// Display a formatted list of checks.
441406
///
442407
/// Each check is formatted with:

src/store.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use std::str::FromStr;
2626

2727
use deepsize::DeepSizeOf;
2828
use serde::{Deserialize, Serialize};
29-
use tracing::{error, trace, warn};
29+
use tracing::{error, info, trace, warn};
3030

3131
use crate::errors::StoreError;
3232
use crate::records::{Check, CheckType, TARGETS};
@@ -109,12 +109,12 @@ impl From<Version> for u8 {
109109

110110
impl Version {
111111
/// Current version of the store format
112-
pub const CURRENT: Self = Version::new(0);
112+
pub const CURRENT: Self = Version::new(1);
113113

114114
/// List of supported store format versions
115115
///
116116
/// Used for compatibility checking when loading stores.
117-
pub const SUPPROTED: &[Self] = &[Version::new(0)];
117+
pub const SUPPROTED: &[Self] = &[Version::new(0), Version::new(1)];
118118

119119
/// Creates a new Version with the given raw version number
120120
pub(crate) const fn new(raw: u8) -> Self {
@@ -358,9 +358,9 @@ impl Store {
358358

359359
// TODO: somehow account for old versions that are not compatible with the store struct
360360
if store.version != Version::CURRENT {
361-
error!("The store that was loaded is not of the current version:\nstore has {} but the current version is {}", store.version, Version::CURRENT);
361+
warn!("The store that was loaded is not of the current version:\nstore has {} but the current version is {}", store.version, Version::CURRENT);
362362
if Version::SUPPROTED.contains(&store.version) {
363-
error!("The old store version is still supported, migrating to newer version");
363+
warn!("The old store version is still supported, migrating to newer version (in memory, can be made permanent by saving)");
364364
store.version = Version::CURRENT;
365365
} else {
366366
error!("The store version is not supported");
@@ -385,6 +385,7 @@ impl Store {
385385
/// - Write fails
386386
/// - Serialization fails
387387
pub fn save(&self) -> Result<(), StoreError> {
388+
info!("Saving the store");
388389
let file = match fs::File::options()
389390
.read(false)
390391
.write(true)

0 commit comments

Comments
 (0)