use hx_core::Version;
use serde::{Deserialize, Serialize};
pub static KNOWN_GHC_VERSIONS: &[&str] = &[
"9.14.1", "9.12.4", "9.12.3", "9.12.2", "9.12.1", "9.10.3", "9.10.2", "9.10.1", "9.8.4", "9.8.3", "9.8.2", "9.8.1", "9.6.7", "9.6.6", "9.6.5", "9.6.4", "9.6.3", "9.6.2", "9.6.1", "9.4.8", "9.4.7", "9.4.6", "9.4.5", "9.4.4", "9.2.8", "9.2.7", "9.0.2",
];
pub static RECOMMENDED_GHC_VERSION: &str = "9.10.3";
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct GhcVersion {
pub version: String,
#[serde(skip)]
pub parsed: Option<Version>,
pub is_recommended: bool,
pub is_installed: bool,
}
impl GhcVersion {
pub fn new(version: impl Into<String>) -> Self {
let version = version.into();
let parsed = version.parse().ok();
let is_recommended = version == RECOMMENDED_GHC_VERSION;
Self {
version,
parsed,
is_recommended,
is_installed: false,
}
}
pub fn with_installed(mut self, installed: bool) -> Self {
self.is_installed = installed;
self
}
pub fn major_minor(&self) -> String {
if let Some(ref parsed) = self.parsed {
format!("{}.{}", parsed.major, parsed.minor)
} else {
let parts: Vec<&str> = self.version.split('.').collect();
if parts.len() >= 2 {
format!("{}.{}", parts[0], parts[1])
} else {
self.version.clone()
}
}
}
pub fn matches(&self, requirement: &str) -> bool {
if requirement == self.version {
return true;
}
let req_parts: Vec<&str> = requirement.split('.').collect();
if req_parts.len() == 2 {
return self.major_minor() == requirement;
}
false
}
}
impl std::fmt::Display for GhcVersion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.version)
}
}
impl Ord for GhcVersion {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match (&self.parsed, &other.parsed) {
(Some(a), Some(b)) => b.cmp(a), _ => other.version.cmp(&self.version),
}
}
}
impl PartialOrd for GhcVersion {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
pub fn known_versions() -> Vec<GhcVersion> {
KNOWN_GHC_VERSIONS
.iter()
.map(|v| GhcVersion::new(*v))
.collect()
}
pub fn is_known_version(version: &str) -> bool {
KNOWN_GHC_VERSIONS.contains(&version)
}
pub fn is_valid_version(version: &str) -> bool {
let parts: Vec<&str> = version.split('.').collect();
if parts.len() < 2 || parts.len() > 3 {
return false;
}
parts.iter().all(|p| p.parse::<u32>().is_ok())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_known_versions() {
let versions = known_versions();
assert!(!versions.is_empty());
assert!(versions.iter().any(|v| v.version == "9.8.2"));
}
#[test]
fn test_is_known_version() {
assert!(is_known_version("9.8.2"));
assert!(is_known_version("9.6.4"));
assert!(!is_known_version("1.0.0"));
}
#[test]
fn test_ghc_version_new() {
let v = GhcVersion::new(RECOMMENDED_GHC_VERSION);
assert_eq!(v.version, RECOMMENDED_GHC_VERSION);
assert!(v.is_recommended);
assert!(!GhcVersion::new("9.8.2").is_recommended);
assert!(!v.is_installed);
}
#[test]
fn test_major_minor() {
let v = GhcVersion::new("9.8.2");
assert_eq!(v.major_minor(), "9.8");
let v = GhcVersion::new("9.10.1");
assert_eq!(v.major_minor(), "9.10");
}
#[test]
fn test_matches() {
let v = GhcVersion::new("9.8.2");
assert!(v.matches("9.8.2"));
assert!(v.matches("9.8"));
assert!(!v.matches("9.6"));
assert!(!v.matches("9.8.1"));
}
#[test]
fn test_is_valid_version() {
assert!(is_valid_version("9.8.2"));
assert!(is_valid_version("9.10.1"));
assert!(is_valid_version("9.8"));
assert!(!is_valid_version("9"));
assert!(!is_valid_version("9.8.2.1"));
assert!(!is_valid_version("abc"));
assert!(!is_valid_version("9.x.2"));
}
#[test]
fn test_version_ordering() {
let mut versions = [
GhcVersion::new("9.6.4"),
GhcVersion::new("9.8.2"),
GhcVersion::new("9.10.1"),
];
versions.sort();
assert_eq!(versions[0].version, "9.10.1");
assert_eq!(versions[1].version, "9.8.2");
assert_eq!(versions[2].version, "9.6.4");
}
}