Skip to main content

src/aws@internal@text_scan.erl

-module(aws@internal@text_scan).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/aws/internal/text_scan.gleam").
-export([xml_tag_text/2, json_string_after_key/2]).

-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(
    " Tiny text-extraction helpers shared by the credential providers\n"
    " and the runtime's error parser. Both shapes — XML `<Tag>X</Tag>`\n"
    " and JSON `\"key\": \"value\"` — appear in places where dragging in a\n"
    " full XML / JSON parser would be over-investment.\n"
    "\n"
    " `xml_tag_text` and `json_string_after_key` each scan once with two\n"
    " `split_once` calls; neither validates the surrounding document. Use\n"
    " them only on inputs whose structure you trust (STS / SSO responses,\n"
    " error envelopes), never on arbitrary user-supplied text.\n"
).

-file("src/aws/internal/text_scan.gleam", 18).
?DOC(
    " Pull the text content between the first `<tag>` and its matching\n"
    " `</tag>`. Returns `Error(Nil)` if either tag is absent. Used by the\n"
    " STS providers (`<AccessKeyId>...</AccessKeyId>`) and the runtime's\n"
    " restXml error path (`<Code>NoSuchBucket</Code>`).\n"
).
-spec xml_tag_text(binary(), binary()) -> {ok, binary()} | {error, nil}.
xml_tag_text(Text, Tag) ->
    Open = <<<<"<"/utf8, Tag/binary>>/binary, ">"/utf8>>,
    Close = <<<<"</"/utf8, Tag/binary>>/binary, ">"/utf8>>,
    gleam@result:'try'(
        gleam@string:split_once(Text, Open),
        fun(_use0) ->
            {_, After_open} = _use0,
            gleam@result:'try'(
                gleam@string:split_once(After_open, Close),
                fun(_use0@1) ->
                    {Content, _} = _use0@1,
                    {ok, Content}
                end
            )
        end
    ).

-file("src/aws/internal/text_scan.gleam", 35).
?DOC(
    " Pull the string value that follows a quoted JSON `\"key\"`. Skips the\n"
    " first quote after the key (i.e. tolerates `\"key\": \"value\"`,\n"
    " `\"key\":\"value\"`, `\"key\" : \"value\"`), then reads the next two\n"
    " quotes as the value's delimiters. Returns `Error(Nil)` if the key\n"
    " is absent or the value isn't a string-typed JSON field.\n"
    "\n"
    " The function deliberately doesn't unescape backslash sequences in\n"
    " the value — callers either don't expect them (STS tokens, error\n"
    " codes) or do their own decoding.\n"
).
-spec json_string_after_key(binary(), binary()) -> {ok, binary()} | {error, nil}.
json_string_after_key(Text, Key) ->
    Needle = <<<<"\""/utf8, Key/binary>>/binary, "\""/utf8>>,
    gleam@result:'try'(
        gleam@string:split_once(Text, Needle),
        fun(_use0) ->
            {_, After_key} = _use0,
            gleam@result:'try'(
                gleam@string:split_once(After_key, <<"\""/utf8>>),
                fun(_use0@1) ->
                    {_, After_first_quote} = _use0@1,
                    gleam@result:'try'(
                        gleam@string:split_once(
                            After_first_quote,
                            <<"\""/utf8>>
                        ),
                        fun(_use0@2) ->
                            {Value, _} = _use0@2,
                            {ok, Value}
                        end
                    )
                end
            )
        end
    ).