Skip to content

Commit 25258c3

Browse files
committed
refactor(store): Version is now an enum
1 parent 4395765 commit 25258c3

File tree

2 files changed

+44
-16
lines changed

2 files changed

+44
-16
lines changed

src/errors.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ pub enum StoreError {
9494
/// that one of these should be set.
9595
#[error("Check is missing at least one of these flags: {0:?}")]
9696
MissingFlag(FlagSet<CheckFlag>),
97+
/// Occurs when trying to convert an arbitrary [u8] to a [Version](crate::store::Version) that
98+
/// is not defined. Only known [Versions][crate::store::Version] are valid.
99+
#[error("Tried to load a store version that does not exist: {0}")]
100+
BadStoreVersion(u8),
97101
}
98102

99103
/// Errors that can occur during network checks.

src/store.rs

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,11 @@ pub const ENV_PATH: &str = "NETPULSE_STORE_PATH";
7373
#[derive(
7474
Debug, PartialEq, Eq, Hash, Deserialize, Serialize, Copy, Clone, DeepSizeOf, PartialOrd, Ord,
7575
)]
76-
pub struct Version {
77-
/// Raw version number as u8
78-
inner: u8,
76+
#[allow(missing_docs)] // It's just versions man
77+
pub enum Version {
78+
V0 = 0,
79+
V1 = 1,
80+
V2 = 2,
7981
}
8082

8183
/// Main storage type for netpulse check results.
@@ -93,19 +95,26 @@ pub struct Store {
9395

9496
impl Display for Version {
9597
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
96-
write!(f, "{}", self.inner)
98+
write!(f, "{}", self.raw())
9799
}
98100
}
99101

100-
impl From<u8> for Version {
101-
fn from(value: u8) -> Self {
102-
Self::new(value)
102+
impl TryFrom<u8> for Version {
103+
type Error = StoreError;
104+
105+
fn try_from(value: u8) -> Result<Self, Self::Error> {
106+
Ok(match value {
107+
0 => Self::V0,
108+
1 => Self::V1,
109+
2 => Self::V2,
110+
_ => return Err(StoreError::BadStoreVersion(value)),
111+
})
103112
}
104113
}
105114

106115
impl From<Version> for u8 {
107116
fn from(value: Version) -> Self {
108-
value.inner
117+
value.raw()
109118
}
110119
}
111120

@@ -118,21 +127,36 @@ impl Version {
118127
/// Used for compatibility checking when loading stores.
119128
pub const SUPPROTED: &[Self] = &[Self::V0, Self::V1, Self::V2];
120129

121-
pub const V0: Self = Version::new(0);
122-
pub const V1: Self = Version::new(1);
123-
pub const V2: Self = Version::new(2);
124-
125-
/// Creates a new Version with the given raw version number
126-
pub(crate) const fn new(raw: u8) -> Self {
127-
Self { inner: raw }
130+
/// Gets the raw [Version] as [u8]
131+
pub const fn raw(&self) -> u8 {
132+
*self as u8
128133
}
129134

135+
/// Returns the next sequential [Version], if one exists.
136+
///
137+
/// Used for version migration logic to determine the next version to upgrade to.
138+
///
139+
/// # Returns
140+
///
141+
/// * `Some(Version)` - The next version in sequence:
142+
/// - V0 → V1
143+
/// - V1 → V2
144+
/// - ...
145+
/// * `None` - If current version is the latest version
146+
///
147+
/// # Examples
148+
///
149+
/// ```rust
150+
/// # use netpulse::store::Version;
151+
/// assert_eq!(Version::V0.next(), Some(Version::V1));
152+
/// assert_eq!(Version::V1.next(), Some(Version::V2));
153+
/// assert_eq!(Version::CURRENT.next(), None); // No version after latest
154+
/// ```
130155
pub fn next(&self) -> Option<Self> {
131156
Some(match *self {
132157
Self::V0 => Self::V1,
133158
Self::V1 => Self::V2,
134159
Self::V2 => return None,
135-
_ => unreachable!("working with a version that does not exist"),
136160
})
137161
}
138162
}

0 commit comments

Comments
 (0)