Skip to content

Commit

Permalink
Consistent formatting (Emacs).
Browse files Browse the repository at this point in the history
  • Loading branch information
oubiwann committed Jun 9, 2021
1 parent a1fd36b commit be907dd
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 176 deletions.
212 changes: 106 additions & 106 deletions src/coers.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@

%% API of Coers
-export([
value/1,
error/1,
has_error/1,
is_ascii_char/1,
maybe_string/1,
to_string/1,
to_string/2,
of_string/1,
of_string/2,
numeric_align/1,
to_int/1,
to_int/2,
to_float/1,
to_float/2,
to_atom/1,
to_atom/2,
to_bool/1,
to_bool/2,
to_rational/1,
to_rational/2
]).
value/1,
error/1,
has_error/1,
is_ascii_char/1,
maybe_string/1,
to_string/1,
to_string/2,
of_string/1,
of_string/2,
numeric_align/1,
to_int/1,
to_int/2,
to_float/1,
to_float/2,
to_atom/1,
to_atom/2,
to_bool/1,
to_bool/2,
to_rational/1,
to_rational/2
]).

-include_lib("results/include/results.hrl").

Expand All @@ -46,29 +46,29 @@ has_error(Result) ->
%% @doc determine if an integer is a potential Ascii Char
-spec is_ascii_char(integer()) -> boolean().
is_ascii_char(X) when is_integer(X) ->
(X >= 32) and (X < 127);
(X >= 32) and (X < 127);
is_ascii_char([H]) ->
is_ascii_char(H);
is_ascii_char(H);
is_ascii_char(_) ->
false.
false.

%% @doc check if a list is maybe a string
-spec maybe_string(list()) -> boolean().
maybe_string(List) when is_list(List) ->
lists:all(fun is_ascii_char/1, List);
lists:all(fun is_ascii_char/1, List);
maybe_string(_) -> false.

%% @doc try to coerce term into string
-spec to_string((binary() | [any()])) -> result().
to_string(Term) when is_bitstring(Term) ->
List = binary_to_list(Term),
to_string(List);
List = binary_to_list(Term),
to_string(List);
to_string(Term) ->
case maybe_string(Term) of
true -> results:new(Term);
false ->
List = io_lib:format("~p", [Term]),
results:new(lists:flatten(List))
case maybe_string(Term) of
true -> results:new(Term);
false ->
List = io_lib:format("~p", [Term]),
results:new(lists:flatten(List))
end.

%% @doc Replace value if coercion failed
Expand All @@ -82,31 +82,31 @@ to_string(Term, Default) ->
%% @doc an ugly and magic coercion from string to term()
-spec of_string(string()) -> result().
of_string(String) ->
{ok, Regexp} = re:compile("^.+(\\,|\\;|\\.)$"),
{ok, Regexp} = re:compile("^.+(\\,|\\;|\\.)$"),
S =
case re:run(String, Regexp) of
{match, [_, {Offset, _}]} ->
Substring = string:substr(String, 1, Offset -1),
Substring ++ ".";
_ -> String ++ "."
end,
case re:run(String, Regexp) of
{match, [_, {Offset, _}]} ->
Substring = string:substr(String, 1, Offset -1),
Substring ++ ".";
_ -> String ++ "."
end,
case erl_scan:string(S) of
{ok, Tokens, _} ->
case erl_parse:parse_exprs(Tokens) of
{ok, Exprs} ->
{value, Val, []} = erl_eval:exprs(Exprs, []),
results:new(Val);
{error, {_, erl_parse, _}} ->
%% TODO extract the error message and add to new_error
format_error_msg(erl_parse, "Could not convert string ~p", [String]);
{error, {Err, A, B}} ->
{ok, Tokens, _} ->
case erl_parse:parse_exprs(Tokens) of
{ok, Exprs} ->
{value, Val, []} = erl_eval:exprs(Exprs, []),
results:new(Val);
{error, {_, erl_parse, _}} ->
%% TODO extract the error message and add to new_error
format_error_msg(erl_parse, "Could not convert string ~p", [String]);
{error, {Err, A, B}} ->
%% TODO extract the error message and add to new_error
format_error_msg(Err, "Could not convert string ~p, ~p, ~p", [String, A, B])
end;
{error, {Err, A, B}, _} ->
%% TODO extract the error message and add to new_error
format_error_msg(Err, "Could not convert string ~p, ~p, ~p", [String, A, B])
end;
{error, {Err, A, B}, _} ->
%% TODO extract the error message and add to new_error
format_error_msg(Err, "Could not convert string ~p, ~p, ~p", [String, A, B])
end.
end.

%% @doc try coercion or define a default value the suceeded flag is preserved
-spec of_string(string(), term()) -> result().
Expand All @@ -118,41 +118,41 @@ of_string(Term, Default) ->
%% @doc numeric alignement of a string (float of int)
-spec numeric_align(string()) -> atom().
numeric_align(String) ->
{ok, RatioRegex} = re:compile(?RATIO_REGEX),
case re:run(String, RatioRegex) of
{match, _} -> rational;
_ -> numeric_alignt_int_float(String)
end.
{ok, RatioRegex} = re:compile(?RATIO_REGEX),
case re:run(String, RatioRegex) of
{match, _} -> rational;
_ -> numeric_alignt_int_float(String)
end.

-spec numeric_alignt_int_float(string()) -> atom().
numeric_alignt_int_float(String) ->
{ok, Regexp} = re:compile(?NUM_REGEX),
case re:run(String, Regexp) of
{match, [_, _]} -> integer;
{match, [_, _, _]} -> float;
{match, [_, _, _, _]} -> float;
_ -> any
end.
{ok, Regexp} = re:compile(?NUM_REGEX),
case re:run(String, Regexp) of
{match, [_, _]} -> integer;
{match, [_, _, _]} -> float;
{match, [_, _, _, _]} -> float;
_ -> any
end.

%% @doc try to coerce a term to an integer
-spec to_int(term()) -> result().
to_int(Obj) when is_integer(Obj) -> results:new(Obj);
to_int(Obj) when is_bitstring(Obj) -> to_int(binary_to_list(Obj));
to_int(Obj) when is_list(Obj) ->
try list_to_integer(Obj) of
Val -> results:new(Val)
catch error:Err ->
case numeric_align(Obj) of
float -> to_int(list_to_float(Obj));
Type -> format_error_msg(Err, "Could not convert ~p (type ~p) to int", [Obj, Type])
end
end;
try list_to_integer(Obj) of
Val -> results:new(Val)
catch error:Err ->
case numeric_align(Obj) of
float -> to_int(list_to_float(Obj));
Type -> format_error_msg(Err, "Could not convert ~p (type ~p) to int", [Obj, Type])
end
end;
to_int(Obj) when is_atom(Obj) ->
try Soft = atom_to_list(Obj), to_int(Soft) of
Result -> Result
catch error:Err ->
format_error_msg(Err, "Could not convert ~p to int", [Obj])
end;
try Soft = atom_to_list(Obj), to_int(Soft) of
Result -> Result
catch error:Err ->
format_error_msg(Err, "Could not convert ~p to int", [Obj])
end;
to_int(Obj) -> format_error_msg(error, "Could not convert ~p to int", [Obj]).

%% @doc try coercion or define a default value
Expand All @@ -169,20 +169,20 @@ to_int(Term, Default) ->
to_float(Obj) when is_float(Obj) -> results:new(Obj);
to_float(Obj) when is_bitstring(Obj) -> to_float(binary_to_list(Obj));
to_float(Obj) when is_list(Obj) ->
try list_to_float(Obj) of
Val -> results:new(Val)
catch error:Err ->
case numeric_align(Obj) of
integer -> to_float(list_to_integer(Obj));
Type -> format_error_msg(Err, "Could not convert ~p (type ~p) to float", [Obj, Type])
end
end;
try list_to_float(Obj) of
Val -> results:new(Val)
catch error:Err ->
case numeric_align(Obj) of
integer -> to_float(list_to_integer(Obj));
Type -> format_error_msg(Err, "Could not convert ~p (type ~p) to float", [Obj, Type])
end
end;
to_float(Obj) when is_atom(Obj) ->
try Obj2 = atom_to_list(Obj), to_float(Obj2) of
Result -> Result
catch error:Err ->
format_error_msg(Err, "Could not convert ~p to flost", [Obj])
end;
try Obj2 = atom_to_list(Obj), to_float(Obj2) of
Result -> Result
catch error:Err ->
format_error_msg(Err, "Could not convert ~p to flost", [Obj])
end;
to_float(Obj) ->
format_error_msg(error, "Could not convert ~p to flost", [Obj]).

Expand All @@ -197,13 +197,13 @@ to_float(Term, Default) ->
-spec to_atom(term()) -> result().
to_atom(Obj) when is_atom(Obj) -> results:new(Obj);
to_atom(Obj) when is_list(Obj) ->
try list_to_atom(Obj) of
Val -> results:new(Val)
catch Err:_ -> results:new_error(Err)
end;
try list_to_atom(Obj) of
Val -> results:new(Val)
catch Err:_ -> results:new_error(Err)
end;
to_atom(Obj) ->
Pred = to_string(Obj),
to_atom(results:value(Pred)).
Pred = to_string(Obj),
to_atom(results:value(Pred)).

%% @doc try coercion or define a default value the suceeded flag is preserved
-spec to_atom(term(), term()) -> result().
Expand All @@ -215,16 +215,16 @@ to_atom(Term, Default) ->
%% @doc try to coerce a term to a boolean
-spec to_bool(term()) -> result().
to_bool(Obj) when is_atom(Obj) ->
results:new(not (Obj == false));
results:new(not (Obj == false));
to_bool(Obj) when is_list(Obj) ->
case string:to_lower(Obj) of
"true" -> results:new(true);
"false" -> results:new(false);
_ -> results:new(true)
end;
case string:to_lower(Obj) of
"true" -> results:new(true);
"false" -> results:new(false);
_ -> results:new(true)
end;
to_bool(X) when is_bitstring(X) ->
Pred = to_string(X),
to_bool(results:value(Pred));
Pred = to_string(X),
to_bool(results:value(Pred));
to_bool(0) -> results:new(false);
to_bool(0.0) -> results:new(false);
to_bool(1) -> results:new(true);
Expand All @@ -234,7 +234,7 @@ to_bool(_) -> results:new(true).
%% @doc try coercion or define a default value the suceeded flag is preserved
-spec to_bool(term(), term()) -> result().
to_bool(Term, Default) ->
results:attempt(to_bool(Term), Default).
results:attempt(to_bool(Term), Default).

-spec to_rational(term()) -> result().
to_rational(Obj) when is_list(Obj) ->
Expand Down
Loading

0 comments on commit be907dd

Please sign in to comment.