1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use std::fmt::{Display, Formatter};
use std::{fmt, iter};
use serde::{Deserialize, Serialize};
use uv_pep440::{Version, VersionSpecifier, VersionSpecifiers};
/// The default version specifier when adding a dependency.
// While PEP 440 allows an arbitrary number of version digits, the `major` and `minor` build on
// most projects sticking to two or three components and a SemVer-ish versioning system, so can
// bump the major or minor version of a major.minor or major.minor.patch input version.
#[derive(Clone, Copy, Debug, Default, Deserialize, PartialEq, Eq, Serialize)]
#[serde(rename_all = "kebab-case")]
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub enum AddBoundsKind {
/// Only a lower bound, e.g., `>=1.2.3`.
#[default]
Lower,
/// Allow the same major version, similar to the semver caret, e.g., `>=1.2.3, <2.0.0`.
///
/// Leading zeroes are skipped, e.g. `>=0.1.2, <0.2.0`.
Major,
/// Allow the same minor version, similar to the semver tilde, e.g., `>=1.2.3, <1.3.0`.
///
/// Leading zeroes are skipped, e.g. `>=0.1.2, <0.1.3`.
Minor,
/// Pin the exact version, e.g., `==1.2.3`.
///
/// This option is not recommended, as versions are already pinned in the uv lockfile.
Exact,
}
impl Display for AddBoundsKind {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::Lower => write!(f, "lower"),
Self::Major => write!(f, "major"),
Self::Minor => write!(f, "minor"),
Self::Exact => write!(f, "exact"),
}
}
}
impl AddBoundsKind {
/// Return the version specifiers for this bound policy and a resolved version.
pub fn specifiers(self, version: Version) -> VersionSpecifiers {
// Nomenclature: "major" is the most significant component of the version, "minor" is the
// second most significant component, so most versions are either major.minor.patch or
// 0.major.minor.
match self {
Self::Lower => {
VersionSpecifiers::from(VersionSpecifier::greater_than_equal_version(version))
}
Self::Major => {
let leading_zeroes = version
.release()
.iter()
.take_while(|digit| **digit == 0)
.count();
// Special case: The version is 0.
if leading_zeroes == version.release().len() {
let upper_bound = Version::new(
[0, 1]
.into_iter()
.chain(iter::repeat_n(0, version.release().iter().skip(2).len())),
);
return VersionSpecifiers::from_iter([
VersionSpecifier::greater_than_equal_version(version),
VersionSpecifier::less_than_version(upper_bound),
]);
}
// Compute the new major version and pad it to the same length:
// 1.2.3 -> 2.0.0
// 1.2 -> 2.0
// 1 -> 2
// We ignore leading zeroes, adding Semver-style semantics to 0.x versions, too:
// 0.1.2 -> 0.2.0
// 0.0.1 -> 0.0.2
let major = version.release().get(leading_zeroes).copied().unwrap_or(0);
// The length of the lower bound minus the leading zero and bumped component.
let trailing_zeros = version.release().iter().skip(leading_zeroes + 1).len();
let upper_bound = Version::new(
iter::repeat_n(0, leading_zeroes)
.chain(iter::once(major + 1))
.chain(iter::repeat_n(0, trailing_zeros)),
);
VersionSpecifiers::from_iter([
VersionSpecifier::greater_than_equal_version(version),
VersionSpecifier::less_than_version(upper_bound),
])
}
Self::Minor => {
let leading_zeroes = version
.release()
.iter()
.take_while(|digit| **digit == 0)
.count();
// Special case: The version is 0.
if leading_zeroes == version.release().len() {
let upper_bound = [0, 0, 1]
.into_iter()
.chain(iter::repeat_n(0, version.release().iter().skip(3).len()));
return VersionSpecifiers::from_iter([
VersionSpecifier::greater_than_equal_version(version),
VersionSpecifier::less_than_version(Version::new(upper_bound)),
]);
}
// If both major and minor version are 0, the concept of bumping the minor version
// instead of the major version is not useful. Instead, we bump the next
// non-zero part of the version. This avoids extending the three components of 0.0.1
// to the four components of 0.0.1.1.
if leading_zeroes >= 2 {
let most_significant =
version.release().get(leading_zeroes).copied().unwrap_or(0);
// The length of the lower bound minus the leading zero and bumped component.
let trailing_zeros = version.release().iter().skip(leading_zeroes + 1).len();
let upper_bound = Version::new(
iter::repeat_n(0, leading_zeroes)
.chain(iter::once(most_significant + 1))
.chain(iter::repeat_n(0, trailing_zeros)),
);
return VersionSpecifiers::from_iter([
VersionSpecifier::greater_than_equal_version(version),
VersionSpecifier::less_than_version(upper_bound),
]);
}
// Compute the new minor version and pad it to the same length where possible:
// 1.2.3 -> 1.3.0
// 1.2 -> 1.3
// 1 -> 1.1
// We ignore leading zero, adding Semver-style semantics to 0.x versions, too:
// 0.1.2 -> 0.1.3
// 0.0.1 -> 0.0.2
// If the version has only one digit, say `1`, or if there are only leading zeroes,
// pad with zeroes.
let major = version.release().get(leading_zeroes).copied().unwrap_or(0);
let minor = version
.release()
.get(leading_zeroes + 1)
.copied()
.unwrap_or(0);
let upper_bound = Version::new(
iter::repeat_n(0, leading_zeroes)
.chain(iter::once(major))
.chain(iter::once(minor + 1))
.chain(iter::repeat_n(
0,
version.release().iter().skip(leading_zeroes + 2).len(),
)),
);
VersionSpecifiers::from_iter([
VersionSpecifier::greater_than_equal_version(version),
VersionSpecifier::less_than_version(upper_bound),
])
}
Self::Exact => {
VersionSpecifiers::from_iter([VersionSpecifier::equals_version(version)])
}
}
}
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
use uv_pep440::Version;
use super::AddBoundsKind;
#[test]
fn bound_kind_to_specifiers_exact() {
let tests = [
("0", "==0"),
("0.0", "==0.0"),
("0.0.0", "==0.0.0"),
("0.1", "==0.1"),
("0.0.1", "==0.0.1"),
("0.0.0.1", "==0.0.0.1"),
("1.0.0", "==1.0.0"),
("1.2", "==1.2"),
("1.2.3", "==1.2.3"),
("1.2.3.4", "==1.2.3.4"),
("1.2.3.4a1.post1", "==1.2.3.4a1.post1"),
];
for (version, expected) in tests {
let actual = AddBoundsKind::Exact
.specifiers(Version::from_str(version).unwrap())
.to_string();
assert_eq!(actual, expected, "{version}");
}
}
#[test]
fn bound_kind_to_specifiers_lower() {
let tests = [
("0", ">=0"),
("0.0", ">=0.0"),
("0.0.0", ">=0.0.0"),
("0.1", ">=0.1"),
("0.0.1", ">=0.0.1"),
("0.0.0.1", ">=0.0.0.1"),
("1", ">=1"),
("1.0.0", ">=1.0.0"),
("1.2", ">=1.2"),
("1.2.3", ">=1.2.3"),
("1.2.3.4", ">=1.2.3.4"),
("1.2.3.4a1.post1", ">=1.2.3.4a1.post1"),
];
for (version, expected) in tests {
let actual = AddBoundsKind::Lower
.specifiers(Version::from_str(version).unwrap())
.to_string();
assert_eq!(actual, expected, "{version}");
}
}
#[test]
fn bound_kind_to_specifiers_major() {
let tests = [
("0", ">=0, <0.1"),
("0.0", ">=0.0, <0.1"),
("0.0.0", ">=0.0.0, <0.1.0"),
("0.0.0.0", ">=0.0.0.0, <0.1.0.0"),
("0.1", ">=0.1, <0.2"),
("0.0.1", ">=0.0.1, <0.0.2"),
("0.0.1.1", ">=0.0.1.1, <0.0.2.0"),
("0.0.0.1", ">=0.0.0.1, <0.0.0.2"),
("1", ">=1, <2"),
("1.0.0", ">=1.0.0, <2.0.0"),
("1.2", ">=1.2, <2.0"),
("1.2.3", ">=1.2.3, <2.0.0"),
("1.2.3.4", ">=1.2.3.4, <2.0.0.0"),
("1.2.3.4a1.post1", ">=1.2.3.4a1.post1, <2.0.0.0"),
];
for (version, expected) in tests {
let actual = AddBoundsKind::Major
.specifiers(Version::from_str(version).unwrap())
.to_string();
assert_eq!(actual, expected, "{version}");
}
}
#[test]
fn bound_kind_to_specifiers_minor() {
let tests = [
("0", ">=0, <0.0.1"),
("0.0", ">=0.0, <0.0.1"),
("0.0.0", ">=0.0.0, <0.0.1"),
("0.0.0.0", ">=0.0.0.0, <0.0.1.0"),
("0.1", ">=0.1, <0.1.1"),
("0.0.1", ">=0.0.1, <0.0.2"),
("0.0.1.1", ">=0.0.1.1, <0.0.2.0"),
("0.0.0.1", ">=0.0.0.1, <0.0.0.2"),
("1", ">=1, <1.1"),
("1.0.0", ">=1.0.0, <1.1.0"),
("1.2", ">=1.2, <1.3"),
("1.2.3", ">=1.2.3, <1.3.0"),
("1.2.3.4", ">=1.2.3.4, <1.3.0.0"),
("1.2.3.4a1.post1", ">=1.2.3.4a1.post1, <1.3.0.0"),
];
for (version, expected) in tests {
let actual = AddBoundsKind::Minor
.specifiers(Version::from_str(version).unwrap())
.to_string();
assert_eq!(actual, expected, "{version}");
}
}
}