@@ -5,6 +5,8 @@ use std::fmt::{self, Display};
5
5
pub enum OptVersionReq {
6
6
Any ,
7
7
Req ( VersionReq ) ,
8
+ /// The exact locked version and the original version requirement.
9
+ Locked ( Version , VersionReq ) ,
8
10
}
9
11
10
12
pub trait VersionExt {
@@ -49,22 +51,48 @@ impl OptVersionReq {
49
51
cmp. op == Op :: Exact && cmp. minor . is_some ( ) && cmp. patch . is_some ( )
50
52
}
51
53
}
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 ,
52
78
}
53
79
}
54
80
55
81
pub fn matches ( & self , version : & Version ) -> bool {
56
82
match self {
57
83
OptVersionReq :: Any => true ,
58
84
OptVersionReq :: Req ( req) => req. matches ( version) ,
85
+ OptVersionReq :: Locked ( v, _) => VersionReq :: exact ( v) . matches ( version) ,
59
86
}
60
87
}
61
88
}
62
89
63
90
impl Display for OptVersionReq {
64
- fn fmt ( & self , formatter : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
91
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
65
92
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) ,
68
96
}
69
97
}
70
98
}
0 commit comments