use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum HostOs {
Linux,
Macos,
Windows,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum Arch {
Amd64,
Arm64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, schemars::JsonSchema)]
#[cfg_attr(feature = "ts-rs", derive(ts_rs::TS), ts(rename = "VagrantProvider"))]
#[serde(rename_all = "snake_case")]
pub enum Provider {
Virtualbox,
VmwareDesktop,
Hyperv,
Parallels,
Libvirt,
Qemu,
Utm,
VmwareEsxi,
}
impl Provider {
pub const ALL: [Provider; 8] = [
Provider::Virtualbox,
Provider::VmwareDesktop,
Provider::Hyperv,
Provider::Parallels,
Provider::Libvirt,
Provider::Qemu,
Provider::Utm,
Provider::VmwareEsxi,
];
#[must_use]
pub const fn id(self) -> &'static str {
match self {
Provider::Virtualbox => "virtualbox",
Provider::VmwareDesktop => "vmware_desktop",
Provider::Hyperv => "hyperv",
Provider::Parallels => "parallels",
Provider::Libvirt => "libvirt",
Provider::Qemu => "qemu",
Provider::Utm => "utm",
Provider::VmwareEsxi => "vmware_esxi",
}
}
#[must_use]
pub const fn plugin(self) -> Option<&'static str> {
match self {
Provider::Virtualbox | Provider::Hyperv => None,
Provider::VmwareDesktop => Some("vagrant-vmware-desktop"),
Provider::Parallels => Some("vagrant-parallels"),
Provider::Libvirt => Some("vagrant-libvirt"),
Provider::Qemu => Some("vagrant-qemu"),
Provider::Utm => Some("vagrant_utm"),
Provider::VmwareEsxi => Some("vagrant-vmware-esxi"),
}
}
#[must_use]
pub const fn is_remote(self) -> bool {
matches!(self, Provider::VmwareEsxi)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum Cloud {
Aws,
Azure,
Gcp,
Digitalocean,
Linode,
Oci,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum Protocol {
#[default]
Tcp,
Udp,
}
impl Protocol {
#[must_use]
pub fn parse(word: &str) -> Option<Protocol> {
if word.eq_ignore_ascii_case("tcp") {
Some(Protocol::Tcp)
} else if word.eq_ignore_ascii_case("udp") {
Some(Protocol::Udp)
} else {
None
}
}
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Protocol::Tcp => "tcp",
Protocol::Udp => "udp",
}
}
}
impl std::fmt::Display for Protocol {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Version {
major: u64,
minor: u64,
patch: u64,
pre: Option<String>,
}
impl Version {
#[must_use]
pub const fn new(major: u64, minor: u64, patch: u64) -> Version {
Version {
major,
minor,
patch,
pre: None,
}
}
#[must_use]
pub fn parse(text: &str) -> Option<Version> {
let (core, pre) = match text.split_once('-') {
Some((core, pre)) => (core, Some(pre)),
None => (text, None),
};
let mut numbers = core.split('.').map(|n| {
if n.is_empty() || !n.bytes().all(|b| b.is_ascii_digit()) {
return None;
}
n.parse::<u64>().ok()
});
let (major, minor, patch) = (numbers.next()??, numbers.next()??, numbers.next()??);
if numbers.next().is_some() {
return None;
}
let pre = match pre {
None => None,
Some(p) if !p.is_empty() && p.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '-')) => Some(p.to_owned()),
Some(_) => return None,
};
Some(Version { major, minor, patch, pre })
}
}
impl std::fmt::Display for Version {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}.{}.{}", self.major, self.minor, self.patch)?;
if let Some(pre) = &self.pre {
write!(f, "-{pre}")?;
}
Ok(())
}
}
impl Ord for Version {
fn cmp(&self, other: &Version) -> std::cmp::Ordering {
use std::cmp::Ordering;
(self.major, self.minor, self.patch)
.cmp(&(other.major, other.minor, other.patch))
.then_with(|| match (&self.pre, &other.pre) {
(None, None) => Ordering::Equal,
(None, Some(_)) => Ordering::Greater,
(Some(_), None) => Ordering::Less,
(Some(a), Some(b)) => pre_release_cmp(a, b),
})
}
}
impl PartialOrd for Version {
fn partial_cmp(&self, other: &Version) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
#[expect(clippy::many_single_char_names, reason = "the two sides of a comparison, field by field")]
fn pre_release_cmp(a: &str, b: &str) -> std::cmp::Ordering {
use std::cmp::Ordering;
let mut xs = a.split('.');
let mut ys = b.split('.');
loop {
let order = match (xs.next(), ys.next()) {
(None, None) => return Ordering::Equal,
(None, Some(_)) => return Ordering::Less,
(Some(_), None) => return Ordering::Greater,
(Some(x), Some(y)) => match (x.parse::<u64>(), y.parse::<u64>()) {
(Ok(m), Ok(n)) => m.cmp(&n),
(Ok(_), Err(_)) => Ordering::Less,
(Err(_), Ok(_)) => Ordering::Greater,
(Err(_), Err(_)) => x.cmp(y),
},
};
if order != Ordering::Equal {
return order;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_version_sorts_by_its_numbers_then_a_pre_release_first() {
let v = |s: &str| Version::parse(s).unwrap();
assert!(v("2.9.0") < v("2.14.0"));
assert!(v("10.0.0") > v("9.0.0"));
assert!(v("1.0.0-rc.1") < v("1.0.0"));
assert!(v("1.0.0-alpha") < v("1.0.0-alpha.1"));
assert!(v("1.0.0-alpha.9") < v("1.0.0-alpha.11"));
assert!(v("1.0.0-1") < v("1.0.0-alpha"));
assert_eq!(v("2.17.0-desktop.1").to_string(), "2.17.0-desktop.1");
for bad in ["1.0", "1.0.0-", "v1.0.0", "1..0", "1.0.0.0"] {
assert_eq!(Version::parse(bad), None, "{bad}");
}
}
}