#![allow(clippy::from_over_into)]
use crate::spec_error::SpecError;
use crate::syntax::Version;
use crate::syntax_parser::parse_alias;
use crate::unresolved_spec::UnresolvedVersionSpec;
use compact_str::CompactString;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::str::FromStr;
#[derive(Clone, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(untagged, into = "String", try_from = "String")]
pub enum VersionSpec {
Canary,
Alias(CompactString),
Version(Version),
}
impl VersionSpec {
pub fn parse<T: AsRef<str>>(value: T) -> Result<Self, SpecError> {
Self::from_str(value.as_ref())
}
pub fn as_version(&self) -> Option<&Version> {
match self {
Self::Version(inner) => Some(inner),
_ => None,
}
}
pub fn get_scope(&self) -> Option<&str> {
match self {
Self::Version(version) => version.scope.as_deref(),
_ => None,
}
}
pub fn is_alias<A: AsRef<str>>(&self, name: A) -> bool {
match self {
Self::Alias(alias) => alias == name.as_ref(),
_ => false,
}
}
pub fn is_canary(&self) -> bool {
match self {
Self::Canary => true,
Self::Alias(alias) => alias == "canary",
_ => false,
}
}
pub fn is_latest(&self) -> bool {
match self {
Self::Alias(alias) => alias == "latest",
_ => false,
}
}
pub fn set_scope(&mut self, scope: impl AsRef<str>) {
if let Self::Version(version) = self {
version.scope = Some(scope.as_ref().into());
}
}
pub fn to_unresolved_spec(&self) -> UnresolvedVersionSpec {
match self {
Self::Canary => UnresolvedVersionSpec::Canary,
Self::Alias(alias) => UnresolvedVersionSpec::Alias(alias.to_owned()),
Self::Version(version) => UnresolvedVersionSpec::Version(version.to_owned()),
}
}
}
#[cfg(feature = "schematic")]
impl schematic::Schematic for VersionSpec {
fn schema_name() -> Option<String> {
Some("VersionSpec".into())
}
fn build_schema(mut schema: schematic::SchemaBuilder) -> schematic::Schema {
schema.set_description("Represents a resolved version or alias.");
schema.string_default()
}
}
impl Default for VersionSpec {
fn default() -> Self {
Self::Alias("latest".into())
}
}
impl FromStr for VersionSpec {
type Err = SpecError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
if value == "canary" {
return Ok(Self::Canary);
}
match Version::parse(value) {
Ok(version) => Ok(Self::Version(version)),
Err(error) => match parse_alias(value) {
Ok(alias) => Ok(Self::Alias(alias)),
Err(_) => Err(error),
},
}
}
}
impl TryFrom<String> for VersionSpec {
type Error = SpecError;
fn try_from(value: String) -> Result<Self, Self::Error> {
Self::from_str(&value)
}
}
impl Into<String> for VersionSpec {
fn into(self) -> String {
self.to_string()
}
}
impl fmt::Debug for VersionSpec {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{self}")
}
}
impl fmt::Display for VersionSpec {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Canary => write!(f, "canary"),
Self::Alias(alias) => write!(f, "{alias}"),
Self::Version(version) => write!(f, "{version}"),
}
}
}
impl PartialEq<&str> for VersionSpec {
fn eq(&self, other: &&str) -> bool {
match self {
Self::Canary => "canary" == *other,
Self::Alias(alias) => alias == other,
_ => &self.to_string() == other,
}
}
}
impl PartialEq<Version> for VersionSpec {
fn eq(&self, other: &Version) -> bool {
match self {
Self::Version(version) => version == other,
_ => false,
}
}
}
impl AsRef<VersionSpec> for VersionSpec {
fn as_ref(&self) -> &VersionSpec {
self
}
}