-module(mongo@cursor). -compile([no_auto_import, nowarn_unused_vars]). -export([new/3, next/1, to_list/1]). -export_type([cursor/0]). -opaque cursor() :: {cursor, mongo@client:collection(), integer(), integer(), gleam@iterator:iterator(bson@value:value())}. -spec new(mongo@client:collection(), integer(), list(bson@value:value())) -> cursor(). new(Collection, Id, Batch) -> {cursor, Collection, Id, gleam@list:length(Batch), gleam@iterator:from_list(Batch)}. -spec get_more(cursor()) -> {ok, cursor()} | {error, mongo@utils:mongo_error()}. get_more(Cursor) -> case mongo@client:get_more( erlang:element(2, Cursor), erlang:element(3, Cursor), erlang:element(4, Cursor) ) of {ok, Result} -> [{<<"cursor"/utf8>>, {document, Result@1}}, {<<"ok"/utf8>>, Ok}] = Result, _assert_subject = gleam@list:key_find(Result@1, <<"id"/utf8>>), {ok, {int64, Id}} = case _assert_subject of {ok, {int64, _}} -> _assert_subject; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Assertion pattern match failed"/utf8>>, value => _assert_fail, module => <<"mongo/cursor"/utf8>>, function => <<"get_more"/utf8>>, line => 76}) end, _assert_subject@1 = gleam@list:key_find( Result@1, <<"nextBatch"/utf8>> ), {ok, {array, Batch}} = case _assert_subject@1 of {ok, {array, _}} -> _assert_subject@1; _assert_fail@1 -> erlang:error(#{gleam_error => let_assert, message => <<"Assertion pattern match failed"/utf8>>, value => _assert_fail@1, module => <<"mongo/cursor"/utf8>>, function => <<"get_more"/utf8>>, line => 77}) end, case Ok of {double, 1.0} -> _pipe = new(erlang:element(2, Cursor), Id, Batch), {ok, _pipe}; _ -> {error, {mongo_error, -16, <<""/utf8>>, null}} end; {error, {Code, Msg}} -> {error, {mongo_error, Code, Msg, null}} end. -spec next(cursor()) -> {gleam@option:option(bson@value:value()), cursor()}. next(Cursor) -> case gleam@iterator:step(erlang:element(5, Cursor)) of {next, Doc, Rest} -> {{some, Doc}, {cursor, erlang:element(2, Cursor), erlang:element(3, Cursor), erlang:element(4, Cursor), Rest}}; done -> case erlang:element(3, Cursor) of 0 -> {none, {cursor, erlang:element(2, Cursor), 0, erlang:element(4, Cursor), gleam@iterator:empty()}}; _ -> _assert_subject = get_more(Cursor), {ok, New_cursor} = case _assert_subject of {ok, _} -> _assert_subject; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Assertion pattern match failed"/utf8>>, value => _assert_fail, module => <<"mongo/cursor"/utf8>>, function => <<"next"/utf8>>, line => 34}) end, case gleam@iterator:step(erlang:element(5, New_cursor)) of {next, Doc@1, Rest@1} -> {{some, Doc@1}, {cursor, erlang:element(2, Cursor), erlang:element(3, New_cursor), erlang:element(4, New_cursor), Rest@1}}; done -> {none, {cursor, erlang:element(2, Cursor), erlang:element(3, New_cursor), erlang:element(4, New_cursor), gleam@iterator:empty()}} end end end. -spec to_list_internal(cursor(), list(bson@value:value())) -> list(bson@value:value()). to_list_internal(Cursor, Storage) -> case next(Cursor) of {{some, Next}, New_cursor} -> to_list_internal(New_cursor, gleam@list:append(Storage, [Next])); {none, _} -> Storage end. -spec to_list(cursor()) -> list(bson@value:value()). to_list(Cursor) -> to_list_internal(Cursor, []).