Skip to content

Commit

Permalink
Remove Stable enum variant
Browse files Browse the repository at this point in the history
  • Loading branch information
kl committed Nov 27, 2024
1 parent 68131b2 commit daef200
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
25 changes: 12 additions & 13 deletions mullvad-version/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::fmt::Display;
use std::str::FromStr;
use std::sync::LazyLock;
use VersionType::*;

use crate::VersionType::{Alpha, Beta};
use regex::Regex;

/// The Mullvad VPN app product version
Expand All @@ -12,16 +12,15 @@ pub const VERSION: &str = include_str!(concat!(env!("OUT_DIR"), "/product-versio
pub struct Version {
pub year: String,
pub incremental: String,
pub version_type: Option<VersionType>,
// All versions may have an optional -dev-[commit hash] suffix.
pub dev: Option<String>,
pub version_type: VersionType,
}

#[derive(Debug, Clone, PartialEq)]
pub enum VersionType {
Alpha(String),
Beta(String),
Stable,
}

impl Version {
Expand All @@ -30,19 +29,19 @@ impl Version {
}

pub fn is_stable(&self) -> bool {
matches!(&self.version_type, Stable)
self.version_type.is_none() && self.dev.is_none()
}

pub fn alpha(&self) -> Option<&str> {
match &self.version_type {
Alpha(v) => Some(v),
Some(VersionType::Alpha(v)) => Some(v),
_ => None,
}
}

pub fn beta(&self) -> Option<&str> {
match &self.version_type {
Beta(beta) => Some(beta),
Some(VersionType::Beta(beta)) => Some(beta),
_ => None,
}
}
Expand All @@ -61,9 +60,9 @@ impl Display for Version {
write!(f, "{year}.{incremental}")?;

match version_type {
Alpha(version) => write!(f, "-alpha{version}")?,
Beta(version) => write!(f, "-beta{version}")?,
Stable => (),
Some(VersionType::Alpha(version)) => write!(f, "-alpha{version}")?,
Some(VersionType::Beta(version)) => write!(f, "-beta{version}")?,
None => (),
};

if let Some(dev) = dev {
Expand Down Expand Up @@ -116,9 +115,9 @@ impl FromStr for Version {
let dev = captures.name("dev").map(|m| m.as_str().to_owned());

let version_type = match (alpha, beta) {
(None, None) => Stable,
(Some(v), None) => Alpha(v),
(None, Some(v)) => Beta(v),
(None, None) => None,
(Some(v), None) => Some(Alpha(v)),
(None, Some(v)) => Some(Beta(v)),
_ => return Err(format!("Invalid version: {version}")),
};

Expand Down Expand Up @@ -185,7 +184,7 @@ mod tests {
let parsed = Version::parse(version);
assert_eq!(parsed.year, "21");
assert_eq!(parsed.incremental, "34");
assert!(parsed.is_stable());
assert!(!parsed.is_stable());
assert_eq!(parsed.dev, Some("0b60e4d87".to_string()));
assert_eq!(parsed.alpha(), None);
assert_eq!(parsed.beta(), None);
Expand Down
12 changes: 6 additions & 6 deletions mullvad-version/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use mullvad_version::{Version, VersionType};
use std::{env, process::exit};
use VersionType::*;

const ANDROID_VERSION: &str =
include_str!(concat!(env!("OUT_DIR"), "/android-product-version.txt"));
Expand Down Expand Up @@ -62,9 +61,10 @@ fn to_android_version_code(version: &str) -> String {
("9", "000")
} else {
match &version.version_type {
Alpha(v) => ("0", v.as_str()),
Beta(v) => ("1", v.as_str()),
Stable => ("9", "000"),
Some(VersionType::Alpha(v)) => ("0", v.as_str()),
Some(VersionType::Beta(v)) => ("1", v.as_str()),
// Stable version
None => ("9", "000"),
}
};

Expand Down Expand Up @@ -97,8 +97,8 @@ fn to_windows_h_format(version: &str) -> String {
)
}

fn is_valid_windows_version(version_type: &VersionType) -> bool {
matches!(version_type, Beta(_) | Stable)
fn is_valid_windows_version(version_type: &Option<VersionType>) -> bool {
matches!(version_type, None | Some(VersionType::Beta(_)))
}

#[cfg(test)]
Expand Down

0 comments on commit daef200

Please sign in to comment.