Skip to content

Commit 6cc7a9f

Browse files
committed
better option validation
1 parent b39f4e4 commit 6cc7a9f

1 file changed

Lines changed: 90 additions & 14 deletions

File tree

lib/dmp/options.ex

Lines changed: 90 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,36 +76,112 @@ defmodule Dmp.Options do
7676
]
7777
7878
iex> Options.valid_options!(match_max_bits: -1)
79-
** (ArgumentError) Invalid Options value(s)
79+
** (ArgumentError) Invalid options: match_max_bits (-1)
8080
8181
"""
8282
@spec valid_options!(t()) :: t()
8383
def valid_options!([]), do: default()
8484

85-
# credo:disable-for-lines:25 Credo.Check.Refactor.CyclomaticComplexity
85+
# credo:disable-for-lines:21 Credo.Check.Refactor.CyclomaticComplexity
8686
def valid_options!(opts) when is_list(opts) do
8787
opts = Keyword.merge(default(), opts) |> Enum.sort()
88+
89+
{_, errors} =
90+
{opts, []}
91+
|> validate_diff_timeout()
92+
|> validate_diff_edit_cost()
93+
|> validate_match_max_bits()
94+
|> validate_match_threshold()
95+
|> validate_match_distance()
96+
|> validate_patch_delete_threshold()
97+
|> validate_patch_margin()
98+
99+
if errors == [] do
100+
opts
101+
else
102+
errors = Enum.map_join(errors, ", ", fn {name, value} -> name <> " (#{value})" end)
103+
raise ArgumentError, "Invalid options: #{errors}"
104+
end
105+
end
106+
107+
defp valid_match_max_bits?(match_max_bits) do
108+
match_max_bits > 0 && match_max_bits <= 128
109+
end
110+
111+
defp valid_threshold?(value) do
112+
value >= 0 and value <= 1
113+
end
114+
115+
defp validate_diff_timeout({opts, errors}) do
88116
diff_timeout = Keyword.fetch!(opts, :diff_timeout)
117+
118+
if diff_timeout >= 0 do
119+
{opts, errors}
120+
else
121+
{opts, [{"diff_timeout", diff_timeout} | errors]}
122+
end
123+
end
124+
125+
defp validate_diff_edit_cost({opts, errors}) do
89126
diff_edit_cost = Keyword.fetch!(opts, :diff_edit_cost)
127+
128+
if diff_edit_cost >= 0 do
129+
{opts, errors}
130+
else
131+
{opts, [{"diff_edit_cost", diff_edit_cost} | errors]}
132+
end
133+
end
134+
135+
defp validate_match_max_bits({opts, errors}) do
90136
match_max_bits = Keyword.fetch!(opts, :match_max_bits)
137+
138+
if valid_match_max_bits?(match_max_bits) do
139+
{opts, errors}
140+
else
141+
{opts, [{"match_max_bits", match_max_bits} | errors]}
142+
end
143+
end
144+
145+
defp validate_match_threshold({opts, errors}) do
91146
match_threshold = Keyword.fetch!(opts, :match_threshold)
147+
148+
if valid_threshold?(match_threshold) do
149+
{opts, errors}
150+
else
151+
{opts, [{"match_threshold", match_threshold} | errors]}
152+
end
153+
end
154+
155+
defp validate_match_distance({opts, errors}) do
92156
match_distance = Keyword.fetch!(opts, :match_distance)
157+
158+
if match_distance >= 0 do
159+
{opts, errors}
160+
else
161+
{opts, [{"match_distance", match_distance} | errors]}
162+
end
163+
end
164+
165+
defp validate_patch_delete_threshold({opts, errors}) do
93166
patch_delete_threshold = Keyword.fetch!(opts, :patch_delete_threshold)
94-
patch_margin = Keyword.fetch!(opts, :patch_margin)
95167

96-
valid =
97-
match_max_bits >= 0 && match_max_bits <= 64 &&
98-
patch_margin >= 0 && patch_margin < match_max_bits &&
99-
match_threshold >= 0 && match_threshold <= 1.0 &&
100-
patch_delete_threshold >= 0 && patch_delete_threshold <= 1.0 &&
101-
match_distance >= 0 &&
102-
diff_edit_cost >= 0 &&
103-
diff_timeout >= 0
168+
if valid_threshold?(patch_delete_threshold) do
169+
{opts, errors}
170+
else
171+
{opts, [{"patch_delete_threshold", patch_delete_threshold} | errors]}
172+
end
173+
end
174+
175+
defp validate_patch_margin({opts, errors}) do
176+
match_max_bits = Keyword.fetch!(opts, :match_max_bits)
177+
patch_margin = Keyword.fetch!(opts, :patch_margin)
104178

105-
if valid do
106-
opts
179+
if patch_margin >= 0 &&
180+
(!valid_match_max_bits?(match_max_bits) ||
181+
patch_margin < match_max_bits) do
182+
{opts, errors}
107183
else
108-
raise ArgumentError, "Invalid Options value(s)"
184+
{opts, [{"patch_margin", patch_margin} | errors]}
109185
end
110186
end
111187
end

0 commit comments

Comments
 (0)