Current section
Files
Jump to
Current section
Files
src/psl.erl
-module(psl).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/psl.gleam").
-export([load_suffix_list/1, parse_with_list/2, parse/2]).
-export_type([parse_error/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-type parse_error() :: invalid_uri | no_host | invalid_domain | unknown_suffix.
-file("src/psl.gleam", 35).
?DOC(
" Load the public suffix list from the embedded data file and choose to\n"
" include private domains or not.\n"
"\n"
" Use this to pre-load the suffix list when parsing multiple URIs.\n"
).
-spec load_suffix_list(boolean()) -> psl@suffix_list:suffix_list().
load_suffix_list(Include_private) ->
psl@suffix_list:load(Include_private).
-file("src/psl.gleam", 62).
?DOC(
" Parse a URI and extract domain parts using a pre-loaded suffix list\n"
"\n"
" For better performance when parsing multiple URIs, load the suffix list\n"
" once using `load_suffix_list()` and reuse it:\n"
"\n"
" ```gleam\n"
" let list = load_suffix_list()\n"
" let result1 = parse_with_list(\"https://example.com\", list)\n"
" let result2 = parse_with_list(\"https://test.co.uk\", list)\n"
" ```\n"
).
-spec parse_with_list(binary(), psl@suffix_list:suffix_list()) -> {ok,
psl@domain:domain_parts()} |
{error, parse_error()}.
parse_with_list(Uri_string, List) ->
gleam@result:'try'(
begin
_pipe = gleam_stdlib:uri_parse(Uri_string),
gleam@result:replace_error(_pipe, invalid_uri)
end,
fun(Parsed_uri) ->
gleam@result:'try'(case erlang:element(4, Parsed_uri) of
{some, H} ->
{ok, H};
none ->
{error, no_host}
end, fun(Host) ->
Decoded_host = psl@punycode:decode_domain(Host),
gleam@result:'try'(
begin
_pipe@1 = psl@suffix_list:find_suffix(
Decoded_host,
List
),
gleam@result:replace_error(_pipe@1, unknown_suffix)
end,
fun(Suffix) ->
_pipe@2 = psl@domain:extract_parts(
Decoded_host,
Suffix
),
gleam@result:map_error(
_pipe@2,
fun(_) -> invalid_domain end
)
end
)
end)
end
).
-file("src/psl.gleam", 44).
?DOC(
" Parse a URI and extract domain parts\n"
"\n"
" Note: This function loads the suffix list on every call. For better\n"
" performance when parsing multiple URIs, use `parse_with_list()` with\n"
" a cached suffix list.\n"
).
-spec parse(binary(), boolean()) -> {ok, psl@domain:domain_parts()} |
{error, parse_error()}.
parse(Uri_string, Include_private) ->
List = load_suffix_list(Include_private),
parse_with_list(Uri_string, List).