From 3e55cb62c9053895412b1cf2a65d54ef90eda34c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Wed, 25 May 2011 23:02:40 +0200 Subject: Refresh the type specifications. Following discussions on #erlounge. Also fixes compilation in R14B03 and fixes a few underspecs dialyzer warnings. --- src/cowboy_dispatcher.erl | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) (limited to 'src/cowboy_dispatcher.erl') diff --git a/src/cowboy_dispatcher.erl b/src/cowboy_dispatcher.erl index 67a7bf6..e34a4eb 100644 --- a/src/cowboy_dispatcher.erl +++ b/src/cowboy_dispatcher.erl @@ -16,22 +16,21 @@ -module(cowboy_dispatcher). -export([split_host/1, split_path/1, match/3]). %% API. --type bindings() :: list({Key::atom(), Value::binary()}). +-type bindings() :: list({atom(), binary()}). -type path_tokens() :: list(binary()). -type match_rule() :: '_' | '*' | list(binary() | '_' | atom()). --type dispatch_rule() :: {Host::match_rule(), list({Path::match_rule(), - Handler::module(), Opts::term()})}. +-type dispatch_path() :: list({match_rule(), module(), any()}). +-type dispatch_rule() :: {Host::match_rule(), Path::dispatch_path()}. -type dispatch_rules() :: list(dispatch_rule()). -export_type([bindings/0, path_tokens/0, dispatch_rules/0]). --include_lib("kernel/include/inet.hrl"). -include_lib("eunit/include/eunit.hrl"). %% API. --spec split_host(Host::binary()) - -> {Tokens::path_tokens(), RawHost::binary(), Port::undefined | ip_port()}. +-spec split_host(binary()) + -> {path_tokens(), binary(), undefined | inet:ip_port()}. split_host(<<>>) -> {[], <<>>, undefined}; split_host(Host) -> @@ -43,8 +42,7 @@ split_host(Host) -> list_to_integer(binary_to_list(Port))} end. --spec split_path(Path::binary()) - -> {Tokens::path_tokens(), RawPath::binary(), Qs::binary()}. +-spec split_path(binary()) -> {path_tokens(), binary(), binary()}. split_path(Path) -> case binary:split(Path, <<"?">>) of [Path] -> {do_split_path(Path, <<"/">>), Path, <<>>}; @@ -52,17 +50,15 @@ split_path(Path) -> [Path2, Qs] -> {do_split_path(Path2, <<"/">>), Path2, Qs} end. --spec do_split_path(RawPath::binary(), Separator::binary()) - -> Tokens::path_tokens(). +-spec do_split_path(binary(), <<_:8>>) -> path_tokens(). do_split_path(RawPath, Separator) -> case binary:split(RawPath, Separator, [global, trim]) of [<<>>|Path] -> Path; Path -> Path end. --spec match(Host::path_tokens(), Path::path_tokens(), - Dispatch::dispatch_rules()) - -> {ok, Handler::module(), Opts::term(), Binds::bindings(), +-spec match(Host::path_tokens(), Path::path_tokens(), dispatch_rules()) + -> {ok, module(), any(), bindings(), HostInfo::undefined | path_tokens(), PathInfo::undefined | path_tokens()} | {error, notfound, host} | {error, notfound, path}. @@ -80,10 +76,9 @@ match(Host, Path, [{HostMatch, PathMatchs}|Tail]) -> match_path(Path, PathMatchs, HostBinds, lists:reverse(HostInfo)) end. --spec match_path(Path::path_tokens(), list({Path::match_rule(), - Handler::module(), Opts::term()}), HostBinds::bindings(), +-spec match_path(path_tokens(), dispatch_path(), bindings(), HostInfo::undefined | path_tokens()) - -> {ok, Handler::module(), Opts::term(), Binds::bindings(), + -> {ok, module(), any(), bindings(), HostInfo::undefined | path_tokens(), PathInfo::undefined | path_tokens()} | {error, notfound, path}. @@ -103,15 +98,15 @@ match_path(Path, [{PathMatch, Handler, Opts}|Tail], HostBinds, HostInfo) -> %% Internal. --spec try_match(Type::host | path, List::path_tokens(), Match::match_rule()) - -> {true, Binds::bindings(), ListInfo::undefined | path_tokens()} | false. +-spec try_match(host | path, path_tokens(), match_rule()) + -> {true, bindings(), undefined | path_tokens()} | false. try_match(host, List, Match) -> list_match(lists:reverse(List), lists:reverse(Match), []); try_match(path, List, Match) -> list_match(List, Match, []). --spec list_match(List::path_tokens(), Match::match_rule(), Binds::bindings()) - -> {true, Binds::bindings(), ListInfo::undefined | path_tokens()} | false. +-spec list_match(path_tokens(), match_rule(), bindings()) + -> {true, bindings(), undefined | path_tokens()} | false. %% Atom '...' matches any trailing path, stop right now. list_match(List, ['...'], Binds) -> {true, Binds, List}; -- cgit v1.2.3