-
Notifications
You must be signed in to change notification settings - Fork 3k
Update the tilde version specifier warning to include more context #14335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -665,11 +665,6 @@ impl VersionSpecifier { | |
| | Operator::NotEqual => false, | ||
| } | ||
| } | ||
|
|
||
| /// Returns true if this is a `~=` specifier without a patch version (e.g. `~=3.11`). | ||
| pub fn is_tilde_without_patch(&self) -> bool { | ||
| self.operator == Operator::TildeEqual && self.version.release().len() == 2 | ||
| } | ||
| } | ||
|
|
||
| impl FromStr for VersionSpecifier { | ||
|
|
@@ -893,6 +888,90 @@ pub(crate) fn parse_version_specifiers( | |
| Ok(version_ranges) | ||
| } | ||
|
|
||
| /// A simple `~=` version specifier with a major, minor and (optional) patch version, e.g., `~=3.13` | ||
| /// or `~=3.13.0`. | ||
| #[derive(Clone, Debug)] | ||
| pub struct TildeVersionSpecifier<'a> { | ||
| inner: Cow<'a, VersionSpecifier>, | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alas I wrote this as using a borrowed version specifier then wanted to use
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. imo we can just always clone those outside the resolver, it's not worth the complexity
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The annoying part is that I didn't want to take an owned value for the |
||
| } | ||
|
|
||
| impl<'a> TildeVersionSpecifier<'a> { | ||
| /// Create a new [`TildeVersionSpecifier`] from a [`VersionSpecifier`] value. | ||
| /// | ||
| /// If a [`Operator::TildeEqual`] is not used, or the version includes more than minor and patch | ||
| /// segments, this will return [`None`]. | ||
| pub fn from_specifier(specifier: VersionSpecifier) -> Option<TildeVersionSpecifier<'a>> { | ||
| TildeVersionSpecifier::new(Cow::Owned(specifier)) | ||
| } | ||
|
|
||
| /// Create a new [`TildeVersionSpecifier`] from a [`VersionSpecifier`] reference. | ||
| /// | ||
| /// See [`TildeVersionSpecifier::from_specifier`]. | ||
| pub fn from_specifier_ref( | ||
| specifier: &'a VersionSpecifier, | ||
| ) -> Option<TildeVersionSpecifier<'a>> { | ||
| TildeVersionSpecifier::new(Cow::Borrowed(specifier)) | ||
| } | ||
|
|
||
| fn new(specifier: Cow<'a, VersionSpecifier>) -> Option<Self> { | ||
| if specifier.operator != Operator::TildeEqual { | ||
| return None; | ||
| } | ||
| if specifier.version().release().len() < 2 || specifier.version().release().len() > 3 { | ||
| return None; | ||
| } | ||
| if specifier.version().any_prerelease() | ||
| || specifier.version().is_local() | ||
| || specifier.version().is_post() | ||
| { | ||
| return None; | ||
| } | ||
| Some(Self { inner: specifier }) | ||
| } | ||
|
|
||
| /// Whether a patch version is present in this tilde version specifier. | ||
| pub fn has_patch(&self) -> bool { | ||
| self.inner.version.release().len() == 3 | ||
| } | ||
|
|
||
| /// Construct the lower and upper bounding version specifiers for this tilde version specifier, | ||
| /// e.g., for `~=3.13` this would return `>=3.13` and `<4` and for `~=3.13.0` it would | ||
| /// return `>=3.13.0` and `<3.14`. | ||
| pub fn bounding_specifiers(&self) -> (VersionSpecifier, VersionSpecifier) { | ||
| let release = self.inner.version().release(); | ||
| let lower = self.inner.version.clone(); | ||
| let upper = if self.has_patch() { | ||
| Version::new([release[0], release[1] + 1]) | ||
| } else { | ||
| Version::new([release[0] + 1]) | ||
| }; | ||
| ( | ||
| VersionSpecifier::greater_than_equal_version(lower), | ||
| VersionSpecifier::less_than_version(upper), | ||
| ) | ||
| } | ||
|
|
||
| /// Construct a new tilde `VersionSpecifier` with the given patch version appended. | ||
| pub fn with_patch_version(&self, patch: u64) -> TildeVersionSpecifier { | ||
| let mut release = self.inner.version.release().to_vec(); | ||
| if self.has_patch() { | ||
| release.pop(); | ||
| } | ||
| release.push(patch); | ||
| TildeVersionSpecifier::from_specifier( | ||
| VersionSpecifier::from_version(Operator::TildeEqual, Version::new(release)) | ||
| .expect("We should always derive a valid new version specifier"), | ||
| ) | ||
| .expect("We should always derive a new tilde version specifier") | ||
| } | ||
| } | ||
|
|
||
| impl std::fmt::Display for TildeVersionSpecifier<'_> { | ||
| fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
| write!(f, "{}", self.inner) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::{cmp::Ordering, str::FromStr}; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This moved up a level, where was have access to the project and group names the specifiers come from