use std::fmt::Display;
use std::str::FromStr;
use crate::{error::OxenError, util::oxen_version::OxenVersion};
#[derive(Debug, Clone)]
pub enum MinOxenVersion {
V0_10_0,
V0_19_0,
V0_25_0,
LATEST,
}
impl MinOxenVersion {
pub fn or_latest(s: Option<String>) -> Result<MinOxenVersion, OxenError> {
if let Some(version) = s {
MinOxenVersion::from_string(version)
} else {
Ok(MinOxenVersion::LATEST)
}
}
pub fn or_earliest(s: Option<String>) -> Result<MinOxenVersion, OxenError> {
if let Some(version) = s {
MinOxenVersion::from_string(version)
} else {
Ok(MinOxenVersion::V0_10_0)
}
}
pub fn from_string(s: impl AsRef<str>) -> Result<MinOxenVersion, OxenError> {
match s.as_ref() {
"0.10.0" => Ok(MinOxenVersion::V0_10_0),
"0.19.0" => Ok(MinOxenVersion::V0_19_0),
"0.25.0" => Ok(MinOxenVersion::V0_25_0),
"0.36.0" => Ok(MinOxenVersion::LATEST),
_ => Err(OxenError::invalid_version(s.as_ref())),
}
}
pub fn as_str(&self) -> &'static str {
match self {
MinOxenVersion::V0_10_0 => "0.10.0",
MinOxenVersion::V0_19_0 => "0.19.0",
MinOxenVersion::V0_25_0 => "0.25.0",
MinOxenVersion::LATEST => "0.36.0",
}
}
pub fn to_oxen_version(&self) -> OxenVersion {
let v = self.as_str();
OxenVersion::from_str(v).unwrap_or_else(|_| panic!("Invalid version string: {v}"))
}
}
impl Display for MinOxenVersion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_str())
}
}
impl Eq for MinOxenVersion {}
impl PartialEq for MinOxenVersion {
fn eq(&self, other: &Self) -> bool {
self.to_oxen_version() == other.to_oxen_version()
}
}
impl PartialOrd for MinOxenVersion {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for MinOxenVersion {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.to_oxen_version().cmp(&other.to_oxen_version())
}
}