Skip to content

Commit d21fd40

Browse files
authored
Merge pull request #47 from boydm/boyd
Typespec and dialyzer fixes
2 parents 09efe85 + 22682f2 commit d21fd40

File tree

10 files changed

+70
-68
lines changed

10 files changed

+70
-68
lines changed

lib/scenic/cache/file.ex

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,7 @@ defmodule Scenic.Cache.File do
8181
defp do_unzip(data, opts) do
8282
case opts[:decompress] do
8383
true ->
84-
case :zlib.gunzip(data) do
85-
bin when is_binary(bin) -> {:ok, bin}
86-
_ -> {:error, :gunzip}
87-
end
84+
{:ok, :zlib.gunzip(data)}
8885

8986
_ ->
9087
# not decompressing

lib/scenic/cache/hash.ex

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,16 @@ defmodule Scenic.Cache.Hash do
3434

3535
# --------------------------------------------------------
3636
def binary(data, hash_type) do
37-
valid_hash_type!(hash_type)
38-
|> :crypto.hash(data)
39-
|> Base.url_encode64(padding: false)
37+
case valid_hash_type?(hash_type) do
38+
true -> {:ok, :crypto.hash(hash_type, data) |> Base.url_encode64(padding: false)}
39+
false -> {:error, :invalid_hash_type}
40+
end
4041
end
4142

4243
def binary!(data, hash_type) do
43-
{:ok, hash} =
44-
valid_hash_type!(hash_type)
45-
|> :crypto.hash(data)
46-
|> Base.url_encode64(padding: false)
47-
48-
hash
44+
valid_hash_type!(hash_type)
45+
|> :crypto.hash(data)
46+
|> Base.url_encode64(padding: false)
4947
end
5048

5149
# --------------------------------------------------------
@@ -99,15 +97,15 @@ defmodule Scenic.Cache.Hash do
9997

10098
# --------------------------------------------------------
10199
def verify(data, hash, hash_type) do
102-
case binary(data, hash_type) == hash do
103-
true -> {:ok, data}
104-
false -> {:error, :hash_failure}
100+
case binary(data, hash_type) do
101+
{:ok, ^hash} -> {:ok, data}
102+
_ -> {:error, :hash_failure}
105103
end
106104
end
107105

108106
# --------------------------------------------------------
109107
def verify!(data, hash, hash_type) do
110-
case binary(data, hash_type) == hash do
108+
case binary!(data, hash_type) == hash do
111109
true -> data
112110
false -> raise Error
113111
end

lib/scenic/cache/term.ex

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ defmodule Scenic.Cache.Term do
1919
# hashes. Is also slower because it has to load the file and compute the hash
2020
# to use as a key even it is is already loaded into the cache.
2121
def load(path, :insecure, opts) do
22-
with {:ok, data} <- Cache.File.read(path, :insecure, opts) do
23-
hash = Hash.binary(data, opts[:hash] || :sha)
24-
22+
with {:ok, data} <- Cache.File.read(path, :insecure, opts),
23+
{:ok, hash} <- Hash.binary(data, opts[:hash] || :sha) do
2524
case Cache.claim(hash, opts[:scope]) do
2625
true ->
2726
{:ok, hash}

lib/scenic/component/input/radio_button.ex

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ defmodule Scenic.Component.Input.RadioButton do
1919
end
2020

2121
# --------------------------------------------------------
22-
def verify({text, _}) when is_bitstring(text), do: {:ok, {text, false}}
22+
def verify({text, _} = data) when is_bitstring(text) do
23+
{:ok, data}
24+
end
2325

24-
def verify({text, _, checked?} = data)
25-
when is_bitstring(text) and is_boolean(checked?) do
26+
def verify({text, _, checked?} = data) when is_bitstring(text) and is_boolean(checked?) do
2627
{:ok, data}
2728
end
2829

30+
def verify(_), do: :invalid_data
31+
2932
# --------------------------------------------------------
3033
def init({text, id}, opts) when is_bitstring(text), do: init({text, id, false}, opts)
3134

lib/scenic/math.ex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,14 @@ defmodule Scenic.Math do
1515
"""
1616

1717
@type vector_2 :: {x :: number, y :: number}
18-
# @type vector_3 :: {x :: number, y :: number, z :: number}
19-
# @type vector_4 :: {x :: number, y :: number, z :: number, w :: number}
2018

2119
@type point :: {x :: number, y :: number}
2220

2321
@type line :: {p0 :: point, p1 :: point}
2422
@type triangle :: {p0 :: point, p1 :: point, p2 :: point}
2523
@type quad :: {p0 :: point, p1 :: point, p2 :: point, p3 :: point}
2624

27-
@type matrix :: <<_::64>>
25+
@type matrix :: binary()
2826

2927
@type matrix_list :: list(number)
3028
end

lib/scenic/math/matrix.ex

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,10 @@ defmodule Scenic.Math.Matrix do
199199
"""
200200
@spec build_translation(vector_2 :: Math.vector_2()) :: Math.matrix()
201201
def build_translation(vector_2)
202-
def build_translation({x, y}), do: build_translation({x, y, 0.0})
202+
def build_translation({x, y}), do: do_build_translation({x, y, 0.0})
203203
# def build_translation({x, y, z}), do: build_translation(x, y, z)
204204
# def build_translation(x, y), do: build_translation(x, y, 0.0)
205-
def build_translation({x, y, z}) do
205+
defp do_build_translation({x, y, z}) do
206206
<<
207207
1.0::float-size(32)-native,
208208
0.0::float-size(32)-native,
@@ -235,11 +235,11 @@ defmodule Scenic.Math.Matrix do
235235
"""
236236
@spec build_scale(scale :: number | Math.vector_2()) :: Math.matrix()
237237
def build_scale(scale)
238-
def build_scale(s) when is_number(s), do: build_scale({s, s, s})
239-
def build_scale({x, y}), do: build_scale({x, y, 1.0})
238+
def build_scale(s) when is_number(s), do: do_build_scale({s, s, s})
239+
def build_scale({x, y}), do: do_build_scale({x, y, 1.0})
240240
# def build_scale({x, y, z}), do: build_scale(x, y, z)
241241
# def build_scale(x, y), do: build_scale(x, y, 1.0)
242-
def build_scale({x, y, z}) do
242+
defp do_build_scale({x, y, z}) do
243243
<<
244244
x * 1.0::float-size(32)-native,
245245
0.0::float-size(32)-native,
@@ -394,13 +394,15 @@ defmodule Scenic.Math.Matrix do
394394
Returns:
395395
A binary matrix
396396
"""
397-
@spec rotate(matrix :: number, pin :: Math.point() | nil) :: Math.matrix()
397+
@spec rotate(matrix :: Math.matrix(), angle :: number | nil) :: Math.matrix()
398398
def rotate(matrix, angle)
399399
def rotate(matrix, nil), do: matrix
400400

401-
def rotate(matrix, amount) do
402-
build_rotation(amount)
403-
|> (&Matrix.mul(matrix, &1)).()
401+
def rotate(matrix, angle) do
402+
Matrix.mul(
403+
matrix,
404+
build_rotation(angle)
405+
)
404406
end
405407

406408
# def rotate( matrix, radians, axis ) when is_atom(axis) do
@@ -419,13 +421,15 @@ defmodule Scenic.Math.Matrix do
419421
Returns:
420422
A binary matrix
421423
"""
422-
@spec translate(matrix :: number, vector_2 :: Math.vector_2() | nil) :: Math.matrix()
424+
@spec translate(matrix :: Math.matrix(), vector_2 :: Math.vector_2() | nil) :: Math.matrix()
423425
def translate(matrix, vector_2)
424426
def translate(matrix, nil), do: matrix
425427

426428
def translate(matrix, {x, y}) do
427-
build_translation({x, y})
428-
|> (&Matrix.mul(matrix, &1)).()
429+
Matrix.mul(
430+
matrix,
431+
build_translation({x, y})
432+
)
429433
end
430434

431435
# def translate(matrix, {x, y, z}), do: translate(matrix, x, y, z)
@@ -444,12 +448,18 @@ defmodule Scenic.Math.Matrix do
444448
Returns:
445449
A binary matrix
446450
"""
447-
@spec scale(matrix :: number, scale :: number | Math.vector_2() | nil) :: Math.matrix()
451+
@spec scale(matrix :: Math.matrix(), scale :: number | Math.vector_2() | nil) :: Math.matrix()
448452
def scale(matrix, scale)
449453
def scale(matrix, nil), do: matrix
450454
# def scale(matrix, {x, y}), do: scale(matrix, {x, y})
451455
# def scale(matrix, {x, y, z}), do: scale(matrix,{ x, y, z})
452-
def scale(matrix, s), do: build_scale(s) |> (&Matrix.mul(matrix, &1)).()
456+
def scale(matrix, s) do
457+
Matrix.mul(
458+
matrix,
459+
build_scale(s)
460+
)
461+
end
462+
453463
# def scale(matrix, x, y), do: build_scale(x, y) |> (&Matrix.mul(matrix, &1)).()
454464
# def scale(matrix, x, y, z), do: build_scale(x, y, z) |> (&Matrix.mul(matrix, &1)).()
455465

@@ -468,7 +478,7 @@ defmodule Scenic.Math.Matrix do
468478
Returns:
469479
A number
470480
"""
471-
@spec get(matrix :: number, x :: number, y :: number) :: number
481+
@spec get(matrix :: Math.matrix(), x :: number, y :: number) :: number
472482
def get(matrix, x, y)
473483

474484
def get(matrix, x, y)
@@ -497,7 +507,7 @@ defmodule Scenic.Math.Matrix do
497507
Returns:
498508
A number
499509
"""
500-
@spec put(matrix :: number, x :: number, y :: number, v :: number) :: Math.matrix()
510+
@spec put(matrix :: Math.matrix(), x :: number, y :: number, v :: number) :: Math.matrix()
501511
def put(matrix, x, y, v)
502512
def put(matrix, x, y, v) when is_integer(v), do: put(matrix, x, y, v * 1.0)
503513

@@ -529,7 +539,7 @@ defmodule Scenic.Math.Matrix do
529539
Returns:
530540
A vector_2
531541
"""
532-
@spec get_xy(matrix :: number) :: Math.vector_2()
542+
@spec get_xy(matrix :: Math.matrix()) :: Math.vector_2()
533543
def get_xy(matrix)
534544

535545
def get_xy(<<
@@ -693,7 +703,7 @@ defmodule Scenic.Math.Matrix do
693703
Returns:
694704
The resulting matrix
695705
"""
696-
@spec mul(matrix :: Math.matrix(), divisor :: number) :: Math.matrix()
706+
@spec div(matrix :: Math.matrix(), divisor :: number) :: Math.matrix()
697707
def div(matrix, scalar)
698708
def div(a, s) when is_integer(s), do: Matrix.div(a, s * 1.0)
699709

lib/scenic/math/vector_2.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ defmodule Scenic.Math.Vector2 do
1212
{3.5, 4.7}
1313
"""
1414

15+
alias Scenic.Math
1516
alias Scenic.Math.Vector2
1617
alias Scenic.Math.Matrix
1718

@@ -353,7 +354,7 @@ defmodule Scenic.Math.Vector2 do
353354
Returns:
354355
true or false
355356
"""
356-
@spec in_bounds?(vector :: Math.vector_2(), bounds :: Math.vector_2()) :: Math.vector_2()
357+
@spec in_bounds?(vector :: Math.vector_2(), bounds :: Math.vector_2()) :: boolean
357358
def in_bounds?(vector, bounds)
358359

359360
def in_bounds?({vx, vy}, {boundsx, boundsy}),
@@ -373,7 +374,7 @@ defmodule Scenic.Math.Vector2 do
373374
A vector derived from the the space between two other vectors
374375
"""
375376
@spec in_bounds?(vector :: Math.vector_2(), min :: Math.vector_2(), max :: Math.vector_2()) ::
376-
Math.vector_2()
377+
boolean
377378
def in_bounds?(vector, min, max)
378379

379380
def in_bounds?({vx, vy}, {minx, miny}, {maxx, maxy}),

lib/utilities.ex

Lines changed: 0 additions & 17 deletions
This file was deleted.

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ defmodule Scenic.Mixfile do
2020
make_targets: ["all"],
2121
make_clean: ["clean"],
2222
make_env: make_env(),
23-
dialyzer: [plt_add_deps: :transitive, plt_add_apps: [:mix, :iex, :scenic_math]],
23+
dialyzer: [plt_add_deps: :transitive, plt_add_apps: [:mix, :iex]],
2424
test_coverage: [tool: ExCoveralls],
2525
preferred_cli_env: [
2626
coveralls: :test,

test/scenic/cache/hash_test.exs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,26 @@ defmodule Scenic.Cache.HashTest do
2929
test "binary computes a hash for some binary data" do
3030
data = "some data. af98hwu4lhrliw4uhtliuhet;giojres;ihg;usdhg"
3131
expected_hash = :crypto.hash(:sha, data) |> Base.url_encode64(padding: false)
32-
assert Hash.binary(data, :sha) == expected_hash
32+
assert Hash.binary(data, :sha) == {:ok, expected_hash}
33+
end
34+
35+
test "binary rejects invalid hash types" do
36+
data = "some data. af98hwu4lhrliw4uhtliuhet;giojres;ihg;usdhg"
37+
assert Hash.binary(data, :invalid) == {:error, :invalid_hash_type}
3338
end
3439

3540
test "binary! computes a hash for some binary data" do
3641
data = "some data. af98hwu4lhrliw4uhtliuhet;giojres;ihg;usdhg"
3742
expected_hash = :crypto.hash(:sha, data) |> Base.url_encode64(padding: false)
38-
assert Hash.binary(data, :sha) == expected_hash
43+
assert Hash.binary!(data, :sha) == expected_hash
44+
end
45+
46+
test "binary! raises on an invalid hash type" do
47+
data = "some data. af98hwu4lhrliw4uhtliuhet;giojres;ihg;usdhg"
48+
49+
assert_raise Scenic.Cache.Hash.Error, fn ->
50+
Hash.binary!(data, :invalid)
51+
end
3952
end
4053

4154
# ============================================================================
@@ -73,7 +86,7 @@ defmodule Scenic.Cache.HashTest do
7386

7487
test "verify returns {:ok, data} when the hash checks out ok" do
7588
data = "This is some data to hash - awleiufhoq34htuwehtljwuh5toihu"
76-
expected = Hash.binary(data, :sha)
89+
expected = Hash.binary!(data, :sha)
7790
assert Hash.verify(data, expected, :sha) == {:ok, data}
7891
end
7992

@@ -87,7 +100,7 @@ defmodule Scenic.Cache.HashTest do
87100

88101
test "verify! returns data when the hash checks out ok" do
89102
data = "This is some data to hash - awleiufhoq34htuwehtljwuh5toihu"
90-
expected = Hash.binary(data, :sha)
103+
expected = Hash.binary!(data, :sha)
91104
assert Hash.verify!(data, expected, :sha) == data
92105
end
93106

0 commit comments

Comments
 (0)