add specs and exported types where needed

This patch exports types that are needed and adds specs where they did
not previously exist. It also cleans up and expands the specs where
they needed to be cleaned up.
This commit is contained in:
Eric Merritt 2011-04-22 09:30:03 -05:00 committed by Jordan Wilberding
parent 2508e1ed82
commit 07a54bb518
3 changed files with 63 additions and 20 deletions

View file

@ -22,7 +22,10 @@
consult/1 consult/1
]). ]).
-export_type([path/0]). -export_type([
path/0,
option/0
]).
-include_lib("kernel/include/file.hrl"). -include_lib("kernel/include/file.hrl").
@ -41,12 +44,13 @@
%% Types %% Types
%%============================================================================ %%============================================================================
-type path() :: string(). -type path() :: string().
-type option() :: [atom()].
%%%=================================================================== %%%===================================================================
%%% API %%% API
%%%=================================================================== %%%===================================================================
%% @doc copy an entire directory to another location. %% @doc copy an entire directory to another location.
-spec copy(path(), path(), Options::list()) -> ok. -spec copy(path(), path(), Options::[option()]) -> ok.
copy(From, To, []) -> copy(From, To, []) ->
copy(From, To); copy(From, To);
copy(From, To, [recursive] = Options) -> copy(From, To, [recursive] = Options) ->
@ -77,7 +81,7 @@ md5sum(Value) ->
%% <pre> %% <pre>
%% Example: remove("./tmp_dir", [recursive]). %% Example: remove("./tmp_dir", [recursive]).
%% </pre> %% </pre>
-spec remove(path(), Options::list()) -> ok | {error, Reason::term()}. -spec remove(path(), Options::[option()]) -> ok | {error, Reason::term()}.
remove(Path, Options) -> remove(Path, Options) ->
try try
ok = ec_file_remove(Path, Options) ok = ec_file_remove(Path, Options)
@ -132,7 +136,7 @@ mkdir_path(Path) ->
%% @doc consult an erlang term file from the file system. %% @doc consult an erlang term file from the file system.
%% Provide user readible exeption on failure. %% Provide user readible exeption on failure.
-spec consult(FilePath::string()) -> term(). -spec consult(FilePath::path()) -> term().
consult(FilePath) -> consult(FilePath) ->
case file:consult(FilePath) of case file:consult(FilePath) of
{ok, [Term]} -> {ok, [Term]} ->
@ -181,7 +185,7 @@ write_term(FileName, Term) ->
%% @doc Finds files and directories that match the regexp supplied in %% @doc Finds files and directories that match the regexp supplied in
%% the TargetPattern regexp. %% the TargetPattern regexp.
-spec find(FromDir::path(), TargetPattern::string()) -> list(). -spec find(FromDir::path(), TargetPattern::string()) -> [path()].
find([], _) -> find([], _) ->
[]; [];
find(FromDir, TargetPattern) -> find(FromDir, TargetPattern) ->
@ -202,6 +206,7 @@ find(FromDir, TargetPattern) ->
%%%=================================================================== %%%===================================================================
%%% Internal Functions %%% Internal Functions
%%%=================================================================== %%%===================================================================
-spec find_in_subdirs(path(), string()) -> [path()].
find_in_subdirs(FromDir, TargetPattern) -> find_in_subdirs(FromDir, TargetPattern) ->
lists:foldl(fun(CheckFromDir, Acc) lists:foldl(fun(CheckFromDir, Acc)
when CheckFromDir == FromDir -> when CheckFromDir == FromDir ->
@ -215,6 +220,7 @@ find_in_subdirs(FromDir, TargetPattern) ->
[], [],
filelib:wildcard(filename:join(FromDir, "*"))). filelib:wildcard(filename:join(FromDir, "*"))).
-spec ec_file_remove(path(), [{atom(), any()}]) -> ok.
ec_file_remove(Path, Options) -> ec_file_remove(Path, Options) ->
case lists:member(recursive, Options) of case lists:member(recursive, Options) of
false -> file:delete(Path); false -> file:delete(Path);
@ -233,6 +239,7 @@ remove_recursive(Path, Options) ->
ok = file:del_dir(Path) ok = file:del_dir(Path)
end. end.
-spec tmp() -> path().
tmp() -> tmp() ->
case erlang:system_info(system_architecture) of case erlang:system_info(system_architecture) of
"win32" -> "win32" ->
@ -243,6 +250,7 @@ tmp() ->
end. end.
%% Copy the subfiles of the From directory to the to directory. %% Copy the subfiles of the From directory to the to directory.
-spec copy_subfiles(path(), path(), [option()]) -> ok.
copy_subfiles(From, To, Options) -> copy_subfiles(From, To, Options) ->
Fun = Fun =
fun(ChildFrom) -> fun(ChildFrom) ->
@ -251,11 +259,13 @@ copy_subfiles(From, To, Options) ->
end, end,
lists:foreach(Fun, filelib:wildcard(filename:join(From, "*"))). lists:foreach(Fun, filelib:wildcard(filename:join(From, "*"))).
-spec ec_file_copy(path(), path()) -> ok.
ec_file_copy(From, To) -> ec_file_copy(From, To) ->
{ok, _} = file:copy(From, To), {ok, _} = file:copy(From, To),
{ok, FileInfo} = file:read_file_info(From), {ok, FileInfo} = file:read_file_info(From),
ok = file:write_file_info(To, FileInfo). ok = file:write_file_info(To, FileInfo).
-spec make_dir_if_dir(path()) -> ok.
make_dir_if_dir(File) -> make_dir_if_dir(File) ->
case filelib:is_dir(File) of case filelib:is_dir(File) of
true -> ok; true -> ok;
@ -263,6 +273,7 @@ make_dir_if_dir(File) ->
end. end.
%% @doc convert a list of integers into hex. %% @doc convert a list of integers into hex.
-spec hex(string() | non_neg_integer()) -> string().
hex(L) when is_list (L) -> hex(L) when is_list (L) ->
lists:flatten([hex(I) || I <- L]); lists:flatten([hex(I) || I <- L]);
hex(I) when I > 16#f -> hex(I) when I > 16#f ->

View file

@ -11,6 +11,20 @@
compare/2 compare/2
]). ]).
-export_type([
semvar/0
]).
%%%===================================================================
%%% Public Types
%%%===================================================================
-type semvar() :: string().
-type parsed_semvar() :: {MajorVsn::string(),
MinorVsn::string(),
PatchVsn::string(),
PathString::string()}.
%%%=================================================================== %%%===================================================================
%%% API %%% API
%%%=================================================================== %%%===================================================================
@ -26,15 +40,20 @@ compare(VsnA, VsnB) ->
%%%=================================================================== %%%===================================================================
%%% Internal Functions %%% Internal Functions
%%%=================================================================== %%%===================================================================
-spec tokens(semvar()) -> parsed_semvar().
tokens(Vsn) -> tokens(Vsn) ->
[MajorVsn, MinorVsn, RawPatch] = string:tokens(Vsn, "."), [MajorVsn, MinorVsn, RawPatch] = string:tokens(Vsn, "."),
{PatchVsn, PatchString} = split_patch(RawPatch), {PatchVsn, PatchString} = split_patch(RawPatch),
{MajorVsn, MinorVsn, PatchVsn, PatchString}. {MajorVsn, MinorVsn, PatchVsn, PatchString}.
-spec split_patch(string()) ->
{PatchVsn::string(), PatchStr::string()}.
split_patch(RawPatch) -> split_patch(RawPatch) ->
{PatchVsn, PatchStr} = split_patch(RawPatch, {"", ""}), {PatchVsn, PatchStr} = split_patch(RawPatch, {"", ""}),
{lists:reverse(PatchVsn), PatchStr}. {lists:reverse(PatchVsn), PatchStr}.
-spec split_patch(string(), {AccPatchVsn::string(), AccPatchStr::string()}) ->
{PatchVsn::string(), PatchStr::string()}.
split_patch([], Acc) -> split_patch([], Acc) ->
Acc; Acc;
split_patch([Dig|T], {PatchVsn, PatchStr}) when Dig >= $0 andalso Dig =< $9 -> split_patch([Dig|T], {PatchVsn, PatchStr}) when Dig >= $0 andalso Dig =< $9 ->
@ -42,10 +61,12 @@ split_patch([Dig|T], {PatchVsn, PatchStr}) when Dig >= $0 andalso Dig =< $9 ->
split_patch(PatchStr, {PatchVsn, ""}) -> split_patch(PatchStr, {PatchVsn, ""}) ->
{PatchVsn, PatchStr}. {PatchVsn, PatchStr}.
-spec compare_toks(parsed_semvar(), parsed_semvar()) -> boolean().
compare_toks({MajA, MinA, PVA, PSA}, {MajB, MinB, PVB, PSB}) -> compare_toks({MajA, MinA, PVA, PSA}, {MajB, MinB, PVB, PSB}) ->
compare_toks2({to_int(MajA), to_int(MinA), to_int(PVA), PSA}, compare_toks2({to_int(MajA), to_int(MinA), to_int(PVA), PSA},
{to_int(MajB), to_int(MinB), to_int(PVB), PSB}). {to_int(MajB), to_int(MinB), to_int(PVB), PSB}).
-spec compare_toks2(parsed_semvar(), parsed_semvar()) -> boolean().
compare_toks2({MajA, _MinA, _PVA, _PSA}, {MajB, _MinB, _PVB, _PSB}) when MajA > MajB -> compare_toks2({MajA, _MinA, _PVA, _PSA}, {MajB, _MinB, _PVB, _PSB}) when MajA > MajB ->
true; true;
compare_toks2({_Maj, MinA, _PVA, _PSA}, {_Maj, MinB, _PVB, _PSB}) when MinA > MinB -> compare_toks2({_Maj, MinA, _PVA, _PSA}, {_Maj, MinB, _PVB, _PSB}) when MinA > MinB ->
@ -61,6 +82,7 @@ compare_toks2({_Maj, _Min, _PV, PSA}, {_Maj, _Min, _PV, PSB}) when PSA > PSB ->
compare_toks2(_ToksA, _ToksB) -> compare_toks2(_ToksA, _ToksB) ->
false. false.
-spec to_int(string()) -> integer().
to_int(String) -> to_int(String) ->
case catch list_to_integer(String) of case catch list_to_integer(String) of
Integer when is_integer(Integer) -> Integer when is_integer(Integer) ->

View file

@ -34,6 +34,7 @@ compare_versions(VsnA, VsnB) ->
%%% Internal Functions %%% Internal Functions
%%%=================================================================== %%%===================================================================
-spec compare(string(), string()) -> boolean().
compare([Str|TA], [Str|TB]) -> compare([Str|TA], [Str|TB]) ->
compare(TA, TB); compare(TA, TB);
compare([StrA|TA], [StrB|TB]) -> compare([StrA|TA], [StrB|TB]) ->
@ -50,6 +51,7 @@ compare([_,_|_], []) ->
compare([], []) -> compare([], []) ->
false. false.
-spec compare_against_nothing(string()) -> boolean().
compare_against_nothing(Str) -> compare_against_nothing(Str) ->
case split_numeric_alpha(Str) of case split_numeric_alpha(Str) of
{_StrDig, ""} -> true; {_StrDig, ""} -> true;
@ -57,6 +59,9 @@ compare_against_nothing(Str) ->
{_StrDig, _StrAlpha} -> true {_StrDig, _StrAlpha} -> true
end. end.
-spec fine_compare({string(), string()}, string(),
{string(), string()}, string()) ->
boolean().
fine_compare({_StrDigA, StrA}, TA, {_StrDigB, _StrB}, _TB) when StrA /= "", TA /= [] -> fine_compare({_StrDigA, StrA}, TA, {_StrDigB, _StrB}, _TB) when StrA /= "", TA /= [] ->
throw(invalid_version_string); throw(invalid_version_string);
fine_compare({_StrDigA, _StrA}, _TA, {_StrDigB, StrB}, TB) when StrB /= "", TB /= [] -> fine_compare({_StrDigA, _StrA}, _TA, {_StrDigB, StrB}, TB) when StrB /= "", TB /= [] ->
@ -76,10 +81,15 @@ fine_compare({StrDigA, _StrA}, _TA, {StrDigB, _StrB}, _TB) ->
%% In the case of a version sub part with a numeric then an alpha, %% In the case of a version sub part with a numeric then an alpha,
%% split out the numeric and alpha "24alpha" becomes {"24", "alpha"} %% split out the numeric and alpha "24alpha" becomes {"24", "alpha"}
-spec split_numeric_alpha(string()) ->
{PatchVsn::string(), PatchStr::string()}.
split_numeric_alpha(RawVsn) -> split_numeric_alpha(RawVsn) ->
{Num, Str} = split_numeric_alpha(RawVsn, {"", ""}), {Num, Str} = split_numeric_alpha(RawVsn, {"", ""}),
{lists:reverse(Num), Str}. {lists:reverse(Num), Str}.
-spec split_numeric_alpha(string(), {PatchVsnAcc::string(),
PatchStrAcc::string()}) ->
{PatchVsn::string(), PatchStr::string()}.
split_numeric_alpha([], Acc) -> split_numeric_alpha([], Acc) ->
Acc; Acc;
split_numeric_alpha([Dig|T], {PatchVsn, PatchStr}) split_numeric_alpha([Dig|T], {PatchVsn, PatchStr})