Skip to content

Commit

Permalink
Use single error type for ac string parsing failure
Browse files Browse the repository at this point in the history
  • Loading branch information
berbiche committed Sep 24, 2023
1 parent 003929a commit 922d95f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
23 changes: 10 additions & 13 deletions src/additional_consent_string.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,19 @@
-export([parse_version/1, parse_atp_ids/1]).
-endif.

-type addtl_parse_error() ::
invalid_atp_id |
invalid_version.
-export_type([addtl_parse_error/0]).

-define(KNOWN_SEPARATOR, "~").
-spec parse(binary()) ->
{ok, addtl_consent()} | {error, addtl_parse_error()}.
{ok, addtl_consent()} | {error, invalid_ac_string}.
% @doc Parses an additional consent string, returning the version
% number and the list of numerical ATP ids.
%
% @returns `{ok, #addtl_consent{}}' if the string is valid,
% otherwise an appropriate error is returned.
%
parse(Bin) ->
parse(Bin) when is_binary(Bin) ->
case parse_version(Bin) of
{ok, Version, Bin1} ->
case parse_atp_ids(Bin1) of
Expand All @@ -37,7 +32,9 @@ parse(Bin) ->
Error -> Error
end;
Error -> Error
end.
end;
parse(_) ->
{error, invalid_ac_string}.
parse_version(Bin) ->
parse_version(Bin, <<"">>).
Expand All @@ -48,12 +45,12 @@ parse_version(<<Bin, Rest/binary>>, Acc) when Bin >= $0, Bin =< $9 ->
parse_version(<<?KNOWN_SEPARATOR, Rest/binary>>, Acc) ->
case safe_binary_to_integer(Acc) of
undefined ->
{error, invalid_version};
{error, invalid_ac_string};
Version ->
{ok, Version, Rest}
end;
parse_version(_, _) ->
{error, invalid_version}.
{error, invalid_ac_string}.
parse_atp_ids(Bin) ->
parse_atp_ids(Bin, <<"">>, []).
Expand All @@ -63,18 +60,18 @@ parse_atp_ids(<<Bin, Rest/binary>>, Acc, Ids) when Bin >= $0, Bin =< $9 ->
parse_atp_ids(<<".", Rest/binary>>, Acc, Ids) when Rest =/= <<"">> ->
case safe_binary_to_integer(Acc) of
undefined ->
{error, invalid_atp_id};
{error, invalid_ac_string};
Id ->
parse_atp_ids(Rest, <<"">>, [Id | Ids])
end;
parse_atp_ids(<<_Invalid, _Rest/binary>>, _Acc, _Ids) ->
{error, invalid_atp_id};
{error, invalid_ac_string};
parse_atp_ids(<<"">>, <<"">>, Ids) ->
{ok, Ids};
parse_atp_ids(<<"">>, Acc, Ids) ->
case safe_binary_to_integer(Acc) of
undefined ->
{error, invalid_atp_id};
{error, invalid_ac_string};
Id ->
% Preserve the order of ids
{ok, lists:reverse([Id | Ids])}
Expand Down
7 changes: 4 additions & 3 deletions test/ac_string_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
-define(_assertMatchVersion(A, B),
?_assertMatch({ok, (A), _}, parse_version((B)))).
-define(_assertErrorVersion(B),
?_assertMatch({error, invalid_version}, parse_version((B)))).
?_assertMatch({error, invalid_ac_string}, parse_version((B)))).
-define(_assertErrorAtp(B),
?_assertMatch({error, invalid_atp_id}, parse_atp_ids((B)))).
?_assertMatch({error, invalid_ac_string}, parse_atp_ids((B)))).

parse_version_only_test_() ->
[
Expand Down Expand Up @@ -52,7 +52,8 @@ parse_test_() ->
?_assertMatch({ok, #addtl_consent{version = 1, atp_ids = [1, 3, 2]}}, parse(<<"1~1.3.2">>)),
% Test errors
?_assertMatch({error, invalid_atp_id}, parse(<<"1~a">>))
?_assertMatch({error, invalid_ac_string}, parse(<<"1~a">>)),
?_assertMatch({error, invalid_ac_string}, parse(<<"1a1~">>))
].
%% Helpers
Expand Down

0 comments on commit 922d95f

Please sign in to comment.