-module(acumen@url). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/acumen/url.gleam"). -export([from_string/1, to_string/1, to_uri/1, decoder/0]). -export_type([url/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( " A URL type that preserves the original server-provided string.\n" "\n" " ACME servers provide URLs in JSON responses and HTTP headers. RFC 8555\n" " Section 6.4 requires the exact server-provided string in signed JWS\n" " headers. This type stores the raw string alongside the parsed URI so\n" " that serialization never alters the URL.\n" ). -opaque url() :: {url, binary(), gleam@uri:uri()}. -file("src/acumen/url.gleam", 24). ?DOC( " Parses a string into a `Url`.\n" "\n" " Succeeds only for absolute HTTPS URLs with a host.\n" ). -spec from_string(binary()) -> {ok, url()} | {error, nil}. from_string(String) -> case gleam_stdlib:uri_parse(String) of {ok, {uri, {some, <<"https"/utf8>>}, _, {some, H}, _, _, _, _} = Parsed} when H =/= <<""/utf8>> -> {ok, {url, String, Parsed}}; _ -> {error, nil} end. -file("src/acumen/url.gleam", 36). ?DOC(" Returns the original string, byte-for-byte as received.\n"). -spec to_string(url()) -> binary(). to_string(Url) -> erlang:element(2, Url). -file("src/acumen/url.gleam", 41). ?DOC(" Returns the parsed URI for component access.\n"). -spec to_uri(url()) -> gleam@uri:uri(). to_uri(Url) -> erlang:element(3, Url). -file("src/acumen/url.gleam", 46). ?DOC(" JSON decoder for ACME URL fields.\n"). -spec decoder() -> gleam@dynamic@decode:decoder(url()). decoder() -> gleam@dynamic@decode:then( {decoder, fun gleam@dynamic@decode:decode_string/1}, fun(String) -> case from_string(String) of {ok, Url} -> gleam@dynamic@decode:success(Url); {error, _} -> gleam@dynamic@decode:failure( {url, <<""/utf8>>, {uri, none, none, none, none, <<""/utf8>>, none, none}}, <<"Url"/utf8>> ) end end ).