Skip to content

Commit d38d0a5

Browse files
committed
Apply mix format
1 parent c0df072 commit d38d0a5

19 files changed

+978
-480
lines changed

lib/builder.ex

+86-29
Large diffs are not rendered by default.

lib/decoder.ex

+254-80
Large diffs are not rendered by default.

lib/document.ex

+51-35
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,59 @@
11
defmodule Toml.Document do
22
@moduledoc false
3-
3+
44
# Represents a TOML document, and handles conversion to a plain map
55
# See `Toml.Builder` for the actual logic for constructing the document.
66

77
defstruct [:keys, :comments, :open_table, :comment_stack, :keyfun, :transforms]
8-
8+
99
# A key is either binary or atom depending on the decoder option value
1010
@type key :: binary | atom | term
11-
11+
1212
# A value is the fully decoded value from the TOML
13-
@type value :: %{key => value}
14-
| {:table_array, [%{key => value}]}
15-
| number
16-
| binary
17-
| NaiveDateTime.t
18-
| DateTime.t
19-
| Date.t
20-
| Time.t
21-
| [value]
22-
13+
@type value ::
14+
%{key => value}
15+
| {:table_array, [%{key => value}]}
16+
| number
17+
| binary
18+
| NaiveDateTime.t()
19+
| DateTime.t()
20+
| Date.t()
21+
| Time.t()
22+
| [value]
23+
2324
# A keypath is a list of keys, they are all of the same key type
2425
@type keypath :: list(binary) | list(atom) | list(term)
25-
26+
2627
@type t :: %__MODULE__{
27-
keys: %{key => value},
28-
comments: %{keypath => binary},
29-
open_table: keypath | nil,
30-
comment_stack: [binary],
31-
keyfun: nil | ((binary) -> term | no_return),
32-
transforms: [Toml.Transform.t]
33-
}
34-
28+
keys: %{key => value},
29+
comments: %{keypath => binary},
30+
open_table: keypath | nil,
31+
comment_stack: [binary],
32+
keyfun: nil | (binary -> term | no_return),
33+
transforms: [Toml.Transform.t()]
34+
}
35+
3536
@doc """
3637
Create a new empty TOML document
3738
"""
38-
@spec new(Toml.opts) :: t
39+
@spec new(Toml.opts()) :: t
3940
def new(opts) when is_list(opts) do
4041
keyfun = to_key_fun(Keyword.get(opts, :keys, :strings))
4142
transforms = Keyword.get(opts, :transforms, [])
43+
4244
%__MODULE__{
43-
keys: %{},
44-
comments: %{},
45+
keys: %{},
46+
comments: %{},
4547
open_table: nil,
4648
comment_stack: [],
4749
keyfun: keyfun,
4850
transforms: transforms
4951
}
5052
end
51-
53+
5254
@doc """
5355
Convert the given TOML document to a plain map.
54-
56+
5557
During conversion to a plain map, keys are converted according
5658
to the key type defined when the document was created.
5759
@@ -68,9 +70,11 @@ defmodule Toml.Document do
6870
case ts do
6971
[] ->
7072
nil
73+
7174
ts when is_list(ts) ->
7275
Toml.Transform.compose(ts)
7376
end
77+
7478
{:ok, to_map2(keys, keyfun, transform)}
7579
catch
7680
_, {:error, {:keys, {:non_existing_atom, _}}} = err ->
@@ -81,19 +85,23 @@ defmodule Toml.Document do
8185
defp to_map2(m, nil, nil) when is_map(m) do
8286
for {k, v} <- m, into: %{}, do: {k, to_map3(k, v, nil, nil)}
8387
end
88+
8489
defp to_map2(m, keyfun, nil) when is_map(m) and is_function(keyfun, 1) do
85-
for {k, v} <- m, into: %{} do
90+
for {k, v} <- m, into: %{} do
8691
k2 = keyfun.(k)
8792
{k2, to_map3(k2, v, keyfun, nil)}
8893
end
8994
end
95+
9096
defp to_map2(m, nil, transform) when is_map(m) and is_function(transform, 2) do
9197
for {k, v} <- m, into: %{} do
9298
v2 = to_map3(k, v, nil, transform)
9399
{k, v2}
94100
end
95101
end
96-
defp to_map2(m, keyfun, transform) when is_map(m) and is_function(keyfun, 1) and is_function(transform, 2) do
102+
103+
defp to_map2(m, keyfun, transform)
104+
when is_map(m) and is_function(keyfun, 1) and is_function(transform, 2) do
97105
for {k, v} <- m, into: %{} do
98106
k2 = keyfun.(k)
99107
v2 = to_map3(k2, v, keyfun, transform)
@@ -104,39 +112,46 @@ defmodule Toml.Document do
104112
# Called when a table value is being converted
105113
defp to_map3(_key, %_{} = s, _keyfun, nil), do: s
106114
defp to_map3(key, %_{} = s, _keyfun, transform), do: transform.(key, s)
115+
107116
defp to_map3(key, list, keyfun, nil) when is_list(list) do
108117
for v <- list, do: to_map3(key, v, keyfun, nil)
109118
end
119+
110120
defp to_map3(key, list, _keyfun, transform) when is_list(list) do
111121
transform.(key, list)
112122
end
113-
defp to_map3(_key, {:table_array, list}, keyfun, transform) do
123+
124+
defp to_map3(_key, {:table_array, list}, keyfun, transform) do
114125
for v <- Enum.reverse(list) do
115126
to_map2(v, keyfun, transform)
116127
end
117128
end
129+
118130
defp to_map3(_key, v, keyfun, nil) when is_map(v) do
119131
to_map2(v, keyfun, nil)
120132
end
133+
121134
defp to_map3(key, v, keyfun, transform) when is_map(v) and is_function(transform) do
122135
transform.(key, to_map2(v, keyfun, transform))
123136
end
137+
124138
defp to_map3(_key, v, _keyfun, nil), do: v
125139
defp to_map3(key, v, _keyfun, transform), do: transform.(key, v)
126-
140+
127141
# Convert the value of `:keys` to a key conversion function (if not already one)
128142
defp to_key_fun(:atoms), do: &to_atom/1
129143
defp to_key_fun(:atoms!), do: &to_existing_atom/1
130144
defp to_key_fun(:strings), do: nil
131145
defp to_key_fun(fun) when is_function(fun, 1), do: fun
132-
146+
133147
# Convert the given key (as binary) to an atom
134148
# Handle converting uppercase keys to module names rather than plain atoms
135149
defp to_atom(<<c::utf8, _::binary>> = key) when c >= ?A and c <= ?Z do
136150
Module.concat([key])
137151
end
152+
138153
defp to_atom(key), do: String.to_atom(key)
139-
154+
140155
# Convert the given key (as binary) to an existing atom
141156
# Handle converting uppercase keys to module names rather than plain atoms
142157
#
@@ -146,12 +161,13 @@ defmodule Toml.Document do
146161
Module.concat([String.to_existing_atom(key)])
147162
rescue
148163
_ ->
149-
throw {:error, {:keys, {:non_existing_atom, key}}}
164+
throw({:error, {:keys, {:non_existing_atom, key}}})
150165
end
166+
151167
defp to_existing_atom(key) do
152168
String.to_existing_atom(key)
153169
rescue
154170
_ ->
155-
throw {:error, {:keys, {:non_existing_atom, key}}}
171+
throw({:error, {:keys, {:non_existing_atom, key}}})
156172
end
157173
end

lib/error.ex

+43-15
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,125 @@
11
defmodule Toml.Error do
22
@moduledoc false
3-
3+
44
defexception [:message, :reason]
5-
5+
66
def exception(msg) when is_binary(msg) do
77
%__MODULE__{message: msg, reason: nil}
88
end
9+
910
def exception({:error, reason}) do
1011
%__MODULE__{message: format_reason(reason), reason: reason}
1112
end
13+
1214
def exception(reason) when is_tuple(reason) do
1315
%__MODULE__{message: format_reason(reason), reason: reason}
1416
end
1517

1618
def message(%__MODULE__{message: message}) do
1719
message
1820
end
19-
21+
2022
@doc """
2123
Convert internal error reasons into friendly, printable form
2224
"""
2325
def format_reason({:keys, {:non_existing_atom, key}}),
2426
do: "unable to convert '#{key}' to an existing atom"
27+
2528
def format_reason({:expected, token, other}),
2629
do: "expected #{format_token(token)}, but got #{format_token(other)}"
30+
2731
def format_reason({:invalid_token, token}),
2832
do: "invalid token #{format_token(token)}"
33+
2934
def format_reason({:invalid_integer, :leading_zero}),
3035
do: "invalid integer syntax, leading zeros are not allowed"
36+
3137
def format_reason({:invalid_float, :leading_zero}),
3238
do: "invalid float syntax, leading zeros are not allowed"
39+
3340
def format_reason({:invalid_float, token}),
3441
do: "invalid float syntax, unexpected token #{format_token(token)}"
42+
3543
def format_reason({:invalid_datetime, reason}),
36-
do: "invalid datetime syntax, #{inspect reason}"
44+
do: "invalid datetime syntax, #{inspect(reason)}"
45+
3746
def format_reason({:invalid_datetime_offset, reason}),
38-
do: "invalid datetime offset syntax, #{inspect reason}"
47+
do: "invalid datetime offset syntax, #{inspect(reason)}"
48+
3949
def format_reason({:invalid_date, reason}),
40-
do: "invalid date syntax, #{inspect reason}"
50+
do: "invalid date syntax, #{inspect(reason)}"
51+
4152
def format_reason({:invalid_time, reason}),
42-
do: "invalid time syntax, #{inspect reason}"
53+
do: "invalid time syntax, #{inspect(reason)}"
54+
4355
def format_reason({:invalid_key_value, token}),
4456
do: "invalid key/value syntax, unexpected token #{format_token(token)}"
57+
4558
def format_reason({:unclosed_table_array_name, token}),
4659
do: "unclosed table array name at #{format_token(token)}"
60+
4761
def format_reason({:unclosed_array, {oline, ocol}}),
4862
do: "unclosed array started on line #{oline}, column #{ocol}"
63+
4964
def format_reason({:unclosed_inline_table, {oline, ocol}}),
5065
do: "unclosed inline table started on line #{oline}, column #{ocol}"
66+
5167
def format_reason(:unclosed_quote),
5268
do: "unclosed quoted string"
69+
5370
def format_reason(:unexpected_newline),
5471
do: "unexpected newline in single-quoted string"
72+
5573
def format_reason(:unexpected_eof),
5674
do: "unexpected end of file"
75+
5776
def format_reason({:invalid_escape, char}),
5877
do: "illegal escape sequence #{char}"
78+
5979
def format_reason({:invalid_control_char, char}) do
6080
if String.printable?(char) do
61-
"illegal control character #{inspect char, base: :hex} ('#{char}')"
81+
"illegal control character #{inspect(char, base: :hex)} ('#{char}')"
6282
else
63-
"illegal control character #{inspect char, base: :hex}"
83+
"illegal control character #{inspect(char, base: :hex)}"
6484
end
6585
end
86+
6687
def format_reason({:invalid_char, char}) do
6788
if String.printable?(char) do
68-
"illegal character #{inspect char, base: :hex} ('#{char}')"
89+
"illegal character #{inspect(char, base: :hex)} ('#{char}')"
6990
else
70-
"illegal character #{inspect char, base: :hex}"
91+
"illegal character #{inspect(char, base: :hex)}"
7192
end
7293
end
94+
7395
def format_reason({:invalid_unicode, char}) do
7496
if String.printable?(char) do
75-
"illegal unicode escape, #{inspect char, base: :hex} ('#{char}')"
97+
"illegal unicode escape, #{inspect(char, base: :hex)} ('#{char}')"
7698
else
77-
"illegal unicode escape, #{inspect char, base: :hex}"
99+
"illegal unicode escape, #{inspect(char, base: :hex)}"
78100
end
79101
end
102+
80103
def format_reason({:key_exists, key}) do
81104
"cannot redefine key in path '#{key}'"
82105
end
83-
def format_reason(reason), do: "#{inspect reason}"
84-
106+
107+
def format_reason(reason), do: "#{inspect(reason)}"
108+
85109
# Format a token in `{type, data}`, or `type` form into printable representation
86110
defp format_token({true, _}), do: "'true'"
87111
defp format_token({false, _}), do: "'false'"
88112
defp format_token(:newline), do: "'\\n'"
113+
89114
defp format_token({_, data}) when is_binary(data),
90115
do: "'#{data}'"
116+
91117
defp format_token({token, data}) when is_atom(token),
92118
do: "'#{data}' (#{token})"
119+
93120
defp format_token({token, _}),
94121
do: "'#{<<token>>}'"
122+
95123
defp format_token(token),
96124
do: "'#{<<token>>}'"
97125
end

0 commit comments

Comments
 (0)