Skip to content

Commit

Permalink
Rename some variants
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Nov 5, 2024
1 parent e3eefcc commit e187a25
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 56 deletions.
78 changes: 41 additions & 37 deletions crates/uv-pep440/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ impl Version {
if value.is_empty() {
self.without_local()
} else {
self.make_full().local = LocalVersion::Actual(value);
self.make_full().local = LocalVersion::Segments(value);
self
}
}
Expand All @@ -544,7 +544,7 @@ impl Version {
#[must_use]
pub fn with_local(mut self, value: LocalVersion) -> Self {
match value {
LocalVersion::Actual(segments) => self.with_local_segments(segments),
LocalVersion::Segments(segments) => self.with_local_segments(segments),
LocalVersion::Max => {
self.make_full().local = value;
self
Expand Down Expand Up @@ -628,7 +628,7 @@ impl Version {
pre: small.pre(),
post: small.post(),
dev: small.dev(),
local: LocalVersion::Actual(vec![]),
local: LocalVersion::Segments(vec![]),
};
*self = Self {
inner: Arc::new(VersionInner::Full { full }),
Expand Down Expand Up @@ -726,10 +726,10 @@ impl std::fmt::Display for Version {
String::new()
} else {
match self.local() {
LocalVersionSlice::Actual(_) => {
format!("+{}", self.local().local_identifier_string())
LocalVersionSlice::Segments(_) => {
format!("+{}", self.local())
}
LocalVersionSlice::Sentinel => String::new(),
LocalVersionSlice::Max => String::new(),
}
};
write!(f, "{epoch}{release}{pre}{post}{dev}{local}")
Expand Down Expand Up @@ -849,7 +849,7 @@ impl FromStr for Version {
/// `min, .devN, aN, bN, rcN, <no suffix>, .postN, max`.
/// Its representation is thus:
/// * The most significant 3 bits of Byte 2 corresponds to a value in
/// the range 0-6 inclusive, corresponding to min, dev, pre-a, pre-b, pre-rc,
/// the range 0-7 inclusive, corresponding to min, dev, pre-a, pre-b, pre-rc,
/// no-suffix or post releases, respectively. `min` is a special version that
/// does not exist in PEP 440, but is used here to represent the smallest
/// possible version, preceding any `dev`, `pre`, `post` or releases. `max` is
Expand Down Expand Up @@ -1178,7 +1178,7 @@ impl VersionSmall {
fn local(&self) -> LocalVersionSlice {
// A "small" version is never used if the version has a non-zero number
// of local segments.
LocalVersionSlice::Actual(&[])
LocalVersionSlice::Segments(&[])
}

#[inline]
Expand All @@ -1200,13 +1200,13 @@ impl VersionSmall {

#[inline]
fn suffix_version(&self) -> u64 {
self.repr & 0x001F_FFFF
self.repr & Self::SUFFIX_MAX_VERSION
}

#[inline]
fn set_suffix_version(&mut self, value: u64) {
debug_assert!(value <= 0x001F_FFFF);
self.repr &= !0x001F_FFFF;
debug_assert!(value <= Self::SUFFIX_MAX_VERSION);
self.repr &= !Self::SUFFIX_MAX_VERSION;
self.repr |= value;
}
}
Expand Down Expand Up @@ -1404,51 +1404,55 @@ impl std::fmt::Display for Prerelease {
#[cfg_attr(feature = "rkyv", rkyv(derive(Debug, Eq, PartialEq, PartialOrd, Ord)))]
pub enum LocalVersion {
/// A sequence of local segments.
Actual(Vec<LocalSegment>),
Segments(Vec<LocalSegment>),
/// An internal-only value that compares greater to all other local versions.
Max,
}

/// Like [`LocalVersion`], but using a slice
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub enum LocalVersionSlice<'a> {
/// Like [`LocalVersion::Actual`]
Actual(&'a [LocalSegment]),
/// Like [`LocalVersion::Segments`]
Segments(&'a [LocalSegment]),
/// Like [`LocalVersion::Sentinel`]
Sentinel,
Max,
}

impl LocalVersion {
/// Convert the local version segments into a slice.
pub fn as_slice(&self) -> LocalVersionSlice<'_> {
match self {
LocalVersion::Actual(segments) => LocalVersionSlice::Actual(segments),
LocalVersion::Max => LocalVersionSlice::Sentinel,
LocalVersion::Segments(segments) => LocalVersionSlice::Segments(segments),
LocalVersion::Max => LocalVersionSlice::Max,
}
}

/// Clear the local version segments, if they exist.
pub fn clear(&mut self) {
match self {
Self::Actual(segments) => segments.clear(),
Self::Max => *self = Self::Actual(Vec::new()),
Self::Segments(segments) => segments.clear(),
Self::Max => *self = Self::Segments(Vec::new()),
}
}
}

impl LocalVersionSlice<'_> {
/// Output the local version identifier string.
///
/// [`LocalVersionSlice::Sentinel`] maps to `"[max]"` which is otherwise an illegal local
/// version because `[` and `]` are not allowed.
pub fn local_identifier_string(&self) -> String {
/// Output the local version identifier string.
///
/// [`LocalVersionSlice::Max`] maps to `"[max]"` which is otherwise an illegal local
/// version because `[` and `]` are not allowed.
impl std::fmt::Display for LocalVersionSlice<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LocalVersionSlice::Actual(segments) => segments
.iter()
.map(ToString::to_string)
.collect::<Vec<String>>()
.join("."),
LocalVersionSlice::Sentinel => String::from("[max]"),
LocalVersionSlice::Segments(segments) => {
for (i, segment) in segments.iter().enumerate() {
if i > 0 {
write!(f, ".")?;
}
write!(f, "{segment}")?;
}
Ok(())
}
LocalVersionSlice::Max => write!(f, "[max]"),
}
}
}
Expand All @@ -1462,18 +1466,18 @@ impl PartialOrd for LocalVersionSlice<'_> {
impl Ord for LocalVersionSlice<'_> {
fn cmp(&self, other: &Self) -> Ordering {
match (self, other) {
(LocalVersionSlice::Actual(lv1), LocalVersionSlice::Actual(lv2)) => lv1.cmp(lv2),
(LocalVersionSlice::Actual(_), LocalVersionSlice::Sentinel) => Ordering::Less,
(LocalVersionSlice::Sentinel, LocalVersionSlice::Actual(_)) => Ordering::Greater,
(LocalVersionSlice::Sentinel, LocalVersionSlice::Sentinel) => Ordering::Equal,
(LocalVersionSlice::Segments(lv1), LocalVersionSlice::Segments(lv2)) => lv1.cmp(lv2),
(LocalVersionSlice::Segments(_), LocalVersionSlice::Max) => Ordering::Less,
(LocalVersionSlice::Max, LocalVersionSlice::Segments(_)) => Ordering::Greater,
(LocalVersionSlice::Max, LocalVersionSlice::Max) => Ordering::Equal,
}
}
}

impl LocalVersionSlice<'_> {
/// Whether the local version is absent
pub fn is_empty(&self) -> bool {
matches!(self, Self::Actual(&[]))
matches!(self, Self::Segments(&[]))
}
}

Expand Down Expand Up @@ -1924,7 +1928,7 @@ impl<'a> Parser<'a> {
.with_pre(self.pre)
.with_post(self.post)
.with_dev(self.dev)
.with_local(LocalVersion::Actual(self.local));
.with_local(LocalVersion::Segments(self.local));
VersionPattern {
version,
wildcard: self.wildcard,
Expand Down
6 changes: 3 additions & 3 deletions crates/uv-pep440/src/version_ranges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ impl From<VersionSpecifier> for Ranges<Version> {
let VersionSpecifier { operator, version } = specifier;
match operator {
Operator::Equal => match version.local() {
LocalVersionSlice::Actual(&[]) => {
LocalVersionSlice::Segments(&[]) => {
let low = version;
let high = low.clone().with_local(LocalVersion::Max);
Ranges::between(low, high)
}
LocalVersionSlice::Actual(_) => Ranges::singleton(version),
LocalVersionSlice::Sentinel => unreachable!(
LocalVersionSlice::Segments(_) => Ranges::singleton(version),
LocalVersionSlice::Max => unreachable!(
"found `LocalVersionSlice::Sentinel`, which should be an internal-only value"
),
},
Expand Down
2 changes: 1 addition & 1 deletion crates/uv-pep440/src/version_specifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ impl std::fmt::Display for VersionSpecifierBuildError {
operator: ref op,
ref version,
} => {
let local = version.local().local_identifier_string();
let local = version.local();
write!(
f,
"Operator {op} is incompatible with versions \
Expand Down
2 changes: 1 addition & 1 deletion crates/uv-python/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2096,7 +2096,7 @@ impl FromStr for VersionRequest {
return Err(Error::InvalidVersionRequest(s.to_string()));
}

let uv_pep440::LocalVersionSlice::Actual([uv_pep440::LocalSegment::String(local)]) =
let uv_pep440::LocalVersionSlice::Segments([uv_pep440::LocalSegment::String(local)]) =
version.local()
else {
return Err(Error::InvalidVersionRequest(s.to_string()));
Expand Down
28 changes: 14 additions & 14 deletions crates/uv-resolver/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,65 +237,65 @@ impl NoSolutionError {
(Bound::Unbounded, Bound::Unbounded) => {}
(Bound::Unbounded, Bound::Included(v)) => {
// `<=1.0.0+[max]` is equivalent to `<=1.0.0`
if v.local() == LocalVersionSlice::Sentinel {
if v.local() == LocalVersionSlice::Max {
*upper = Bound::Included(v.clone().without_local());
}
}
(Bound::Unbounded, Bound::Excluded(v)) => {
// `<1.0.0+[max]` is equivalent to `<1.0.0`
if v.local() == LocalVersionSlice::Sentinel {
if v.local() == LocalVersionSlice::Max {
*upper = Bound::Excluded(v.clone().without_local());
}
}
(Bound::Included(v), Bound::Unbounded) => {
// `>=1.0.0+[max]` is equivalent to `>1.0.0`
if v.local() == LocalVersionSlice::Sentinel {
if v.local() == LocalVersionSlice::Max {
*lower = Bound::Excluded(v.clone().without_local());
}
}
(Bound::Included(v), Bound::Included(b)) => {
// `>=1.0.0+[max]` is equivalent to `>1.0.0`
if v.local() == LocalVersionSlice::Sentinel {
if v.local() == LocalVersionSlice::Max {
*lower = Bound::Excluded(v.clone().without_local());
}
// `<=1.0.0+[max]` is equivalent to `<=1.0.0`
if b.local() == LocalVersionSlice::Sentinel {
if b.local() == LocalVersionSlice::Max {
*upper = Bound::Included(b.clone().without_local());
}
}
(Bound::Included(v), Bound::Excluded(b)) => {
// `>=1.0.0+[max]` is equivalent to `>1.0.0`
if v.local() == LocalVersionSlice::Sentinel {
if v.local() == LocalVersionSlice::Max {
*lower = Bound::Excluded(v.clone().without_local());
}
// `<1.0.0+[max]` is equivalent to `<1.0.0`
if b.local() == LocalVersionSlice::Sentinel {
if b.local() == LocalVersionSlice::Max {
*upper = Bound::Included(b.clone().without_local());
}
}
(Bound::Excluded(v), Bound::Unbounded) => {
// `>1.0.0+[max]` is equivalent to `>1.0.0`
if v.local() == LocalVersionSlice::Sentinel {
if v.local() == LocalVersionSlice::Max {
*lower = Bound::Excluded(v.clone().without_local());
}
}
(Bound::Excluded(v), Bound::Included(b)) => {
// `>1.0.0+[max]` is equivalent to `>1.0.0`
if v.local() == LocalVersionSlice::Sentinel {
if v.local() == LocalVersionSlice::Max {
*lower = Bound::Excluded(v.clone().without_local());
}
// `<=1.0.0+[max]` is equivalent to `<=1.0.0`
if b.local() == LocalVersionSlice::Sentinel {
if b.local() == LocalVersionSlice::Max {
*upper = Bound::Included(b.clone().without_local());
}
}
(Bound::Excluded(v), Bound::Excluded(b)) => {
// `>1.0.0+[max]` is equivalent to `>1.0.0`
if v.local() == LocalVersionSlice::Sentinel {
if v.local() == LocalVersionSlice::Max {
*lower = Bound::Excluded(v.clone().without_local());
}
// `<1.0.0+[max]` is equivalent to `<1.0.0`
if b.local() == LocalVersionSlice::Sentinel {
if b.local() == LocalVersionSlice::Max {
*upper = Bound::Excluded(b.clone().without_local());
}
}
Expand All @@ -309,10 +309,10 @@ impl NoSolutionError {
let (Bound::Excluded(lower), Bound::Excluded(upper)) = (lower, upper) else {
return false;
};
if lower.local() == LocalVersionSlice::Sentinel {
if lower.local() == LocalVersionSlice::Max {
return false;
}
if upper.local() != LocalVersionSlice::Sentinel {
if upper.local() != LocalVersionSlice::Max {
return false;
}
*lower == upper.clone().without_local()
Expand Down

0 comments on commit e187a25

Please sign in to comment.