Skip to content

Commit 8588595

Browse files
authored
Add formatter and appease the lint gods (#9)
* Add formatter to lint task * Apply formatting with mix format * Bump version to 0.2.5
1 parent ba2210a commit 8588595

37 files changed

+640
-330
lines changed

.formatter.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Used by "mix format"
2+
[
3+
inputs: [
4+
"{mix,.formatter}.exs",
5+
"{lib,test,bench}/**/*.{ex,exs}"
6+
],
7+
line_length: 88
8+
]

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.2.4
1+
0.2.5

lib/ex_image_info.ex

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,18 @@ defmodule ExImageInfo do
7575
# but still keeping :png as the first
7676
@types [:png, :jpeg, :gif, :bmp, :ico, :tiff, :webp, :psd, :jp2, :pnm]
7777

78-
@type image_format :: :png | :jpeg | :gif | :bmp | :ico | :tiff | :webp | :psd | :jp2 | :pnm
78+
@typedoc "The supported image formats"
79+
@type image_format ::
80+
:png
81+
| :jpeg
82+
| :gif
83+
| :bmp
84+
| :ico
85+
| :tiff
86+
| :webp
87+
| :psd
88+
| :jp2
89+
| :pnm
7990

8091
## Public API
8192

@@ -176,7 +187,8 @@ defmodule ExImageInfo do
176187
maybe_png_binary |> ExImageInfo.type :png
177188
# nil
178189
"""
179-
@spec type(binary, format :: atom) :: {mimetype :: String.t, variant :: String.t} | nil
190+
@spec type(binary, format :: atom) ::
191+
{mimetype :: String.t(), variant :: String.t()} | nil
180192
def type(binary, format)
181193
def type(binary, :png), do: PNG.type(binary)
182194
def type(binary, :gif), do: GIF.type(binary)
@@ -213,7 +225,7 @@ defmodule ExImageInfo do
213225
webp_full_binary |> ExImageInfo.type
214226
# {"image/webp", "webpVP8"}
215227
"""
216-
@spec type(binary) :: {mimetype :: String.t, variant :: String.t} | nil
228+
@spec type(binary) :: {mimetype :: String.t(), variant :: String.t()} | nil
217229
def type(binary), do: try_type(binary, @types)
218230

219231
@doc """
@@ -245,7 +257,9 @@ defmodule ExImageInfo do
245257
# nil
246258
"""
247259
@spec info(binary, format :: atom) ::
248-
{mimetype :: String.t, width :: Integer.t, height :: Integer.t, variant :: String.t} | nil
260+
{mimetype :: String.t(), width :: Integer.t(), height :: Integer.t(),
261+
variant :: String.t()}
262+
| nil
249263
def info(binary, format)
250264
def info(binary, :png), do: PNG.info(binary)
251265
def info(binary, :gif), do: GIF.info(binary)
@@ -282,19 +296,24 @@ defmodule ExImageInfo do
282296
webp_full_binary |> ExImageInfo.info
283297
# {"image/webp", 20, 100, "webpVP8"}
284298
"""
285-
@spec info(binary) :: {mimetype :: String.t, width :: Integer.t, height :: Integer.t, variant :: String.t} | nil
299+
@spec info(binary) ::
300+
{mimetype :: String.t(), width :: Integer.t(), height :: Integer.t(),
301+
variant :: String.t()}
302+
| nil
286303
def info(binary), do: try_info(binary, @types)
287304

288305
## Private
289306

290307
@doc false
291308
defp try_seems?(_binary, []), do: nil
309+
292310
defp try_seems?(binary, [type | types]) do
293311
if seems?(binary, type), do: type, else: try_seems?(binary, types)
294312
end
295313

296314
@doc false
297315
defp try_type(_binary, []), do: nil
316+
298317
defp try_type(binary, [type | types]) do
299318
case type(binary, type) do
300319
type_t when is_tuple(type_t) -> type_t
@@ -304,11 +323,11 @@ defmodule ExImageInfo do
304323

305324
@doc false
306325
defp try_info(_binary, []), do: nil
326+
307327
defp try_info(binary, [type | types]) do
308328
case info(binary, type) do
309329
info_t when is_tuple(info_t) -> info_t
310330
_ -> try_info(binary, types)
311331
end
312332
end
313-
314333
end

lib/ex_image_info/detector.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
defmodule ExImageInfo.Detector do
22
@moduledoc false
33

4-
@callback info(binary) :: {mimetype :: String.t, width :: Integer.t, height :: Integer.t, variant :: String.t} | nil
5-
@callback type(binary) :: {mimetype :: String.t, variant :: String.t} | nil
4+
@callback info(binary) ::
5+
{mimetype :: String.t(), width :: Integer.t(), height :: Integer.t(),
6+
variant :: String.t()}
7+
| nil
8+
@callback type(binary) :: {mimetype :: String.t(), variant :: String.t()} | nil
69
@callback seems?(binary) :: boolean()
710
end

lib/ex_image_info/types/bmp.ex

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
defmodule ExImageInfo.Types.BMP do
2-
32
@moduledoc false
43

54
@behaviour ExImageInfo.Detector
@@ -11,13 +10,17 @@ defmodule ExImageInfo.Types.BMP do
1110

1211
## Public API
1312

14-
def seems?(<< @signature, _rest::binary >>), do: true
13+
def seems?(<<@signature, _rest::binary>>), do: true
1514
def seems?(_), do: false
1615

17-
def info(<< @signature, _::bytes-size(16), width::little-size(16), _::bytes-size(2), height::little-size(16), _rest::binary >>), do: {@mime, width, height, @ftype}
16+
def info(
17+
<<@signature, _::bytes-size(16), width::little-size(16), _::bytes-size(2),
18+
height::little-size(16), _rest::binary>>
19+
),
20+
do: {@mime, width, height, @ftype}
21+
1822
def info(_), do: nil
1923

20-
def type(<< @signature, _rest::binary >>), do: {@mime, @ftype}
24+
def type(<<@signature, _rest::binary>>), do: {@mime, @ftype}
2125
def type(_), do: nil
22-
2326
end

lib/ex_image_info/types/gif.ex

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
defmodule ExImageInfo.Types.GIF do
2-
32
@moduledoc false
43

54
@behaviour ExImageInfo.Detector
@@ -11,16 +10,25 @@ defmodule ExImageInfo.Types.GIF do
1110
@signature_87a <<"GIF87a">>
1211
@signature_89a <<"GIF89a">>
1312

14-
def seems?(<< @signature_87a, _rest::binary >>), do: true
15-
def seems?(<< @signature_89a, _rest::binary >>), do: true
13+
def seems?(<<@signature_87a, _rest::binary>>), do: true
14+
def seems?(<<@signature_89a, _rest::binary>>), do: true
1615
def seems?(_), do: false
1716

18-
def info(<< @signature_87a, width::little-size(16), height::little-size(16), _rest::binary >>), do: {@mime, width, height, @ftype87}
19-
def info(<< @signature_89a, width::little-size(16), height::little-size(16), _rest::binary >>), do: {@mime, width, height, @ftype89}
17+
def info(
18+
<<@signature_87a, width::little-size(16), height::little-size(16),
19+
_rest::binary>>
20+
),
21+
do: {@mime, width, height, @ftype87}
22+
23+
def info(
24+
<<@signature_89a, width::little-size(16), height::little-size(16),
25+
_rest::binary>>
26+
),
27+
do: {@mime, width, height, @ftype89}
28+
2029
def info(_), do: nil
2130

22-
def type(<< @signature_89a, _rest::binary >>), do: {@mime, @ftype89}
23-
def type(<< @signature_87a, _rest::binary >>), do: {@mime, @ftype87}
31+
def type(<<@signature_89a, _rest::binary>>), do: {@mime, @ftype89}
32+
def type(<<@signature_87a, _rest::binary>>), do: {@mime, @ftype87}
2433
def type(_), do: nil
25-
2634
end

lib/ex_image_info/types/ico.ex

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
defmodule ExImageInfo.Types.ICO do
2-
32
@moduledoc false
43

54
@behaviour ExImageInfo.Detector
@@ -8,39 +7,47 @@ defmodule ExImageInfo.Types.ICO do
87
@mime "image/x-icon"
98
@ftype "ICO"
109

11-
@signature << 0::size(16), 0x01, 0x00 >> # 0x0100 -> little endian -> 1 (.ICO), 2 (.CUR)
10+
# 0x0100 -> little endian -> 1 (.ICO), 2 (.CUR)
11+
@signature <<0::size(16), 0x01, 0x00>>
1212

1313
## Public API
1414

15-
def seems?(<< @signature, _rest::binary >>), do: true
15+
def seems?(<<@signature, _rest::binary>>), do: true
1616
def seems?(_), do: false
1717

18-
def info(<< @signature, num_images::little-size(16), rest::binary >>) do
18+
def info(<<@signature, num_images::little-size(16), rest::binary>>) do
1919
case parse(rest, num_images) do
2020
{w, h} -> {@mime, w, h, @ftype}
2121
_ -> nil
2222
end
2323
end
24+
2425
def info(_), do: nil
2526

26-
def type(<< @signature, _rest::binary >>), do: {@mime, @ftype}
27+
def type(<<@signature, _rest::binary>>), do: {@mime, @ftype}
2728
def type(_), do: nil
2829

2930
## Private
3031

3132
defp parse(binary, num), do: parse(binary, num, {0, 0})
32-
defp parse(<< w::size(8), h::size(8), _head::bytes-size(14), rest::binary >>, num, {wp, hp} = acc) do
33+
34+
defp parse(
35+
<<w::size(8), h::size(8), _head::bytes-size(14), rest::binary>>,
36+
num,
37+
{wp, hp} = acc
38+
) do
3339
w = if w == 0, do: 256, else: w
3440
h = if h == 0, do: 256, else: h
3541
sump = wp + hp
3642
sum = w + h
3743
acc = if sum > sump, do: {w, h}, else: acc
38-
if num == 1 do # last one
44+
# last one
45+
if num == 1 do
3946
acc
4047
else
4148
parse(rest, num - 1, acc)
4249
end
4350
end
44-
defp parse(_, _, _), do: nil
4551

52+
defp parse(_, _, _), do: nil
4653
end

lib/ex_image_info/types/jp2.ex

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
defmodule ExImageInfo.Types.JP2 do
2-
32
@moduledoc false
43

54
@behaviour ExImageInfo.Detector
@@ -9,18 +8,22 @@ defmodule ExImageInfo.Types.JP2 do
98
@mime "image/jp2"
109
@ftype "JP2"
1110

12-
@signature << 0::size(24), 0x0c6A5020200D0A870A::size(72) >>
13-
@signature_ihdr << "ihdr" >>
11+
@signature <<0::size(24), 0x0C6A5020200D0A870A::size(72)>>
12+
@signature_ihdr <<"ihdr">>
1413

1514
## Public API
1615

17-
def seems?(<< @signature, _rest::binary >>), do: true
16+
def seems?(<<@signature, _rest::binary>>), do: true
1817
def seems?(_), do: false
1918

20-
def info(<< @signature, _::bytes-size(32), @signature_ihdr, h::size(32), w::size(32), _rest::binary >>), do: {@mime, w, h, @ftype}
19+
def info(
20+
<<@signature, _::bytes-size(32), @signature_ihdr, h::size(32), w::size(32),
21+
_rest::binary>>
22+
),
23+
do: {@mime, w, h, @ftype}
24+
2125
def info(_), do: nil
2226

23-
def type(<< @signature, _rest::binary >>), do: {@mime, @ftype}
27+
def type(<<@signature, _rest::binary>>), do: {@mime, @ftype}
2428
def type(_), do: nil
25-
2629
end

lib/ex_image_info/types/jpeg.ex

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
defmodule ExImageInfo.Types.JPEG do
2-
32
@moduledoc false
43

54
@behaviour ExImageInfo.Detector
@@ -9,14 +8,14 @@ defmodule ExImageInfo.Types.JPEG do
98
@ftype_base "baseJPEG"
109
@ftype_prog "progJPEG"
1110

12-
@signature << 0xFFD8::size(16) >>
11+
@signature <<0xFFD8::size(16)>>
1312

1413
## Public API
1514

16-
def seems?(<< @signature, _rest::binary >>), do: true
15+
def seems?(<<@signature, _rest::binary>>), do: true
1716
def seems?(_), do: false
1817

19-
def info(<< @signature, _::size(16), rest::binary >>), do: parse_jpeg(rest)
18+
def info(<<@signature, _::size(16), rest::binary>>), do: parse_jpeg(rest)
2019
def info(_), do: nil
2120

2221
def type(bin) do
@@ -28,27 +27,37 @@ defmodule ExImageInfo.Types.JPEG do
2827

2928
## Private
3029

31-
defp parse_jpeg(<< block_len::size(16), rest::binary >>) do
30+
defp parse_jpeg(<<block_len::size(16), rest::binary>>) do
3231
parse_jpeg_block(block_len, rest)
3332
end
33+
3434
defp parse_jpeg(_), do: nil
3535

36-
defp parse_jpeg_block(block_len, << rest::binary >>) do
37-
block_len = block_len - 2 # bytes of block_len
36+
defp parse_jpeg_block(block_len, <<rest::binary>>) do
37+
# bytes of block_len
38+
block_len = block_len - 2
39+
3840
case rest do
39-
<< _::bytes-size(block_len), 0xFF, sof::size(8), next::binary >> -> parse_jpeg_sof(sof, next)
40-
_ -> nil
41+
<<_::bytes-size(block_len), 0xFF, sof::size(8), next::binary>> ->
42+
parse_jpeg_sof(sof, next)
43+
44+
_ ->
45+
nil
4146
end
4247
end
48+
4349
defp parse_jpeg_block(_, _), do: nil
4450

4551
defp parse_jpeg_sof(0xC0, next), do: parse_jpeg_dimensions(@ftype_base, next)
4652
defp parse_jpeg_sof(0xC2, next), do: parse_jpeg_dimensions(@ftype_prog, next)
4753
defp parse_jpeg_sof(_, next), do: parse_jpeg(next)
4854

49-
defp parse_jpeg_dimensions(ftype, << _skip::size(24), height::size(16), width::size(16), _::binary >>) do
55+
defp parse_jpeg_dimensions(
56+
ftype,
57+
<<_skip::size(24), height::size(16), width::size(16), _::binary>>
58+
) do
5059
{@mime, width, height, ftype}
5160
end
52-
defp parse_jpeg_dimensions(_, _), do: nil
5361

62+
defp parse_jpeg_dimensions(_, _), do: nil
5463
end

lib/ex_image_info/types/png.ex

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
defmodule ExImageInfo.Types.PNG do
2-
32
@moduledoc false
43

54
@behaviour ExImageInfo.Detector
@@ -12,13 +11,19 @@ defmodule ExImageInfo.Types.PNG do
1211

1312
## Public API
1413

15-
def seems?(<< _::bytes-size(1), @signature, _rest::binary >>), do: true
14+
def seems?(<<_::bytes-size(1), @signature, _rest::binary>>), do: true
1615
def seems?(_), do: false
1716

18-
def info(<< _::bytes-size(1), @signature, _::size(32), @signature_ihdr, width::size(32), height::size(32), _rest::binary >>), do: {@mime, width, height, @ftype}
17+
def info(
18+
<<_::bytes-size(1), @signature, _::size(32), @signature_ihdr, width::size(32),
19+
height::size(32), _rest::binary>>
20+
),
21+
do: {@mime, width, height, @ftype}
22+
1923
def info(_), do: nil
2024

21-
def type(<< _::size(8), @signature, _::size(32), @signature_ihdr, _rest::binary >>), do: {@mime, @ftype}
22-
def type(_), do: nil
25+
def type(<<_::size(8), @signature, _::size(32), @signature_ihdr, _rest::binary>>),
26+
do: {@mime, @ftype}
2327

28+
def type(_), do: nil
2429
end

0 commit comments

Comments
 (0)