Skip to content

Commit 1224aa9

Browse files
committed
Add specs to the URI module (#4711)
1 parent bc75075 commit 1224aa9

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

lib/elixir/lib/uri.ex

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,16 @@ defmodule URI do
1111
fragment: nil, authority: nil,
1212
userinfo: nil, host: nil, port: nil
1313

14-
@type t :: %__MODULE__{}
14+
@type t :: %__MODULE__{
15+
scheme: nil | binary,
16+
path: nil | binary,
17+
query: nil | binary,
18+
fragment: nil | binary,
19+
authority: nil | binary,
20+
userinfo: nil | binary,
21+
host: nil | binary,
22+
port: nil | :inet.port_number,
23+
}
1524

1625
import Bitwise
1726

@@ -31,6 +40,7 @@ defmodule URI do
3140
nil
3241
3342
"""
43+
@spec default_port(binary) :: nil | pos_integer
3444
def default_port(scheme) when is_binary(scheme) do
3545
:elixir_config.get({:uri, scheme})
3646
end
@@ -47,6 +57,7 @@ defmodule URI do
4757
application's start callback in case you want to register
4858
new URIs.
4959
"""
60+
@spec default_port(binary, pos_integer) :: :ok
5061
def default_port(scheme, port) when is_binary(scheme) and port > 0 do
5162
:elixir_config.put({:uri, scheme}, port)
5263
end
@@ -76,6 +87,7 @@ defmodule URI do
7687
** (ArgumentError) encode_query/1 values cannot be lists, got: [:a, :list]
7788
7889
"""
90+
@spec encode_query(term) :: binary
7991
def encode_query(l), do: Enum.map_join(l, "&", &pair/1)
8092

8193
@doc """
@@ -97,6 +109,7 @@ defmodule URI do
97109
%{"percent" => "oh yes!", "starting" => "map"}
98110
99111
"""
112+
@spec decode_query(binary, map) :: map
100113
def decode_query(q, map \\ %{})
101114

102115
def decode_query(q, %{__struct__: _} = dict) when is_binary(q) do
@@ -139,6 +152,7 @@ defmodule URI do
139152
[{"foo", "1"}, {"bar", "2"}]
140153
141154
"""
155+
@spec query_decoder(binary) :: Enumerable.t
142156
def query_decoder(q) when is_binary(q) do
143157
Stream.unfold(q, &do_decode_query/1)
144158
end
@@ -190,6 +204,7 @@ defmodule URI do
190204
true
191205
192206
"""
207+
@spec char_reserved?(term) :: boolean
193208
def char_reserved?(c) do
194209
c in ':/?#[]@!$&\'()*+,;='
195210
end
@@ -206,6 +221,7 @@ defmodule URI do
206221
true
207222
208223
"""
224+
@spec char_unreserved?(term) :: boolean
209225
def char_unreserved?(c) do
210226
c in ?0..?9 or
211227
c in ?a..?z or
@@ -225,6 +241,7 @@ defmodule URI do
225241
false
226242
227243
"""
244+
@spec char_unescaped?(term) :: boolean
228245
def char_unescaped?(c) do
229246
char_reserved?(c) or char_unreserved?(c)
230247
end
@@ -246,6 +263,7 @@ defmodule URI do
246263
"a str%69ng"
247264
248265
"""
266+
@spec encode(binary, (byte -> boolean)) :: binary
249267
def encode(str, predicate \\ &char_unescaped?/1) when is_binary(str) do
250268
for <<c <- str>>, into: "", do: percent(c, predicate)
251269
end
@@ -259,6 +277,7 @@ defmodule URI do
259277
"put%3A+it%2B%D0%B9"
260278
261279
"""
280+
@spec encode_www_form(binary) :: binary
262281
def encode_www_form(str) when is_binary(str) do
263282
for <<c <- str>>, into: "" do
264283
case percent(c, &char_unreserved?/1) do
@@ -288,6 +307,7 @@ defmodule URI do
288307
"http://elixir-lang.org"
289308
290309
"""
310+
@spec decode(binary) :: binary
291311
def decode(uri) do
292312
unpercent(uri, "", false)
293313
catch
@@ -304,6 +324,7 @@ defmodule URI do
304324
"<all in/"
305325
306326
"""
327+
@spec decode_www_form(binary) :: binary
307328
def decode_www_form(str) do
308329
unpercent(str, "", true)
309330
catch
@@ -366,6 +387,9 @@ defmodule URI do
366387
port: nil, query: nil, scheme: nil, userinfo: nil}
367388
368389
"""
390+
@spec parse(t | binary) :: t
391+
def parse(uri)
392+
369393
def parse(%URI{} = uri), do: uri
370394

371395
def parse(s) when is_binary(s) do
@@ -419,6 +443,7 @@ defmodule URI do
419443
"foo://bar.baz"
420444
421445
"""
446+
@spec to_string(t) :: binary
422447
defdelegate to_string(uri), to: String.Chars.URI
423448

424449
@doc ~S"""
@@ -436,6 +461,9 @@ defmodule URI do
436461
"http://google.com"
437462
438463
"""
464+
@spec merge(t | binary, t | binary) :: t
465+
def merge(uri, rel)
466+
439467
def merge(%URI{authority: nil}, _rel) do
440468
raise ArgumentError, "you must merge onto an absolute URI"
441469
end

0 commit comments

Comments
 (0)