Skip to content

Commit

Permalink
Refactoring names
Browse files Browse the repository at this point in the history
  • Loading branch information
kl committed Nov 27, 2024
1 parent 1c3cce1 commit ffeb7e4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
28 changes: 14 additions & 14 deletions mullvad-version/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt::Display;
use std::str::FromStr;
use std::sync::LazyLock;

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

/// The Mullvad VPN app product version
Expand All @@ -12,15 +12,15 @@ pub const VERSION: &str = include_str!(concat!(env!("OUT_DIR"), "/product-versio
pub struct Version {
pub year: String,
pub incremental: String,
/// A version can have an optional type, e.g. alpha or beta. If `version_type` and `dev`
/// both are None the version is stable.
pub version_type: Option<VersionType>,
/// A version can have an optional pre-stable type, e.g. alpha or beta. If `pre_stable`
/// and `dev` both are None the version is stable.
pub pre_stable: Option<PreStableType>,
/// All versions may have an optional -dev-[commit hash] suffix.
pub dev: Option<String>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum VersionType {
pub enum PreStableType {
Alpha(String),
Beta(String),
}
Expand All @@ -31,19 +31,19 @@ impl Version {
}

pub fn is_stable(&self) -> bool {
self.version_type.is_none() && self.dev.is_none()
self.pre_stable.is_none() && self.dev.is_none()
}

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

pub fn beta(&self) -> Option<&str> {
match &self.version_type {
Some(VersionType::Beta(beta)) => Some(beta),
match &self.pre_stable {
Some(PreStableType::Beta(beta)) => Some(beta),
_ => None,
}
}
Expand All @@ -55,15 +55,15 @@ impl Display for Version {
let Version {
year,
incremental,
version_type,
pre_stable: version_type,
dev,
} = &self;

write!(f, "{year}.{incremental}")?;

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

Expand Down Expand Up @@ -126,7 +126,7 @@ impl FromStr for Version {
Ok(Version {
year,
incremental,
version_type,
pre_stable: version_type,
dev,
})
}
Expand Down
37 changes: 18 additions & 19 deletions mullvad-version/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use mullvad_version::{Version, VersionType};
use mullvad_version::{PreStableType, Version};
use std::{env, process::exit};

const ANDROID_VERSION: &str =
Expand Down Expand Up @@ -60,9 +60,9 @@ fn to_android_version_code(version: &str) -> String {
let (build_type, build_number) = if version.dev.is_some() {
("9", "000")
} else {
match &version.version_type {
Some(VersionType::Alpha(v)) => ("0", v.as_str()),
Some(VersionType::Beta(v)) => ("1", v.as_str()),
match &version.pre_stable {
Some(PreStableType::Alpha(v)) => ("0", v.as_str()),
Some(PreStableType::Beta(v)) => ("1", v.as_str()),
// Stable version
None => ("9", "000"),
}
Expand All @@ -74,31 +74,30 @@ fn to_android_version_code(version: &str) -> String {
)
}

/// On Windows we do not support alpha versions for now, so this function will panic
/// if the parsed version is an alpha version.
fn to_windows_h_format(version: &str) -> String {
let Version {
year,
incremental,
version_type,
..
} = Version::parse(version);

fn to_windows_h_format(version_str: &str) -> String {
let version = Version::parse(version_str);
assert!(
is_valid_windows_version(&version_type),
"Invalid Windows version type: {version_type:?}"
is_valid_windows_version(&version),
"Invalid Windows version: {version:?}"
);

let Version {
year, incremental, ..
} = version;

format!(
"#define MAJOR_VERSION 20{year}
#define MINOR_VERSION {incremental}
#define PATCH_VERSION 0
#define PRODUCT_VERSION \"{version}\""
#define PRODUCT_VERSION \"{version_str}\""
)
}

fn is_valid_windows_version(version_type: &Option<VersionType>) -> bool {
matches!(version_type, None | Some(VersionType::Beta(_)))
/// On Windows we currently support the following versions: stable, beta and dev.
fn is_valid_windows_version(version: &Version) -> bool {
version.is_stable()
|| version.beta().is_some()
|| (version.dev.is_some() && version.alpha().is_none())
}

#[cfg(test)]
Expand Down

0 comments on commit ffeb7e4

Please sign in to comment.