Packages

Emit an OpenAPI 3.1.0 document from atproto lexicons.

Current section

Files

Jump to
atproto_openapi src atproto_openapi.erl
Raw

src/atproto_openapi.erl

-module(atproto_openapi).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/atproto_openapi.gleam").
-export([generate/2, main/0]).
-export_type([options/0, generated/0, 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.
?MODULEDOC(
" Public API: turn a set of parsed `atproto_lexicon/ast.LexiconDoc`\n"
" values into a single OpenAPI 3.1.0 document. See\n"
" `docs/lexicon-openapi.md` for the mapping this implements and\n"
" `atproto_openapi/emit` for the assembly logic this delegates to.\n"
" `main` runs the CLI (`atproto_openapi/cli`), which walks a directory of\n"
" lexicon JSON files and writes the generated document to disk.\n"
).
-type options() :: {options, binary(), binary(), list(binary())}.
-type generated() :: {generated, binary(), integer(), integer(), list(binary())}.
-type error() :: {ref_error, atproto_openapi@refs:ref_error()}.
-file("src/atproto_openapi.gleam", 38).
?DOC(
" Assemble `docs` into one OpenAPI 3.1.0 document. Fails only when a ref\n"
" is malformed or two distinct lexicon refs derive the same\n"
" `components/schemas` key; see `atproto_openapi/refs`. Every other input\n"
" shape has a defined mapping per the design doc.\n"
).
-spec generate(list(atproto_lexicon@ast:lexicon_doc()), options()) -> {ok,
generated()} |
{error, error()}.
generate(Docs, Options) ->
_pipe = atproto_openapi@emit:assemble(
Docs,
erlang:element(2, Options),
erlang:element(3, Options),
erlang:element(4, Options)
),
_pipe@1 = gleam@result:map(
_pipe,
fun(Document) ->
{generated,
gleam@json:to_string(erlang:element(2, Document)),
erlang:element(3, Document),
erlang:element(4, Document),
erlang:element(5, Document)}
end
),
gleam@result:map_error(_pipe@1, fun(Field@0) -> {ref_error, Field@0} end).
-file("src/atproto_openapi.gleam", 56).
?DOC(
" `gleam run -m atproto_openapi -- <lexicons-dir> <out-file> [--title T]\n"
" [--version V] [--server URL]`. See `atproto_openapi/cli`.\n"
).
-spec main() -> nil.
main() ->
atproto_openapi@cli:main().