Packages

Efficient Gleam representation for IP addresses

Current section

Files

Jump to
glip src glip.erl
Raw

src/glip.erl

-module(glip).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/glip.gleam").
-export([parse_ip/1, parse_ip_preserving_string/1, to_external_ip/1, address_family/1, ip_to_string/1, enrich/1]).
-export_type([address_family/0, external_ip_address/0, ip_address/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.
?MODULEDOC(
" Glip provides a standard representation for IP addresses\n"
" on Gleam for Erlang and node/bun on JavaScript.\n"
" The intent is to have a minimal representation in memory,\n"
" and avoid unnecessary conversions for common operations.\n"
" Glip is designed for server-oriented usage,\n"
" which is why it doesn't have a browser-compatible implementation,\n"
" and only wraps functionality in the runtimes.\n"
" (Feel free to open an issue if you have a valid browser use case).\n"
).
-type address_family() :: ipv4 | ipv6.
-type external_ip_address() :: any().
-type ip_address() :: any().
-file("src/glip.gleam", 35).
?DOC(
" Tries to parse an IP address from a string.\n"
" In IPv4, leading zeros or short forms are not allowed.\n"
" This is in order to keep the behavior constant across targets.\n"
).
-spec parse_ip(binary()) -> {ok, ip_address()} | {error, nil}.
parse_ip(Address) ->
glip_ffi:parse_ip(Address).
-file("src/glip.gleam", 44).
?DOC(
" Tires to parse an IP address from a string,\n"
" and on Erlang, preserves the original format.\n"
" This will also be more efficient if the IP needs to be\n"
" represented as a string often on Erlang.\n"
" On JavaScript, this is equivalent to `parse_ip`.\n"
).
-spec parse_ip_preserving_string(binary()) -> {ok, ip_address()} | {error, nil}.
parse_ip_preserving_string(Address) ->
glip_ffi:parse_ip_preserving_string(Address).
-file("src/glip.gleam", 49).
?DOC(" Gets the target-specific external representation of an IP address.\n").
-spec to_external_ip(ip_address()) -> external_ip_address().
to_external_ip(Ip) ->
glip_ffi:to_external_ip(Ip).
-file("src/glip.gleam", 54).
?DOC(" Gets the address family of an IP address.\n").
-spec address_family(ip_address()) -> address_family().
address_family(Ip) ->
glip_ffi:address_family(Ip).
-file("src/glip.gleam", 59).
?DOC(" Converts an IP address to a string.\n").
-spec ip_to_string(ip_address()) -> binary().
ip_to_string(Ip) ->
glip_ffi:to_string(Ip).
-file("src/glip.gleam", 67).
?DOC(
" Adds missing information to partial representations for repeated access.\n"
" On Erlang, fills in the string for addresses coming from external functions or `prase_ip`.\n"
" One JavaScript, resolves the address family for addresses returned from external functions.\n"
" If no information is missing, does nothing.\n"
).
-spec enrich(ip_address()) -> ip_address().
enrich(Ip) ->
glip_ffi:enrich(Ip).