Skip to content

Commit d6fd499

Browse files
committed
Add Locked variant for OptVersionReq
`OptVersionReq::Locked` stores the original version requirement and the acutal locked version. This makes error message describing more precise.
1 parent 72ef2bd commit d6fd499

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

src/cargo/util/semver_ext.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use std::fmt::{self, Display};
55
pub enum OptVersionReq {
66
Any,
77
Req(VersionReq),
8+
/// The exact locked version and the original version requirement.
9+
Locked(Version, VersionReq),
810
}
911

1012
pub trait VersionExt {
@@ -49,22 +51,48 @@ impl OptVersionReq {
4951
cmp.op == Op::Exact && cmp.minor.is_some() && cmp.patch.is_some()
5052
}
5153
}
54+
OptVersionReq::Locked(..) => true,
55+
}
56+
}
57+
58+
pub fn lock_to(&mut self, version: &Version) {
59+
assert!(self.matches(version), "cannot lock {} to {}", self, version);
60+
use OptVersionReq::*;
61+
let version = version.clone();
62+
*self = match self {
63+
Any => Locked(version, VersionReq::STAR),
64+
Req(req) => Locked(version, req.clone()),
65+
Locked(_, req) => Locked(version, req.clone()),
66+
};
67+
}
68+
69+
pub fn is_locked(&self) -> bool {
70+
matches!(self, OptVersionReq::Locked(..))
71+
}
72+
73+
/// Gets the version to which this req is locked, if any.
74+
pub fn locked_version(&self) -> Option<&Version> {
75+
match self {
76+
OptVersionReq::Locked(version, _) => Some(version),
77+
_ => None,
5278
}
5379
}
5480

5581
pub fn matches(&self, version: &Version) -> bool {
5682
match self {
5783
OptVersionReq::Any => true,
5884
OptVersionReq::Req(req) => req.matches(version),
85+
OptVersionReq::Locked(v, _) => VersionReq::exact(v).matches(version),
5986
}
6087
}
6188
}
6289

6390
impl Display for OptVersionReq {
64-
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
91+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6592
match self {
66-
OptVersionReq::Any => formatter.write_str("*"),
67-
OptVersionReq::Req(req) => Display::fmt(req, formatter),
93+
OptVersionReq::Any => f.write_str("*"),
94+
OptVersionReq::Req(req) => Display::fmt(req, f),
95+
OptVersionReq::Locked(_, req) => Display::fmt(req, f),
6896
}
6997
}
7098
}

0 commit comments

Comments
 (0)