-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make MinimumOSVersion newtype over four u16s
- Loading branch information
1 parent
888396c
commit 172ecbe
Showing
4 changed files
with
122 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,74 @@ | ||
use color_eyre::eyre::Error; | ||
use derive_more::{Display, FromStr}; | ||
use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; | ||
use versions::Version; | ||
use color_eyre::eyre::OptionExt; | ||
use derive_more::Display; | ||
use serde_with::{DeserializeFromStr, SerializeDisplay}; | ||
use std::str::FromStr; | ||
|
||
#[derive(Clone, Debug, Default, Display, Eq, FromStr, Hash, Ord, PartialEq, PartialOrd)] | ||
pub struct MinimumOSVersion(Version); | ||
#[derive( | ||
SerializeDisplay, | ||
DeserializeFromStr, | ||
Copy, | ||
Clone, | ||
Debug, | ||
Default, | ||
Display, | ||
Eq, | ||
PartialEq, | ||
Hash, | ||
Ord, | ||
PartialOrd, | ||
)] | ||
#[display("{_0}.{_1}.{_2}.{_3}")] | ||
pub struct MinimumOSVersion(u16, u16, u16, u16); | ||
|
||
impl MinimumOSVersion { | ||
pub fn new(input: &str) -> color_eyre::Result<Self> { | ||
Ok(Self(Version::from_str(input).map_err(Error::msg)?)) | ||
} | ||
impl FromStr for MinimumOSVersion { | ||
type Err = color_eyre::Report; | ||
|
||
pub fn removable() -> Self { | ||
Self::new("10.0.0.0").unwrap() | ||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
let mut parts = s.splitn(Self::MAX_PARTS as usize, Self::SEPARATOR); | ||
let major = parts | ||
.next() | ||
.ok_or_eyre("No major version")? | ||
.parse::<u16>()?; | ||
let minor = parts.next().map_or(Ok(0), u16::from_str)?; | ||
let patch = parts.next().map_or(Ok(0), u16::from_str)?; | ||
let build = parts.next().map_or(Ok(0), u16::from_str)?; | ||
Ok(Self(major, minor, patch, build)) | ||
} | ||
} | ||
|
||
impl Serialize for MinimumOSVersion { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
serializer.serialize_str(&self.0.to_string()) | ||
impl MinimumOSVersion { | ||
const MAX_PARTS: u8 = 4; | ||
const SEPARATOR: char = '.'; | ||
|
||
pub const fn removable() -> Self { | ||
Self(10, 0, 0, 0) | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for MinimumOSVersion { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
String::deserialize(deserializer)? | ||
.parse() | ||
.map_err(de::Error::custom) | ||
#[cfg(test)] | ||
mod tests { | ||
use crate::types::minimum_os_version::MinimumOSVersion; | ||
use rstest::rstest; | ||
use std::str::FromStr; | ||
|
||
#[rstest] | ||
#[case("10.0.17763.0", MinimumOSVersion(10, 0, 17763, 0))] | ||
#[case("11", MinimumOSVersion(11, 0, 0, 0))] | ||
#[case("10.1", MinimumOSVersion(10, 1, 0, 0))] | ||
#[case("0", MinimumOSVersion(0, 0, 0, 0))] | ||
#[case( | ||
"65535.65535.65535.65535", | ||
MinimumOSVersion(u16::MAX, u16::MAX, u16::MAX, u16::MAX) | ||
)] | ||
fn from_str(#[case] minimum_os_version: &str, #[case] expected: MinimumOSVersion) { | ||
assert_eq!( | ||
MinimumOSVersion::from_str(minimum_os_version).unwrap(), | ||
expected | ||
) | ||
} | ||
|
||
#[test] | ||
fn display() { | ||
assert_eq!(MinimumOSVersion(1, 2, 3, 4).to_string(), "1.2.3.4") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters