-module(multipartkit@query). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/multipartkit/query.gleam"). -export([names/1, fields/2, field/2, required_field/2, file/2, required_file/2, files/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. -file("src/multipartkit/query.gleam", 86). -spec collect_names( list(multipartkit@part:part()), list(binary()), list(binary()) ) -> list(binary()). collect_names(Remaining, Seen, Acc) -> case Remaining of [] -> lists:reverse(Acc); [The_part | Rest] -> case erlang:element(3, The_part) of none -> collect_names(Rest, Seen, Acc); {some, Name} -> case gleam@list:contains(Seen, Name) of true -> collect_names(Rest, Seen, Acc); false -> collect_names(Rest, [Name | Seen], [Name | Acc]) end end end. -file("src/multipartkit/query.gleam", 82). ?DOC( " Distinct field names preserving first-appearance order. Parts with\n" " `name == None` are skipped.\n" ). -spec names(list(multipartkit@part:part())) -> list(binary()). names(Parts) -> collect_names(Parts, [], []). -file("src/multipartkit/query.gleam", 112). -spec is_text_field_named(multipartkit@part:part(), binary()) -> boolean(). is_text_field_named(The_part, Name) -> case {erlang:element(3, The_part), erlang:element(4, The_part)} of {{some, Value}, none} -> Value =:= Name; {_, _} -> false end. -file("src/multipartkit/query.gleam", 49). ?DOC( " Return all text fields with the given `name` in input order. Parts whose\n" " body is not valid UTF-8 are silently skipped. For strict semantics,\n" " iterate manually with `required_field`.\n" ). -spec fields(list(multipartkit@part:part()), binary()) -> list(binary()). fields(Parts, Name) -> _pipe = Parts, _pipe@1 = gleam@list:filter( _pipe, fun(P) -> is_text_field_named(P, Name) end ), gleam@list:filter_map( _pipe@1, fun(P@1) -> gleam@bit_array:to_string(erlang:element(6, P@1)) end ). -file("src/multipartkit/query.gleam", 105). -spec find_text_field(list(multipartkit@part:part()), binary()) -> gleam@option:option(multipartkit@part:part()). find_text_field(Parts, Name) -> case gleam@list:find(Parts, fun(P) -> is_text_field_named(P, Name) end) of {ok, Found_part} -> {some, Found_part}; {error, nil} -> none end. -file("src/multipartkit/query.gleam", 19). ?DOC( " Return the body of the first text field with the given `name`, decoded as\n" " UTF-8.\n" "\n" " A part counts as a *text field* iff its `Content-Disposition` is\n" " `form-data` with a `name` parameter and **no** `filename` parameter.\n" " Parts whose `name` is `None` are skipped.\n" "\n" " If the first matching text field exists but its body is not valid UTF-8,\n" " `None` is returned. Use `required_field` if you need to distinguish\n" " \"missing\" from \"present but not UTF-8\".\n" ). -spec field(list(multipartkit@part:part()), binary()) -> gleam@option:option(binary()). field(Parts, Name) -> case find_text_field(Parts, Name) of none -> none; {some, Found_part} -> case gleam@bit_array:to_string(erlang:element(6, Found_part)) of {ok, Value} -> {some, Value}; {error, nil} -> none end end. -file("src/multipartkit/query.gleam", 32). ?DOC( " Strict variant of `field`. Returns `MissingField` if no text field\n" " matches; returns `InvalidUtf8Field` if the body is not valid UTF-8.\n" ). -spec required_field(list(multipartkit@part:part()), binary()) -> {ok, binary()} | {error, multipartkit@error:multipart_error()}. required_field(Parts, Name) -> case find_text_field(Parts, Name) of none -> {error, {missing_field, Name}}; {some, Found_part} -> case gleam@bit_array:to_string(erlang:element(6, Found_part)) of {ok, Value} -> {ok, Value}; {error, nil} -> {error, {invalid_utf8_field, Name}} end end. -file("src/multipartkit/query.gleam", 119). -spec is_file_named(multipartkit@part:part(), binary()) -> boolean(). is_file_named(The_part, Name) -> case {erlang:element(3, The_part), erlang:element(4, The_part)} of {{some, Value}, {some, _}} -> Value =:= Name; {_, _} -> false end. -file("src/multipartkit/query.gleam", 57). ?DOC( " First file part with the given `name`. `filename = Some(\"\")` still counts\n" " as a file (the unselected `` case).\n" ). -spec file(list(multipartkit@part:part()), binary()) -> gleam@option:option(multipartkit@part:part()). file(Parts, Name) -> case gleam@list:find(Parts, fun(P) -> is_file_named(P, Name) end) of {ok, Found_part} -> {some, Found_part}; {error, nil} -> none end. -file("src/multipartkit/query.gleam", 65). ?DOC(" Strict variant of `file`. Returns `MissingFile` if no file part matches.\n"). -spec required_file(list(multipartkit@part:part()), binary()) -> {ok, multipartkit@part:part()} | {error, multipartkit@error:multipart_error()}. required_file(Parts, Name) -> case file(Parts, Name) of {some, Found_part} -> {ok, Found_part}; none -> {error, {missing_file, Name}} end. -file("src/multipartkit/query.gleam", 76). ?DOC(" All file parts with the given `name` in input order.\n"). -spec files(list(multipartkit@part:part()), binary()) -> list(multipartkit@part:part()). files(Parts, Name) -> gleam@list:filter(Parts, fun(P) -> is_file_named(P, Name) end).