Skip to content

Commit 1803c8b

Browse files
nazo6saecki
andauthored
fix: sort fetched crate versions (#156)
* fix: sort fetched crate versions * fix: also sort fetched crate versions by build-metatdata --------- Co-authored-by: Tobias Schmitz <[email protected]>
1 parent 8535680 commit 1803c8b

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

lua/crates/api.lua

+24
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,26 @@ function M.fetch_search(name)
266266
end)
267267
end
268268

269+
---@param a SemVer
270+
---@param b SemVer
271+
---@return boolean
272+
local function sort_semvers(a, b)
273+
if a.major ~= b.major then
274+
return a.major > b.major
275+
end
276+
if a.minor ~= b.minor then
277+
return a.minor > b.minor
278+
end
279+
if a.patch ~= b.patch then
280+
return a.patch > b.patch
281+
end
282+
local pre = semver.compare_pre(a.pre, b.pre)
283+
if pre ~= 0 then
284+
return pre > 0
285+
end
286+
return semver.compare_meta(a.meta, b.meta) > 0
287+
end
288+
269289
---@param a ApiFeature
270290
---@param b ApiFeature
271291
---@return boolean
@@ -445,6 +465,10 @@ function M.parse_crate(index_json_str, meta_json)
445465
version.created = assert(DateTime.parse_rfc_3339(v.created_at))
446466
table.insert(crate.versions, version)
447467
end
468+
-- sort versions
469+
table.sort(crate.versions, function(a, b)
470+
return sort_semvers(a.parsed, b.parsed)
471+
end)
448472

449473
return crate
450474
end

lua/crates/semver.lua

+26-7
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,29 @@ function M.parse_requirements(str)
200200
return requirements
201201
end
202202

203-
---@param version string
204-
---@param req string
203+
-- TODO: port https://github.com/dtolnay/semver/blob/master/src%2Fimpls.rs#L51-L107
204+
---@param version? string
205+
---@param req? string
205206
---@return integer
206-
local function compare_pre(version, req)
207+
function M.compare_pre(version, req)
208+
if version and req then
209+
if version < req then
210+
return -1
211+
elseif version == req then
212+
return 0
213+
elseif version > req then
214+
return 1
215+
end
216+
end
217+
218+
return (req and 1 or 0) - (version and 1 or 0)
219+
end
220+
221+
-- TODO: port https://github.com/dtolnay/semver/blob/master/src/impls.rs#L109-L153
222+
---@param version? string
223+
---@param req? string
224+
---@return integer
225+
function M.compare_meta(version, req)
207226
if version and req then
208227
if version < req then
209228
return -1
@@ -231,7 +250,7 @@ local function matches_less(version, req)
231250
return version.patch < req.patch
232251
end
233252

234-
return compare_pre(version.pre, req.pre) < 0
253+
return M.compare_pre(version.pre, req.pre) < 0
235254
end
236255

237256
---@param version SemVer
@@ -248,7 +267,7 @@ local function matches_greater(version, req)
248267
return version.patch > req.patch
249268
end
250269

251-
return compare_pre(version.pre, req.pre) > 0
270+
return M.compare_pre(version.pre, req.pre) > 0
252271
end
253272

254273
---@param version SemVer
@@ -304,7 +323,7 @@ local function matches_caret(version, req)
304323
return false
305324
end
306325

307-
return compare_pre(version.pre, req.pre) >= 0
326+
return M.compare_pre(version.pre, req.pre) >= 0
308327
end
309328

310329
---@param version SemVer
@@ -321,7 +340,7 @@ local function matches_tilde(version, req)
321340
return version.patch > req.patch
322341
end
323342

324-
return compare_pre(version.pre, req.pre) >= 0
343+
return M.compare_pre(version.pre, req.pre) >= 0
325344
end
326345

327346
---@param v SemVer

0 commit comments

Comments
 (0)