diff options
author | V1K1NGbg <victor@ilchev.com> | 2024-08-22 17:48:57 -0300 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2024-10-21 13:30:51 -0300 |
commit | 1e5e7ba13a5230955785cd3989beabb60418c2a9 (patch) | |
tree | 0bb22348f784096acbbcf7562675c1460c96984b | |
parent | 68a5ee0d17a366cd49c4fa9ff6ac887fe917acd8 (diff) | |
download | luarocks-1e5e7ba13a5230955785cd3989beabb60418c2a9.tar.gz luarocks-1e5e7ba13a5230955785cd3989beabb60418c2a9.tar.bz2 luarocks-1e5e7ba13a5230955785cd3989beabb60418c2a9.zip |
Teal: convert luarocks.queries
-rw-r--r-- | src/luarocks/queries.tl (renamed from src/luarocks/queries.lua) | 74 |
1 files changed, 34 insertions, 40 deletions
diff --git a/src/luarocks/queries.lua b/src/luarocks/queries.tl index 0c8790fa..303d9250 100644 --- a/src/luarocks/queries.lua +++ b/src/luarocks/queries.tl | |||
@@ -1,20 +1,23 @@ | |||
1 | 1 | ||
2 | local queries = {} | 2 | local record queries |
3 | end | ||
3 | 4 | ||
4 | local vers = require("luarocks.core.vers") | 5 | local vers = require("luarocks.core.vers") |
5 | local util = require("luarocks.util") | 6 | local util = require("luarocks.util") |
6 | local cfg = require("luarocks.core.cfg") | 7 | local cfg = require("luarocks.core.cfg") |
7 | 8 | ||
8 | local query_mt = {} | 9 | local query = require("luarocks.core.types.query") |
10 | local type Query = query.Query | ||
9 | 11 | ||
10 | query_mt.__index = query_mt | 12 | local type Version = require("luarocks.core.types.version").Version |
13 | local type Constraint = require("luarocks.core.types.version").Constraint | ||
11 | 14 | ||
12 | function query_mt.type() | 15 | local query_mt: metatable<Query> = {} |
13 | return "query" | 16 | |
14 | end | 17 | query_mt.__index = query.Query |
15 | 18 | ||
16 | -- Fallback default value for the `arch` field, if not explicitly set. | 19 | -- Fallback default value for the `arch` field, if not explicitly set. |
17 | query_mt.arch = { | 20 | query.Query.arch = { |
18 | src = true, | 21 | src = true, |
19 | all = true, | 22 | all = true, |
20 | rockspec = true, | 23 | rockspec = true, |
@@ -23,14 +26,14 @@ query_mt.arch = { | |||
23 | } | 26 | } |
24 | 27 | ||
25 | -- Fallback default value for the `substring` field, if not explicitly set. | 28 | -- Fallback default value for the `substring` field, if not explicitly set. |
26 | query_mt.substring = false | 29 | query.Query.substring = false |
27 | 30 | ||
28 | --- Convert the arch field of a query table to table format. | 31 | --- Convert the arch field of a query table to table format. |
29 | -- @param input string, table or nil | 32 | -- @param input string, table or nil |
30 | local function arch_to_table(input) | 33 | local function arch_to_table(input: string | {string: boolean}): {string: boolean} |
31 | if type(input) == "table" then | 34 | if input is {string: boolean} then |
32 | return input | 35 | return input |
33 | elseif type(input) == "string" then | 36 | elseif input is string then |
34 | local arch = {} | 37 | local arch = {} |
35 | for a in input:gmatch("[%w_-]+") do | 38 | for a in input:gmatch("[%w_-]+") do |
36 | arch[a] = true | 39 | arch[a] = true |
@@ -48,17 +51,11 @@ end | |||
48 | -- @param arch string?: a string with pipe-separated accepted arch values | 51 | -- @param arch string?: a string with pipe-separated accepted arch values |
49 | -- @param operator string?: operator for version matching (default is "==") | 52 | -- @param operator string?: operator for version matching (default is "==") |
50 | -- @return table: A query in table format | 53 | -- @return table: A query in table format |
51 | function queries.new(name, namespace, version, substring, arch, operator) | 54 | function queries.new(name: string, namespace?: string, version?: string, substring?: boolean, arch?: string, operator?: string): Query |
52 | assert(type(name) == "string") | ||
53 | assert(type(namespace) == "string" or not namespace) | ||
54 | assert(type(version) == "string" or not version) | ||
55 | assert(type(substring) == "boolean" or not substring) | ||
56 | assert(type(arch) == "string" or not arch) | ||
57 | assert(type(operator) == "string" or not operator) | ||
58 | 55 | ||
59 | operator = operator or "==" | 56 | operator = operator or "==" |
60 | 57 | ||
61 | local self = { | 58 | local self: Query = { |
62 | name = name, | 59 | name = name, |
63 | namespace = namespace, | 60 | namespace = namespace, |
64 | constraints = {}, | 61 | constraints = {}, |
@@ -69,24 +66,23 @@ function queries.new(name, namespace, version, substring, arch, operator) | |||
69 | table.insert(self.constraints, { op = operator, version = vers.parse_version(version)}) | 66 | table.insert(self.constraints, { op = operator, version = vers.parse_version(version)}) |
70 | end | 67 | end |
71 | 68 | ||
72 | query_mt.arch[cfg.arch] = true | 69 | query.Query.arch[cfg.arch] = true |
73 | return setmetatable(self, query_mt) | 70 | return setmetatable(self, query_mt) |
74 | end | 71 | end |
75 | 72 | ||
76 | -- Query for all packages | 73 | -- Query for all packages |
77 | -- @param arch string (optional) | 74 | -- @param arch string (optional) |
78 | function queries.all(arch) | 75 | function queries.all(arch?: string): Query |
79 | assert(type(arch) == "string" or not arch) | ||
80 | 76 | ||
81 | return queries.new("", nil, nil, true, arch) | 77 | return queries.new("", nil, nil, true, arch) |
82 | end | 78 | end |
83 | 79 | ||
84 | do | 80 | do |
85 | local parse_constraints | 81 | local parse_constraints: function(string): {Constraint}, string |
86 | do | 82 | do |
87 | local parse_constraint | 83 | local parse_constraint: function(string): Constraint, string |
88 | do | 84 | do |
89 | local operators = { | 85 | local operators: {string: string} = { |
90 | ["=="] = "==", | 86 | ["=="] = "==", |
91 | ["~="] = "~=", | 87 | ["~="] = "~=", |
92 | [">"] = ">", | 88 | [">"] = ">", |
@@ -108,19 +104,18 @@ do | |||
108 | -- @return (table, string) or nil: A table representing the same | 104 | -- @return (table, string) or nil: A table representing the same |
109 | -- constraints and the string with the unused input, or nil if the | 105 | -- constraints and the string with the unused input, or nil if the |
110 | -- input string is invalid. | 106 | -- input string is invalid. |
111 | parse_constraint = function(input) | 107 | parse_constraint = function(input: string): {string: any}, string |
112 | assert(type(input) == "string") | ||
113 | 108 | ||
114 | local no_upgrade, op, version, rest = input:match("^(@?)([<>=~!]*)%s*([%w%.%_%-]+)[%s,]*(.*)") | 109 | local no_upgrade, op, versionstr, rest = input:match("^(@?)([<>=~!]*)%s*([%w%.%_%-]+)[%s,]*(.*)") |
115 | local _op = operators[op] | 110 | local _op = operators[op] |
116 | version = vers.parse_version(version) | 111 | local version = vers.parse_version(versionstr) |
117 | if not _op then | 112 | if not _op then |
118 | return nil, "Encountered bad constraint operator: '"..tostring(op).."' in '"..input.."'" | 113 | return nil, "Encountered bad constraint operator: '"..tostring(op).."' in '"..input.."'" |
119 | end | 114 | end |
120 | if not version then | 115 | if not version then |
121 | return nil, "Could not parse version from constraint: '"..input.."'" | 116 | return nil, "Could not parse version from constraint: '"..input.."'" |
122 | end | 117 | end |
123 | return { op = _op, version = version, no_upgrade = no_upgrade=="@" and true or nil }, rest | 118 | return { op = _op, version = version, no_upgrade = no_upgrade=="@" and true or nil }, rest --? false instead of nil |
124 | end | 119 | end |
125 | end | 120 | end |
126 | 121 | ||
@@ -132,10 +127,10 @@ do | |||
132 | -- @param input string: A list of constraints in string format. | 127 | -- @param input string: A list of constraints in string format. |
133 | -- @return table or nil: A table representing the same constraints, | 128 | -- @return table or nil: A table representing the same constraints, |
134 | -- or nil if the input string is invalid. | 129 | -- or nil if the input string is invalid. |
135 | parse_constraints = function(input) | 130 | parse_constraints = function(input: string): {Constraint}, string |
136 | assert(type(input) == "string") | ||
137 | 131 | ||
138 | local constraints, oinput, constraint = {}, input | 132 | local constraints, oinput = {}, input |
133 | local constraint: Constraint | ||
139 | while #input > 0 do | 134 | while #input > 0 do |
140 | constraint, input = parse_constraint(input) | 135 | constraint, input = parse_constraint(input) |
141 | if constraint then | 136 | if constraint then |
@@ -152,8 +147,7 @@ do | |||
152 | -- @param depstr string: A dependency in string format | 147 | -- @param depstr string: A dependency in string format |
153 | -- as entered in rockspec files. | 148 | -- as entered in rockspec files. |
154 | -- @return table: A query in table format, or nil and an error message in case of errors. | 149 | -- @return table: A query in table format, or nil and an error message in case of errors. |
155 | function queries.from_dep_string(depstr) | 150 | function queries.from_dep_string(depstr: string): Query, string |
156 | assert(type(depstr) == "string") | ||
157 | 151 | ||
158 | local ns_name, rest = depstr:match("^%s*([a-zA-Z0-9%.%-%_]*/?[a-zA-Z0-9][a-zA-Z0-9%.%-%_]*)%s*([^/]*)") | 152 | local ns_name, rest = depstr:match("^%s*([a-zA-Z0-9%.%-%_]*/?[a-zA-Z0-9][a-zA-Z0-9%.%-%_]*)%s*([^/]*)") |
159 | if not ns_name then | 153 | if not ns_name then |
@@ -175,13 +169,13 @@ do | |||
175 | constraints = constraints, | 169 | constraints = constraints, |
176 | } | 170 | } |
177 | 171 | ||
178 | query_mt.arch[cfg.arch] = true | 172 | query.Query.arch[cfg.arch] = true |
179 | return setmetatable(self, query_mt) | 173 | return setmetatable(self, query_mt) |
180 | end | 174 | end |
181 | end | 175 | end |
182 | 176 | ||
183 | function queries.from_persisted_table(tbl) | 177 | function queries.from_persisted_table(tbl: Query): Query |
184 | query_mt.arch[cfg.arch] = true | 178 | query.Query.arch[cfg.arch] = true |
185 | return setmetatable(tbl, query_mt) | 179 | return setmetatable(tbl, query_mt) |
186 | end | 180 | end |
187 | 181 | ||
@@ -189,7 +183,7 @@ end | |||
189 | -- Includes namespace, name and version, but not arch or constraints. | 183 | -- Includes namespace, name and version, but not arch or constraints. |
190 | -- @param query table: a query table | 184 | -- @param query table: a query table |
191 | -- @return string: a result such as `my_user/my_rock 1.0` or `my_rock`. | 185 | -- @return string: a result such as `my_user/my_rock 1.0` or `my_rock`. |
192 | function query_mt:__tostring() | 186 | function query_mt.__tostring(self: Query): string |
193 | local out = {} | 187 | local out = {} |
194 | if self.namespace then | 188 | if self.namespace then |
195 | table.insert(out, self.namespace) | 189 | table.insert(out, self.namespace) |
@@ -200,7 +194,7 @@ function query_mt:__tostring() | |||
200 | if #self.constraints > 0 then | 194 | if #self.constraints > 0 then |
201 | local pretty = {} | 195 | local pretty = {} |
202 | for _, c in ipairs(self.constraints) do | 196 | for _, c in ipairs(self.constraints) do |
203 | local v = c.version.string | 197 | local v = tostring(c.version) |
204 | if c.op == "==" then | 198 | if c.op == "==" then |
205 | table.insert(pretty, v) | 199 | table.insert(pretty, v) |
206 | else | 200 | else |