use std::fmt;
use std::str::FromStr;
use crate::err::Error;
#[derive(Clone)]
pub enum VmId {
Name(String),
Uuid(uuid::Uuid)
}
impl fmt::Display for VmId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &*self {
VmId::Name(n) => write!(f, "{}", n),
VmId::Uuid(u) => write!(
f,
"{{{}}}",
u.to_hyphenated()
.encode_lower(&mut uuid::Uuid::encode_buffer())
.to_string()
)
}
}
}
impl From<&str> for VmId {
fn from(s: &str) -> Self {
VmId::Name(s.to_string())
}
}
impl FromStr for VmId {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match uuid::Uuid::parse_str(s) {
Ok(u) => VmId::Uuid(u),
Err(_) => VmId::Name(s.to_string())
})
}
}