aboutsummaryrefslogtreecommitdiffstats
path: root/lua/mason-core/pep440/init.lua
blob: 0141c501dda75106ad4080dd1e23d461f7a25f57 (plain) (blame)
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
-- Function to split a version string into its components
local function split_version(version)
    local parts = {}
    for part in version:gmatch "[^.]+" do
        table.insert(parts, tonumber(part) or part)
    end
    return parts
end

-- Function to compare two versions
local function compare_versions(version1, version2)
    local v1_parts = split_version(version1)
    local v2_parts = split_version(version2)
    local len = math.max(#v1_parts, #v2_parts)

    for i = 1, len do
        local v1_part = v1_parts[i] or 0
        local v2_part = v2_parts[i] or 0

        if v1_part < v2_part then
            return -1
        elseif v1_part > v2_part then
            return 1
        end
    end

    return 0
end

-- Function to check a version against a single specifier
local function check_single_specifier(version, specifier)
    local operator, spec_version = specifier:match "^([<>=!]+)%s*(.+)$"
    local comp_result = compare_versions(version, spec_version)

    if operator == "==" then
        return comp_result == 0
    elseif operator == "!=" then
        return comp_result ~= 0
    elseif operator == "<=" then
        return comp_result <= 0
    elseif operator == "<" then
        return comp_result < 0
    elseif operator == ">=" then
        return comp_result >= 0
    elseif operator == ">" then
        return comp_result > 0
    else
        error("Invalid operator in version specifier: " .. operator)
    end
end

-- Function to check a version against multiple specifiers
local function check_version(version, specifiers)
    for specifier in specifiers:gmatch "[^,]+" do
        if not check_single_specifier(version, specifier:match "^%s*(.-)%s*$") then
            return false
        end
    end
    return true
end

return {
    check_version = check_version,
}