use crate::ParseCondaLockError;
use serde::de::Error;
use serde_repr::{Deserialize_repr, Serialize_repr};
use std::fmt::{Display, Formatter};
#[derive(
Copy, Clone, PartialEq, Eq, Hash, Debug, Ord, PartialOrd, Serialize_repr, Deserialize_repr,
)]
#[repr(u16)]
pub enum FileFormatVersion {
V1 = 1,
V2 = 2,
V3 = 3,
V4 = 4,
V5 = 5,
V6 = 6,
}
impl Display for FileFormatVersion {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", *self as u16)
}
}
impl FileFormatVersion {
pub const LATEST: Self = FileFormatVersion::V6;
pub fn should_pypi_indexes_be_present(self) -> bool {
self >= FileFormatVersion::V5
}
}
impl Default for FileFormatVersion {
fn default() -> Self {
Self::LATEST
}
}
impl TryFrom<u64> for FileFormatVersion {
type Error = ParseCondaLockError;
fn try_from(value: u64) -> Result<Self, Self::Error> {
Ok(match value {
0 => {
return Err(ParseCondaLockError::ParseError(serde_yaml::Error::custom(
"`version` field in lock file is not an integer",
)))
}
1 => Self::V1,
2 => Self::V2,
3 => Self::V3,
4 => Self::V4,
5 => Self::V5,
6 => Self::V6,
_ => {
return Err(ParseCondaLockError::IncompatibleVersion {
lock_file_version: value,
max_supported_version: FileFormatVersion::LATEST,
})
}
})
}
}