Skip to content

Commit e9659ed

Browse files
gamachemichalmuskala
authored andcommitted
applied mix format to formatter and formatter_test
1 parent 2438992 commit e9659ed

File tree

2 files changed

+99
-75
lines changed

2 files changed

+99
-75
lines changed

lib/formatter.ex

Lines changed: 98 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ defmodule Jason.Formatter do
1010
"""
1111

1212
@type opts :: [
13-
{:indent, iodata} |
14-
{:line_separator, iodata} |
15-
{:record_separator, iodata} |
16-
{:after_colon, iodata}
17-
]
18-
13+
{:indent, iodata}
14+
| {:line_separator, iodata}
15+
| {:record_separator, iodata}
16+
| {:after_colon, iodata}
17+
]
1918

2019
@doc ~S"""
2120
Returns a binary containing a pretty-printed representation of
@@ -51,10 +50,9 @@ defmodule Jason.Formatter do
5150
@spec pretty_print(iodata, opts) :: binary
5251
def pretty_print(iodata, opts \\ []) do
5352
pretty_print_to_iodata(iodata, opts)
54-
|> IO.iodata_to_binary
53+
|> IO.iodata_to_binary()
5554
end
5655

57-
5856
@doc ~S"""
5957
Returns an iolist containing a pretty-printed representation of
6058
JSON-encoded `iodata`.
@@ -70,13 +68,11 @@ defmodule Jason.Formatter do
7068
first = true
7169
opts = normalize_opts(opts)
7270

73-
{output, _state} =
74-
pp_iodata(iodata, [], depth, in_str, in_bs, empty, first, opts)
71+
{output, _state} = pp_iodata(iodata, [], depth, in_str, in_bs, empty, first, opts)
7572

7673
output
7774
end
7875

79-
8076
@doc ~S"""
8177
Returns a binary containing a minimized representation of
8278
JSON-encoded `iodata`.
@@ -96,10 +92,9 @@ defmodule Jason.Formatter do
9692
@spec minimize(iodata, opts) :: binary
9793
def minimize(iodata, opts \\ []) do
9894
minimize_to_iodata(iodata, opts)
99-
|> IO.iodata_to_binary
95+
|> IO.iodata_to_binary()
10096
end
10197

102-
10398
@doc ~S"""
10499
Returns an iolist containing a minimized representation of
105100
JSON-encoded `iodata`.
@@ -117,48 +112,57 @@ defmodule Jason.Formatter do
117112
)
118113
end
119114

120-
121115
## Returns a copy of `opts` with defaults applied
122116
@spec normalize_opts(keyword) :: opts
123117
defp normalize_opts(opts) do
124118
[
125119
indent: opts[:indent] || " ",
126120
line_separator: opts[:line_separator] || "\n",
127121
record_separator: opts[:record_separator] || opts[:line_separator] || "\n",
128-
after_colon: opts[:after_colon] || " ",
122+
after_colon: opts[:after_colon] || " "
129123
]
130124
end
131125

132-
133126
## Returns an iolist containing `depth` instances of `opts[:indent]`
134127
@spec tab(opts, non_neg_integer, iolist) :: iolist
135128
defp tab(opts, depth, output \\ []) do
136129
if depth < 1 do
137130
output
138131
else
139-
tab(opts, depth-1, [opts[:indent] | output])
132+
tab(opts, depth - 1, [opts[:indent] | output])
140133
end
141134
end
142135

143-
144136
@typep pp_state :: {
145-
non_neg_integer, ## depth -- current nesting depth
146-
boolean, ## in_str -- is the current byte in a string?
147-
boolean, ## in_bs -- does the current byte follow a backslash in a string?
148-
boolean, ## empty -- is the current object or array empty?
149-
boolean, ## first -- is this the first object or array in the input?
150-
}
137+
## depth -- current nesting depth
138+
non_neg_integer,
139+
## in_str -- is the current byte in a string?
140+
boolean,
141+
## in_bs -- does the current byte follow a backslash in a string?
142+
boolean,
143+
## empty -- is the current object or array empty?
144+
boolean,
145+
## first -- is this the first object or array in the input?
146+
boolean
147+
}
151148

152149
@spec pp_iodata(
153-
iodata, ## input -- input data
154-
iodata, ## output_acc -- output iolist (built in reverse order)
155-
non_neg_integer, ## depth -- current nesting depth
156-
boolean, ## in_str -- is the current byte in a string?
157-
boolean, ## in_bs -- does the current byte follow a backslash in a string?
158-
boolean, ## empty -- is the current object or array empty?
159-
boolean, ## first -- is this the first object or array in the input?
160-
opts
161-
) :: {iodata, pp_state}
150+
## input -- input data
151+
iodata,
152+
## output_acc -- output iolist (built in reverse order)
153+
iodata,
154+
## depth -- current nesting depth
155+
non_neg_integer,
156+
## in_str -- is the current byte in a string?
157+
boolean,
158+
## in_bs -- does the current byte follow a backslash in a string?
159+
boolean,
160+
## empty -- is the current object or array empty?
161+
boolean,
162+
## first -- is this the first object or array in the input?
163+
boolean,
164+
opts
165+
) :: {iodata, pp_state}
162166
defp pp_iodata(input, output_acc, depth, in_str, in_bs, empty, first, opts)
163167

164168
defp pp_iodata("", output_acc, depth, in_str, in_bs, empty, first, opts) do
@@ -169,111 +173,133 @@ defmodule Jason.Formatter do
169173
{output_acc, {depth, in_str, in_bs, empty, first, opts}}
170174
end
171175

172-
defp pp_iodata(<<byte::size(8), rest::binary>>, output_acc, depth, in_str, in_bs, empty, first, opts) do
176+
defp pp_iodata(
177+
<<byte::size(8), rest::binary>>,
178+
output_acc,
179+
depth,
180+
in_str,
181+
in_bs,
182+
empty,
183+
first,
184+
opts
185+
) do
173186
pp_byte(byte, rest, output_acc, depth, in_str, in_bs, empty, first, opts)
174187
end
175188

176-
defp pp_iodata(byte, output_acc, depth, in_str, in_bs, empty, first, opts) when is_integer(byte) do
189+
defp pp_iodata(byte, output_acc, depth, in_str, in_bs, empty, first, opts)
190+
when is_integer(byte) do
177191
pp_byte(byte, [], output_acc, depth, in_str, in_bs, empty, first, opts)
178192
end
179193

180194
defp pp_iodata(list, output_acc, depth, in_str, in_bs, empty, first, opts) when is_list(list) do
181195
starting_state = {depth, in_str, in_bs, empty, first, opts}
182-
{reversed_output, end_state} = Enum.reduce list, {[], starting_state}, fn (item, {output_acc, state}) ->
183-
{depth, in_str, in_bs, empty, first, opts} = state
184-
{item_output, new_state} = pp_iodata(item, [], depth, in_str, in_bs, empty, first, opts)
185-
{[output_acc, item_output], new_state}
186-
end
187-
{[output_acc, reversed_output], end_state}
188-
end
189196

197+
{list_output, end_state} =
198+
Enum.reduce(list, {[], starting_state}, fn item, {output_acc, state} ->
199+
{depth, in_str, in_bs, empty, first, opts} = state
200+
{item_output, new_state} = pp_iodata(item, [], depth, in_str, in_bs, empty, first, opts)
201+
{[output_acc, item_output], new_state}
202+
end)
203+
204+
{[output_acc, list_output], end_state}
205+
end
190206

191207
@spec pp_byte(
192-
byte, ## byte -- current byte
193-
iodata, ## rest -- rest of input data
194-
iodata, ## output -- output iolist (built in reverse order)
195-
non_neg_integer, ## depth -- current nesting depth
196-
boolean, ## in_str -- is the current byte in a string?
197-
boolean, ## in_bs -- does the current byte follow a backslash in a string?
198-
boolean, ## empty -- is the current object or array empty?
199-
boolean, ## first -- is this the first object or array in the input?
200-
opts
201-
) :: {iodata, pp_state}
208+
## byte -- current byte
209+
byte,
210+
## rest -- rest of input data
211+
iodata,
212+
## output -- output iolist (built in reverse order)
213+
iodata,
214+
## depth -- current nesting depth
215+
non_neg_integer,
216+
## in_str -- is the current byte in a string?
217+
boolean,
218+
## in_bs -- does the current byte follow a backslash in a string?
219+
boolean,
220+
## empty -- is the current object or array empty?
221+
boolean,
222+
## first -- is this the first object or array in the input?
223+
boolean,
224+
opts
225+
) :: {iodata, pp_state}
202226
defp pp_byte(byte, rest, output, depth, in_str, in_bs, empty, first, opts)
203227

204228
## in string, following backslash
205-
defp pp_byte(byte, rest, output, depth, true=in_str, true=_in_bs, empty, first, opts) do
229+
defp pp_byte(byte, rest, output, depth, true = in_str, true = _in_bs, empty, first, opts) do
206230
in_bs = false
207231
pp_iodata(rest, [output, byte], depth, in_str, in_bs, empty, first, opts)
208232
end
209233

210234
## in string, backslash
211-
defp pp_byte(byte, rest, output, depth, true=in_str, _in_bs, empty, first, opts)
212-
when byte in '\\' do
235+
defp pp_byte(byte, rest, output, depth, true = in_str, _in_bs, empty, first, opts)
236+
when byte in '\\' do
213237
in_bs = true
214238
pp_iodata(rest, [output, byte], depth, in_str, in_bs, empty, first, opts)
215239
end
216240

217241
## in string, end quote
218-
defp pp_byte(byte, rest, output, depth, true=_in_str, in_bs, empty, first, opts)
219-
when byte in '"' do
242+
defp pp_byte(byte, rest, output, depth, true = _in_str, in_bs, empty, first, opts)
243+
when byte in '"' do
220244
in_str = false
221245
pp_iodata(rest, [output, byte], depth, in_str, in_bs, empty, first, opts)
222246
end
223247

224248
## in string, other character
225-
defp pp_byte(byte, rest, output, depth, true=in_str, in_bs, empty, first, opts) do
249+
defp pp_byte(byte, rest, output, depth, true = in_str, in_bs, empty, first, opts) do
226250
pp_iodata(rest, [output, byte], depth, in_str, in_bs, empty, first, opts)
227251
end
228252

229253
## out of string, whitespace
230254
defp pp_byte(byte, rest, output, depth, in_str, in_bs, empty, first, opts)
231-
when byte in ' \n\r\t' do
255+
when byte in ' \n\r\t' do
232256
pp_iodata(rest, output, depth, in_str, in_bs, empty, first, opts)
233257
end
234258

235259
## out of string, start block
236260
defp pp_byte(byte, rest, output, depth, in_str, in_bs, empty, first, opts)
237-
when byte in '{[' do
238-
out = cond do
239-
first -> byte
240-
empty -> [opts[:line_separator], tab(opts, depth), byte]
241-
depth == 0 -> [opts[:record_separator], byte]
242-
true -> byte
243-
end
261+
when byte in '{[' do
262+
out =
263+
cond do
264+
first -> byte
265+
empty -> [opts[:line_separator], tab(opts, depth), byte]
266+
depth == 0 -> [opts[:record_separator], byte]
267+
true -> byte
268+
end
269+
244270
first = false
245271
empty = true
246272
depth = depth + 1
247273
pp_iodata(rest, [output, out], depth, in_str, in_bs, empty, first, opts)
248274
end
249275

250276
## out of string, end empty block
251-
defp pp_byte(byte, rest, output, depth, in_str, in_bs, true=_empty, first, opts)
252-
when byte in '}]' do
277+
defp pp_byte(byte, rest, output, depth, in_str, in_bs, true = _empty, first, opts)
278+
when byte in '}]' do
253279
empty = false
254280
depth = depth - 1
255281
pp_iodata(rest, [output, byte], depth, in_str, in_bs, empty, first, opts)
256282
end
257283

258284
## out of string, end non-empty block
259-
defp pp_byte(byte, rest, output, depth, in_str, in_bs, false=empty, first, opts)
260-
when byte in '}]' do
285+
defp pp_byte(byte, rest, output, depth, in_str, in_bs, false = empty, first, opts)
286+
when byte in '}]' do
261287
depth = depth - 1
262288
out = [opts[:line_separator], tab(opts, depth), byte]
263289
pp_iodata(rest, [output, out], depth, in_str, in_bs, empty, first, opts)
264290
end
265291

266292
## out of string, comma
267293
defp pp_byte(byte, rest, output, depth, in_str, in_bs, _empty, first, opts)
268-
when byte in ',' do
294+
when byte in ',' do
269295
empty = false
270296
out = [byte, opts[:line_separator], tab(opts, depth)]
271297
pp_iodata(rest, [output, out], depth, in_str, in_bs, empty, first, opts)
272298
end
273299

274300
## out of string, colon
275301
defp pp_byte(byte, rest, output, depth, in_str, in_bs, empty, first, opts)
276-
when byte in ':' do
302+
when byte in ':' do
277303
out = [byte, opts[:after_colon]]
278304
pp_iodata(rest, [output, out], depth, in_str, in_bs, empty, first, opts)
279305
end
@@ -286,4 +312,3 @@ defmodule Jason.Formatter do
286312
pp_iodata(rest, [output, out], depth, in_str, in_bs, empty, first, opts)
287313
end
288314
end
289-

test/formatter_test.exs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ defmodule Jason.FormatterTest do
1010
"simple-object",
1111
"multiple-objects",
1212
"backslash-string",
13-
"empty-nest",
13+
"empty-nest"
1414
]
1515

1616
for name <- @test_cases do
@@ -65,4 +65,3 @@ defmodule Jason.FormatterTest do
6565
assert(pretty_print(input, indent: "\t") == output)
6666
end
6767
end
68-

0 commit comments

Comments
 (0)