Skip to content

Commit 98088da

Browse files
committed
Simplify code and fix Dialyzer errors
1 parent d8e9683 commit 98088da

File tree

1 file changed

+15
-28
lines changed

1 file changed

+15
-28
lines changed

lib/zbar.ex

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ defmodule Zbar do
2323
{:ok, list(Zbar.Symbol.t())}
2424
| {:error, :timeout}
2525
| {:error, binary()}
26-
| no_return()
2726
def scan(jpeg_data, timeout \\ 5000) do
28-
# We run this is a `Task` so that `collect_output` can use `receive`
27+
# We run this in a `Task` so that `collect_output` can use `receive`
2928
# without interfering with the caller's mailbox.
3029
Task.async(fn -> do_scan(jpeg_data, timeout) end)
3130
|> Task.await(:infinity)
@@ -36,20 +35,9 @@ defmodule Zbar do
3635
| {:error, :timeout}
3736
| {:error, binary()}
3837
defp do_scan(jpeg_data, timeout) do
39-
write_image_to_temp_file(jpeg_data)
40-
open_zbar_port()
41-
|> collect_output(timeout)
42-
|> format_result()
43-
end
44-
45-
@spec write_image_to_temp_file(binary()) :: :ok | no_return()
46-
defp write_image_to_temp_file(jpeg_data) do
4738
File.open!(temp_file(), [:write, :binary], & IO.binwrite(&1, jpeg_data))
48-
end
4939

50-
@spec open_zbar_port() :: port()
51-
defp open_zbar_port do
52-
{:spawn_executable, zbar_binary()}
40+
{:spawn_executable, to_charlist(zbar_binary())}
5341
|> Port.open([
5442
{:args, [temp_file()]},
5543
:binary,
@@ -58,6 +46,19 @@ defmodule Zbar do
5846
:use_stdio,
5947
:stderr_to_stdout
6048
])
49+
|> collect_output(timeout)
50+
|> case do
51+
{:ok, data} ->
52+
symbols =
53+
data
54+
|> String.split("\n", trim: true)
55+
|> Enum.map(&parse_symbol/1)
56+
57+
{:ok, symbols}
58+
59+
{:error, reason} ->
60+
{:error, reason}
61+
end
6162
end
6263

6364
@spec temp_file() :: binary()
@@ -70,7 +71,6 @@ defmodule Zbar do
7071
{:ok, binary()}
7172
| {:error, :timeout}
7273
| {:error, binary()}
73-
| no_return()
7474
defp collect_output(port, timeout, buffer \\ "") do
7575
receive do
7676
{^port, {:data, "Premature end of JPEG file\n"}} ->
@@ -88,19 +88,6 @@ defmodule Zbar do
8888
end
8989
end
9090

91-
# `output` is the complete multi-line output collected from the `port`.
92-
@spec format_result({:ok | :error, binary()}) ::
93-
{:ok, [Zbar.Symbol.t()]}
94-
| {:error, binary()}
95-
defp format_result({:ok, output}) do
96-
symbols =
97-
output
98-
|> String.split("\n", trim: true)
99-
|> Enum.map(&parse_symbol/1)
100-
{:ok, symbols}
101-
end
102-
defp format_result({:error, reason}), do: {:error, reason}
103-
10491
# Accepts strings like:
10592
# type:QR-Code quality:1 points:40,40;40,250;250,250;250,40 data:UkVGMQ==
10693
#

0 commit comments

Comments
 (0)