-module(oas). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([decoder/1]). -export_type([document/0, info/0, path_item/0, components/0, parameter/0, operation/0, request_body/0, status/0, response/0, header/0, media_type/0, schema/0]). -type document() :: {document, binary(), info(), gleam@dict:dict(binary(), path_item()), components()}. -type info() :: {info, binary()}. -type path_item() :: {path_item, gleam@option:option(binary()), gleam@option:option(binary()), list(parameter()), list({gleam@http:method(), operation()})}. -type components() :: {components, gleam@dict:dict(binary(), schema())}. -type parameter() :: {query_parameter, binary(), gleam@option:option(binary()), boolean(), schema()} | {path_parameter, binary(), schema()} | {header_parameter, binary(), gleam@option:option(binary()), boolean(), schema()} | {cookie_parameter, binary(), gleam@option:option(binary()), boolean(), schema()}. -type operation() :: {operation, list(binary()), gleam@option:option(binary()), gleam@option:option(binary()), binary(), list(parameter()), gleam@option:option(request_body()), gleam@dict:dict(status(), response())}. -type request_body() :: {request_body, gleam@option:option(binary()), gleam@dict:dict(binary(), media_type()), boolean()}. -type status() :: default | {status, integer()}. -type response() :: {response, binary(), gleam@dict:dict(binary(), header()), gleam@option:option(gleam@dict:dict(binary(), media_type()))}. -type header() :: {header, gleam@option:option(binary()), boolean(), schema()}. -type media_type() :: {media_type, schema()}. -type schema() :: boolean | integer | number | string | {array, schema()} | {object, gleam@dict:dict(binary(), schema())}. -file("/home/peter/Projects/crowdhailer/mono-2024/oas/src/oas.gleam", 35). -spec info_decoder(gleam@dynamic:dynamic_()) -> {ok, info()} | {error, list(gleam@dynamic:decode_error())}. info_decoder(Raw) -> (gleam@dynamic:decode1( fun(Field@0) -> {info, Field@0} end, gleam@dynamic:field(<<"version"/utf8>>, fun gleam@dynamic:string/1) ))(Raw). -file("/home/peter/Projects/crowdhailer/mono-2024/oas/src/oas.gleam", 241). -spec status_decoder(gleam@dynamic:dynamic_()) -> {ok, status()} | {error, list(gleam@dynamic:decode_error())}. status_decoder(Raw) -> gleam@result:'try'(gleam@dynamic:string(Raw), fun(Key) -> case Key of <<"default"/utf8>> -> {ok, default}; Key@1 -> case gleam@int:parse(Key@1) of {ok, I} -> {ok, {status, I}}; {error, nil} -> {error, [{decode_error, <<"integer"/utf8>>, Key@1, []}]} end end end). -file("/home/peter/Projects/crowdhailer/mono-2024/oas/src/oas.gleam", 357). -spec follow_path(list(any()), gleam@dynamic:dynamic_()) -> {ok, gleam@dynamic:dynamic_()} | {error, list(gleam@dynamic:decode_error())}. follow_path(Path, Top) -> gleam@list:try_fold( Path, Top, fun(Spec, Item) -> (gleam@dynamic:field(Item, fun(Field@0) -> {ok, Field@0} end))(Spec) end ). -file("/home/peter/Projects/crowdhailer/mono-2024/oas/src/oas.gleam", 361). -spec follow_if_ref(gleam@dynamic:dynamic_(), gleam@dynamic:dynamic_()) -> {ok, gleam@dynamic:dynamic_()} | {error, list(gleam@dynamic:decode_error())}. follow_if_ref(Raw, Top) -> case (gleam@dynamic:field(<<"$ref"/utf8>>, fun gleam@dynamic:string/1))(Raw) of {ok, Ref} -> Path = gleam@string:split(Ref, <<"/"/utf8>>), case Path of [<<"#"/utf8>> | Path@1] -> _assert_subject = follow_path(Path@1, Top), {ok, Spec} = case _assert_subject of {ok, _} -> _assert_subject; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, value => _assert_fail, module => <<"oas"/utf8>>, function => <<"follow_if_ref"/utf8>>, line => 367}) end, {ok, Spec}; _ -> {error, [{decode_error, <<"local ref path"/utf8>>, Ref, []}]} end; {error, _} -> {ok, Raw} end. -file("/home/peter/Projects/crowdhailer/mono-2024/oas/src/oas.gleam", 377). -spec with_default( fun((HIG) -> {ok, gleam@option:option(HEF)} | {error, HIO}), HEF ) -> fun((HIG) -> {ok, HEF} | {error, HIO}). with_default(Decoder, Value) -> fun(Raw) -> gleam@result:'try'( Decoder(Raw), fun(Decoded) -> {ok, gleam@option:unwrap(Decoded, Value)} end ) end. -file("/home/peter/Projects/crowdhailer/mono-2024/oas/src/oas.gleam", 349). -spec decode_properties(gleam@dynamic:dynamic_(), gleam@dynamic:dynamic_()) -> {ok, gleam@dict:dict(binary(), schema())} | {error, list(gleam@dynamic:decode_error())}. decode_properties(Raw, Top) -> gleam@result:'try'( (gleam@dynamic:optional_field( <<"properties"/utf8>>, gleam@dynamic:dict( fun gleam@dynamic:string/1, fun(_capture) -> schema_decoder(_capture, Top) end ) ))(Raw), fun(Properties) -> {ok, gleam@option:unwrap(Properties, gleam@dict:new())} end ). -file("/home/peter/Projects/crowdhailer/mono-2024/oas/src/oas.gleam", 311). -spec schema_decoder(gleam@dynamic:dynamic_(), gleam@dynamic:dynamic_()) -> {ok, schema()} | {error, list(gleam@dynamic:decode_error())}. schema_decoder(Raw, Top) -> gleam@result:'try'( follow_if_ref(Raw, Top), fun(Content) -> (gleam@dynamic:any( [gleam@dynamic:field( <<"type"/utf8>>, fun(Field) -> gleam@result:'try'( gleam@dynamic:string(Field), fun(Type_) -> case Type_ of <<"boolean"/utf8>> -> {ok, boolean}; <<"integer"/utf8>> -> {ok, integer}; <<"number"/utf8>> -> {ok, number}; <<"string"/utf8>> -> {ok, string}; <<"array"/utf8>> -> gleam@result:'try'( (gleam@dynamic:field( <<"items"/utf8>>, fun(_capture) -> schema_decoder( _capture, Top ) end ))(Content), fun(Items) -> {ok, {array, Items}} end ); <<"object"/utf8>> -> decode_object(Content, Top); _ -> {error, [{decode_error, <<"json type"/utf8>>, Type_, []}]} end end ) end ), gleam@dynamic:field( <<"allOf"/utf8>>, fun(Raw@1) -> gleam@result:'try'( (gleam@dynamic:list( fun(Raw@2) -> gleam@result:'try'( follow_if_ref(Raw@2, Top), fun(Content@1) -> decode_properties( Content@1, Top ) end ) end ))(Raw@1), fun(Elements) -> _assert_subject = gleam@list:reduce( Elements, fun gleam@dict:merge/2 ), {ok, Properties} = case _assert_subject of {ok, _} -> _assert_subject; _assert_fail -> erlang:error( #{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, value => _assert_fail, module => <<"oas"/utf8>>, function => <<"schema_decoder"/utf8>>, line => 339} ) end, {ok, {object, Properties}} end ) end )] ))(Content) end ). -file("/home/peter/Projects/crowdhailer/mono-2024/oas/src/oas.gleam", 98). -spec components_decoder(gleam@dynamic:dynamic_(), gleam@dynamic:dynamic_()) -> {ok, components()} | {error, list(gleam@dynamic:decode_error())}. components_decoder(Raw, Top) -> (gleam@dynamic:decode1( fun(Field@0) -> {components, Field@0} end, gleam@dynamic:field( <<"schemas"/utf8>>, gleam@dynamic:dict( fun gleam@dynamic:string/1, fun(_capture) -> schema_decoder(_capture, Top) end ) ) ))(Raw). -file("/home/peter/Projects/crowdhailer/mono-2024/oas/src/oas.gleam", 130). -spec parameter_decoder(gleam@dynamic:dynamic_(), gleam@dynamic:dynamic_()) -> {ok, parameter()} | {error, list(gleam@dynamic:decode_error())}. parameter_decoder(Raw, Top) -> gleam@result:'try'( follow_if_ref(Raw, Top), fun(Content) -> (gleam@dynamic:any( [gleam@dynamic:field( <<"in"/utf8>>, fun(Field) -> gleam@result:'try'( gleam@dynamic:string(Field), fun(In) -> case In of <<"query"/utf8>> -> (gleam@dynamic:decode4( fun(Field@0, Field@1, Field@2, Field@3) -> {query_parameter, Field@0, Field@1, Field@2, Field@3} end, gleam@dynamic:field( <<"name"/utf8>>, fun gleam@dynamic:string/1 ), gleam@dynamic:optional_field( <<"description"/utf8>>, fun gleam@dynamic:string/1 ), begin _pipe = gleam@dynamic:optional_field( <<"required"/utf8>>, fun gleam@dynamic:bool/1 ), with_default(_pipe, false) end, gleam@dynamic:field( <<"schema"/utf8>>, fun(_capture) -> schema_decoder( _capture, Top ) end ) ))(Content); <<"header"/utf8>> -> (gleam@dynamic:decode4( fun(Field@0, Field@1, Field@2, Field@3) -> {header_parameter, Field@0, Field@1, Field@2, Field@3} end, gleam@dynamic:field( <<"name"/utf8>>, fun gleam@dynamic:string/1 ), gleam@dynamic:optional_field( <<"description"/utf8>>, fun gleam@dynamic:string/1 ), begin _pipe@1 = gleam@dynamic:optional_field( <<"required"/utf8>>, fun gleam@dynamic:bool/1 ), with_default(_pipe@1, false) end, gleam@dynamic:field( <<"schema"/utf8>>, fun(_capture@1) -> schema_decoder( _capture@1, Top ) end ) ))(Content); <<"path"/utf8>> -> (gleam@dynamic:decode2( fun(Field@0, Field@1) -> {path_parameter, Field@0, Field@1} end, gleam@dynamic:field( <<"name"/utf8>>, fun gleam@dynamic:string/1 ), gleam@dynamic:field( <<"schema"/utf8>>, fun(_capture@2) -> schema_decoder( _capture@2, Top ) end ) ))(Content); <<"cookie"/utf8>> -> (gleam@dynamic:decode4( fun(Field@0, Field@1, Field@2, Field@3) -> {header_parameter, Field@0, Field@1, Field@2, Field@3} end, gleam@dynamic:field( <<"name"/utf8>>, fun gleam@dynamic:string/1 ), gleam@dynamic:optional_field( <<"description"/utf8>>, fun gleam@dynamic:string/1 ), begin _pipe@2 = gleam@dynamic:optional_field( <<"required"/utf8>>, fun gleam@dynamic:bool/1 ), with_default(_pipe@2, false) end, gleam@dynamic:field( <<"schema"/utf8>>, fun(_capture@3) -> schema_decoder( _capture@3, Top ) end ) ))(Content); _ -> {error, [{decode_error, <<"valid in field"/utf8>>, In, []}]} end end ) end )] ))(Content) end ). -file("/home/peter/Projects/crowdhailer/mono-2024/oas/src/oas.gleam", 281). -spec decode_header(gleam@dynamic:dynamic_(), gleam@dynamic:dynamic_()) -> {ok, header()} | {error, list(gleam@dynamic:decode_error())}. decode_header(Raw, Top) -> gleam@result:'try'( follow_if_ref(Raw, Top), fun(Content) -> (gleam@dynamic:decode3( fun(Field@0, Field@1, Field@2) -> {header, Field@0, Field@1, Field@2} end, gleam@dynamic:optional_field( <<"description"/utf8>>, fun gleam@dynamic:string/1 ), begin _pipe = gleam@dynamic:optional_field( <<"required"/utf8>>, fun gleam@dynamic:bool/1 ), with_default(_pipe, false) end, gleam@dynamic:field( <<"schema"/utf8>>, fun(_capture) -> schema_decoder(_capture, Top) end ) ))(Content) end ). -file("/home/peter/Projects/crowdhailer/mono-2024/oas/src/oas.gleam", 296). -spec media_type_decoder(gleam@dynamic:dynamic_(), gleam@dynamic:dynamic_()) -> {ok, media_type()} | {error, list(gleam@dynamic:decode_error())}. media_type_decoder(Raw, Top) -> (gleam@dynamic:decode1( fun(Field@0) -> {media_type, Field@0} end, gleam@dynamic:field( <<"schema"/utf8>>, fun(_capture) -> schema_decoder(_capture, Top) end ) ))(Raw). -file("/home/peter/Projects/crowdhailer/mono-2024/oas/src/oas.gleam", 232). -spec content_decoder(gleam@dynamic:dynamic_(), gleam@dynamic:dynamic_()) -> {ok, gleam@dict:dict(binary(), media_type())} | {error, list(gleam@dynamic:decode_error())}. content_decoder(Raw, Top) -> (gleam@dynamic:dict( fun gleam@dynamic:string/1, fun(_capture) -> media_type_decoder(_capture, Top) end ))(Raw). -file("/home/peter/Projects/crowdhailer/mono-2024/oas/src/oas.gleam", 221). -spec request_body_decoder(gleam@dynamic:dynamic_(), gleam@dynamic:dynamic_()) -> {ok, request_body()} | {error, list(gleam@dynamic:decode_error())}. request_body_decoder(Raw, Top) -> gleam@result:'try'( follow_if_ref(Raw, Top), fun(Content) -> (gleam@dynamic:decode3( fun(Field@0, Field@1, Field@2) -> {request_body, Field@0, Field@1, Field@2} end, gleam@dynamic:optional_field( <<"description"/utf8>>, fun gleam@dynamic:string/1 ), gleam@dynamic:field( <<"content"/utf8>>, fun(_capture) -> content_decoder(_capture, Top) end ), begin _pipe = gleam@dynamic:optional_field( <<"required"/utf8>>, fun gleam@dynamic:bool/1 ), with_default(_pipe, false) end ))(Content) end ). -file("/home/peter/Projects/crowdhailer/mono-2024/oas/src/oas.gleam", 261). -spec response_decoder(gleam@dynamic:dynamic_(), gleam@dynamic:dynamic_()) -> {ok, response()} | {error, list(gleam@dynamic:decode_error())}. response_decoder(Raw, Top) -> gleam@result:'try'( follow_if_ref(Raw, Top), fun(Content) -> (gleam@dynamic:decode3( fun(Field@0, Field@1, Field@2) -> {response, Field@0, Field@1, Field@2} end, gleam@dynamic:field( <<"description"/utf8>>, fun gleam@dynamic:string/1 ), fun(Raw@1) -> gleam@result:'try'( (gleam@dynamic:optional_field( <<"headers"/utf8>>, gleam@dynamic:dict( fun gleam@dynamic:string/1, fun(_capture) -> decode_header(_capture, Top) end ) ))(Raw@1), fun(Headers) -> {ok, gleam@option:unwrap(Headers, gleam@dict:new())} end ) end, gleam@dynamic:optional_field( <<"content"/utf8>>, fun(_capture@1) -> content_decoder(_capture@1, Top) end ) ))(Content) end ). -file("/home/peter/Projects/crowdhailer/mono-2024/oas/src/oas.gleam", 190). -spec operation_decoder(gleam@dynamic:dynamic_(), gleam@dynamic:dynamic_()) -> {ok, operation()} | {error, list(gleam@dynamic:decode_error())}. operation_decoder(Raw, Top) -> (gleam@dynamic:decode7( fun(Field@0, Field@1, Field@2, Field@3, Field@4, Field@5, Field@6) -> {operation, Field@0, Field@1, Field@2, Field@3, Field@4, Field@5, Field@6} end, gleam@dynamic:field( <<"tags"/utf8>>, gleam@dynamic:list(fun gleam@dynamic:string/1) ), gleam@dynamic:optional_field( <<"summary"/utf8>>, fun gleam@dynamic:string/1 ), gleam@dynamic:optional_field( <<"description"/utf8>>, fun gleam@dynamic:string/1 ), gleam@dynamic:field(<<"operationId"/utf8>>, fun gleam@dynamic:string/1), fun(Raw@1) -> gleam@result:'try'( (gleam@dynamic:optional_field( <<"parameters"/utf8>>, gleam@dynamic:list( fun(_capture) -> parameter_decoder(_capture, Top) end ) ))(Raw@1), fun(Maybe_parameters) -> Parameters = gleam@option:unwrap(Maybe_parameters, []), {ok, Parameters} end ) end, gleam@dynamic:optional_field( <<"requestBody"/utf8>>, fun(_capture@1) -> request_body_decoder(_capture@1, Top) end ), gleam@dynamic:field( <<"responses"/utf8>>, gleam@dynamic:dict( fun status_decoder/1, fun(_capture@2) -> response_decoder(_capture@2, Top) end ) ) ))(Raw). -file("/home/peter/Projects/crowdhailer/mono-2024/oas/src/oas.gleam", 53). -spec path_decoder(gleam@dynamic:dynamic_(), gleam@dynamic:dynamic_()) -> {ok, path_item()} | {error, list(gleam@dynamic:decode_error())}. path_decoder(Raw, Top) -> gleam@result:'try'( (gleam@dynamic:optional_field( <<"parameters"/utf8>>, gleam@dynamic:list( fun(_capture) -> parameter_decoder(_capture, Top) end ) ))(Raw), fun(Maybe_parameters) -> Parameters = gleam@option:unwrap(Maybe_parameters, []), gleam@result:'try'( begin _pipe = [{<<"get"/utf8>>, get}, {<<"put"/utf8>>, put}, {<<"post"/utf8>>, post}, {<<"delete"/utf8>>, delete}, {<<"options"/utf8>>, options}, {<<"head"/utf8>>, head}, {<<"patch"/utf8>>, patch}, {<<"trace"/utf8>>, trace}], _pipe@1 = gleam@list:map( _pipe, fun(Lookup) -> {Key, Method} = Lookup, gleam@result:'try'( (gleam@dynamic:optional_field( Key, fun(_capture@1) -> operation_decoder(_capture@1, Top) end ))(Raw), fun(Maybe) -> {ok, {Method, Maybe}} end ) end ), gleam@result:all(_pipe@1) end, fun(Maybe_operations) -> Operations = gleam@list:filter_map( Maybe_operations, fun(Maybe@1) -> {Method@1, Maybe@2} = Maybe@1, case Maybe@2 of {some, Value} -> {ok, {Method@1, Value}}; none -> {error, nil} end end ), gleam@result:'try'( (gleam@dynamic:optional_field( <<"summary"/utf8>>, fun gleam@dynamic:string/1 ))(Raw), fun(Summary) -> gleam@result:'try'( (gleam@dynamic:optional_field( <<"description"/utf8>>, fun gleam@dynamic:string/1 ))(Raw), fun(Description) -> {ok, {path_item, Summary, Description, Parameters, Operations}} end ) end ) end ) end ). -file("/home/peter/Projects/crowdhailer/mono-2024/oas/src/oas.gleam", 49). -spec paths_decoder(gleam@dynamic:dynamic_(), gleam@dynamic:dynamic_()) -> {ok, gleam@dict:dict(binary(), path_item())} | {error, list(gleam@dynamic:decode_error())}. paths_decoder(Raw, Top) -> (gleam@dynamic:dict( fun gleam@dynamic:string/1, fun(_capture) -> path_decoder(_capture, Top) end ))(Raw). -file("/home/peter/Projects/crowdhailer/mono-2024/oas/src/oas.gleam", 20). -spec decoder(gleam@dynamic:dynamic_()) -> {ok, document()} | {error, list(gleam@dynamic:decode_error())}. decoder(Top) -> (gleam@dynamic:decode4( fun(Field@0, Field@1, Field@2, Field@3) -> {document, Field@0, Field@1, Field@2, Field@3} end, gleam@dynamic:field(<<"openapi"/utf8>>, fun gleam@dynamic:string/1), gleam@dynamic:field(<<"info"/utf8>>, fun info_decoder/1), gleam@dynamic:field( <<"paths"/utf8>>, fun(_capture) -> paths_decoder(_capture, Top) end ), gleam@dynamic:field( <<"components"/utf8>>, fun(_capture@1) -> components_decoder(_capture@1, Top) end ) ))(Top). -file("/home/peter/Projects/crowdhailer/mono-2024/oas/src/oas.gleam", 345). -spec decode_object(gleam@dynamic:dynamic_(), gleam@dynamic:dynamic_()) -> {ok, schema()} | {error, list(gleam@dynamic:decode_error())}. decode_object(Raw, Top) -> (gleam@dynamic:decode1( fun(Field@0) -> {object, Field@0} end, fun(_capture) -> decode_properties(_capture, Top) end ))(Raw).