@@ -73,9 +73,11 @@ pub const ENV_PATH: &str = "NETPULSE_STORE_PATH";
73
73
#[ derive(
74
74
Debug , PartialEq , Eq , Hash , Deserialize , Serialize , Copy , Clone , DeepSizeOf , PartialOrd , Ord ,
75
75
) ]
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 ,
79
81
}
80
82
81
83
/// Main storage type for netpulse check results.
@@ -93,19 +95,26 @@ pub struct Store {
93
95
94
96
impl Display for Version {
95
97
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
96
- write ! ( f, "{}" , self . inner )
98
+ write ! ( f, "{}" , self . raw ( ) )
97
99
}
98
100
}
99
101
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
+ } )
103
112
}
104
113
}
105
114
106
115
impl From < Version > for u8 {
107
116
fn from ( value : Version ) -> Self {
108
- value. inner
117
+ value. raw ( )
109
118
}
110
119
}
111
120
@@ -118,21 +127,36 @@ impl Version {
118
127
/// Used for compatibility checking when loading stores.
119
128
pub const SUPPROTED : & [ Self ] = & [ Self :: V0 , Self :: V1 , Self :: V2 ] ;
120
129
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
128
133
}
129
134
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
+ /// ```
130
155
pub fn next ( & self ) -> Option < Self > {
131
156
Some ( match * self {
132
157
Self :: V0 => Self :: V1 ,
133
158
Self :: V1 => Self :: V2 ,
134
159
Self :: V2 => return None ,
135
- _ => unreachable ! ( "working with a version that does not exist" ) ,
136
160
} )
137
161
}
138
162
}
0 commit comments