1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Copyright (c) 2026, Salesforce, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT
//! Server version parsing and comparison.
//!
//! Provides a [`ServerVersion`] struct for parsing and comparing Hyper server
//! version strings. This enables feature detection based on server capabilities.
//!
//! # Example
//!
//! ```
//! use hyperdb_api::ServerVersion;
//!
//! let v = ServerVersion::parse("0.0.19038").unwrap();
//! assert_eq!(v.major(), 0);
//! assert_eq!(v.minor(), 0);
//! assert_eq!(v.patch(), 19038);
//! assert!(v >= ServerVersion::new(0, 0, 19000));
//! ```
use std::fmt;
/// A parsed server version with comparison support.
///
/// Hyper server versions typically follow the format `major.minor.patch`,
/// sometimes with additional build metadata (e.g., `0.0.19038.r12345`).
///
/// # Ordering
///
/// Versions are compared lexicographically by (major, minor, patch).
/// The `suffix` field is ignored for comparison and ordering.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ServerVersion {
major: u32,
minor: u32,
patch: u32,
/// Optional suffix after the version numbers (e.g., "r12345", "beta1").
suffix: Option<String>,
/// The original version string.
raw: String,
}
impl ServerVersion {
/// Creates a new `ServerVersion` from components.
#[must_use]
pub fn new(major: u32, minor: u32, patch: u32) -> Self {
ServerVersion {
major,
minor,
patch,
suffix: None,
raw: format!("{major}.{minor}.{patch}"),
}
}
/// Parses a version string like "0.0.19038" or "1.2.3.r456".
///
/// Accepts various production formats:
/// - `"0.0.19038"` — standard dotted numeric
/// - `"1.2.3.r456"` — dot-separated suffix
/// - `"1.2.3-beta1"` — hyphen-separated pre-release
/// - `"v1.2.3"` — optional `v`/`V` prefix
/// - `"1.2"` — patch defaults to 0
/// - `" 1.2.3 "` — leading/trailing whitespace is trimmed
///
/// Returns `None` if the string doesn't contain at least two numeric
/// components separated by a dot.
#[must_use]
pub fn parse(s: &str) -> Option<Self> {
let trimmed = s.trim();
// Strip optional 'v' or 'V' prefix
let trimmed = trimmed
.strip_prefix('v')
.or_else(|| trimmed.strip_prefix('V'))
.unwrap_or(trimmed);
let mut parts = trimmed.splitn(4, '.');
let major: u32 = parts.next()?.parse().ok()?;
let minor: u32 = parts.next()?.parse().ok()?;
// Patch is optional (default 0)
let (patch, suffix) = if let Some(patch_str) = parts.next() {
// The patch component may contain non-numeric trailing text,
// e.g. "3-beta1" or "19038rc1". Extract leading digits as
// the patch number and treat the rest as suffix.
let num_end = patch_str
.find(|c: char| !c.is_ascii_digit())
.unwrap_or(patch_str.len());
if num_end == 0 {
// No leading digits at all (e.g. "abc") — not a valid patch
return None;
}
let patch: u32 = patch_str[..num_end].parse().ok()?;
// Build suffix from any trailing text in the patch component
// plus any remaining dot-separated part.
let patch_tail = &patch_str[num_end..];
let dot_tail = parts.next();
let suffix = match (patch_tail.is_empty(), dot_tail) {
(true, None) => None,
(true, Some(t)) => Some(t.to_string()),
(false, None) => Some(patch_tail.to_string()),
(false, Some(t)) => Some(format!("{patch_tail}.{t}")),
};
(patch, suffix)
} else {
(0, None)
};
Some(ServerVersion {
major,
minor,
patch,
suffix,
raw: s.trim().to_string(),
})
}
/// Returns the major version number.
#[must_use]
pub fn major(&self) -> u32 {
self.major
}
/// Returns the minor version number.
#[must_use]
pub fn minor(&self) -> u32 {
self.minor
}
/// Returns the patch version number.
#[must_use]
pub fn patch(&self) -> u32 {
self.patch
}
/// Returns the optional suffix (e.g., "r12345").
#[must_use]
pub fn suffix(&self) -> Option<&str> {
self.suffix.as_deref()
}
/// Returns the original version string.
#[must_use]
pub fn raw(&self) -> &str {
&self.raw
}
}
impl PartialOrd for ServerVersion {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for ServerVersion {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
(self.major, self.minor, self.patch).cmp(&(other.major, other.minor, other.patch))
}
}
impl fmt::Display for ServerVersion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.raw)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_basic() {
let v = ServerVersion::parse("0.0.19038").unwrap();
assert_eq!(v.major(), 0);
assert_eq!(v.minor(), 0);
assert_eq!(v.patch(), 19038);
assert_eq!(v.suffix(), None);
}
#[test]
fn test_parse_with_suffix() {
let v = ServerVersion::parse("1.2.3.r456").unwrap();
assert_eq!(v.major(), 1);
assert_eq!(v.minor(), 2);
assert_eq!(v.patch(), 3);
assert_eq!(v.suffix(), Some("r456"));
}
#[test]
fn test_parse_two_parts() {
let v = ServerVersion::parse("1.2").unwrap();
assert_eq!(v.major(), 1);
assert_eq!(v.minor(), 2);
assert_eq!(v.patch(), 0);
}
#[test]
fn test_parse_invalid() {
assert!(ServerVersion::parse("").is_none());
assert!(ServerVersion::parse("abc").is_none());
assert!(ServerVersion::parse("1").is_none());
assert!(ServerVersion::parse("1.2.abc").is_none());
assert!(ServerVersion::parse("vabc").is_none());
assert!(ServerVersion::parse("v").is_none());
}
#[test]
fn test_parse_v_prefix() {
let v = ServerVersion::parse("v1.2.3").unwrap();
assert_eq!(v.major(), 1);
assert_eq!(v.minor(), 2);
assert_eq!(v.patch(), 3);
assert_eq!(v.suffix(), None);
}
#[test]
fn test_parse_uppercase_v_prefix() {
let v = ServerVersion::parse("V1.2.3").unwrap();
assert_eq!(v.major(), 1);
assert_eq!(v.patch(), 3);
}
#[test]
fn test_parse_hyphen_prerelease() {
let v = ServerVersion::parse("1.2.3-beta1").unwrap();
assert_eq!(v.major(), 1);
assert_eq!(v.minor(), 2);
assert_eq!(v.patch(), 3);
assert_eq!(v.suffix(), Some("-beta1"));
}
#[test]
fn test_parse_patch_with_rc_suffix() {
let v = ServerVersion::parse("0.0.19038rc1").unwrap();
assert_eq!(v.patch(), 19038);
assert_eq!(v.suffix(), Some("rc1"));
}
#[test]
fn test_parse_hyphen_prerelease_with_dot_suffix() {
let v = ServerVersion::parse("1.2.3-beta.1").unwrap();
assert_eq!(v.patch(), 3);
// "-beta" from patch tail, ".1" from dot tail
assert_eq!(v.suffix(), Some("-beta.1"));
}
#[test]
fn test_parse_whitespace() {
let v = ServerVersion::parse(" 1.2.3 ").unwrap();
assert_eq!(v.major(), 1);
assert_eq!(v.patch(), 3);
}
#[test]
fn test_comparison() {
let v1 = ServerVersion::new(1, 0, 0);
let v2 = ServerVersion::new(1, 0, 1);
let v3 = ServerVersion::new(1, 1, 0);
let v4 = ServerVersion::new(2, 0, 0);
assert!(v1 < v2);
assert!(v2 < v3);
assert!(v3 < v4);
assert!(v1 == ServerVersion::new(1, 0, 0));
}
#[test]
fn test_comparison_ignores_suffix() {
let v1 = ServerVersion::parse("1.2.3").unwrap();
let v2 = ServerVersion::parse("1.2.3-beta1").unwrap();
// PartialEq compares all fields, but Ord ignores suffix
assert_eq!(v1.cmp(&v2), std::cmp::Ordering::Equal);
assert!(v1 >= v2);
assert!(v2 >= v1);
}
#[test]
fn test_display() {
let v = ServerVersion::parse("0.0.19038").unwrap();
assert_eq!(format!("{v}"), "0.0.19038");
}
#[test]
fn test_display_preserves_original() {
// v-prefix is preserved in raw/display
let v = ServerVersion::parse("v1.2.3").unwrap();
assert_eq!(format!("{v}"), "v1.2.3");
}
}