-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathdate_version.rs
More file actions
118 lines (98 loc) · 3.06 KB
/
date_version.rs
File metadata and controls
118 lines (98 loc) · 3.06 KB
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
use std::fmt;
use std::str::FromStr;
use serde::de::Deserialize;
use serde::ser::Serialize;
use thiserror::Error;
#[derive(Debug, Error)]
#[error("invalid date (`YYYY.MM.DD.R`) version")]
pub struct DateVersionError;
/// Parsed application version represented in the format `YYYY.MM.DD.R`
#[derive(Debug, Default, Eq, PartialEq, PartialOrd, Ord, Clone, Copy)]
pub struct DateVersion {
// NOTE: Field order is important for `PartialOrd` and `Ord` derives
pub year: u32,
pub month: u32,
pub day: u32,
pub revision: u32,
}
impl DateVersion {
pub fn fmt_without_revision(&self) -> String {
format!("{}.{}.{}", self.year, self.month, self.day)
}
}
impl Serialize for DateVersion {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::ser::Serializer,
{
self.to_string().serialize(serializer)
}
}
impl<'de> Deserialize<'de> for DateVersion {
fn deserialize<D>(deserializer: D) -> Result<DateVersion, D::Error>
where
D: serde::de::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
DateVersion::from_str(&s).map_err(serde::de::Error::custom)
}
}
impl FromStr for DateVersion {
type Err = DateVersionError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut parts = s.splitn(4, '.');
let mut next_part = || parts.next().and_then(|s| u32::from_str(s).ok()).ok_or(DateVersionError);
let year = next_part()?;
let month = next_part()?;
let day = next_part()?;
// Allow version without revision
let revision = next_part().unwrap_or(0);
if parts.next().is_some() {
return Err(DateVersionError);
}
Ok(DateVersion {
year,
month,
day,
revision,
})
}
}
impl fmt::Display for DateVersion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}.{}.{}.{}", self.year, self.month, self.day, self.revision)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn date_version_roundtrip() {
let version = DateVersion {
year: 2022,
month: 10,
day: 1,
revision: 2,
};
let version_str = version.to_string();
assert_eq!(version_str, "2022.10.1.2");
let parsed_version = DateVersion::from_str(&version_str).unwrap();
assert_eq!(version, parsed_version);
}
// Regression test in case field order gets changed
#[test]
fn date_version_ordering() {
const VERSIONS_ASCENDING_PAIRS: &[(&str, &str)] = &[
// cases (>) for fields in order
("2022.10.1.2", "2022.10.1.1"),
("2022.10.2.1", "2022.10.1.1"),
("2022.11.1.1", "2022.10.1.1"),
("2023.10.1.1", "2022.10.1.1"),
];
for (v1, v2) in VERSIONS_ASCENDING_PAIRS {
let greater = DateVersion::from_str(v1).unwrap();
let lesser = DateVersion::from_str(v2).unwrap();
assert!(greater > lesser);
}
}
}