mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 14:57:35 +00:00
LibIMAP+Mail+test-imap: Let a Promise result type be non-optional
The Empty type was used only when parsing a response failed. The failures will now go to Promise's error type.
This commit is contained in:
parent
1f605407bd
commit
90af21aef4
4 changed files with 71 additions and 72 deletions
|
@ -171,12 +171,12 @@ ErrorOr<void> Client::send_raw(StringView data)
|
|||
return {};
|
||||
}
|
||||
|
||||
NonnullRefPtr<Promise<Optional<Response>>> Client::send_command(Command&& command)
|
||||
NonnullRefPtr<Promise<Response>> Client::send_command(Command&& command)
|
||||
{
|
||||
m_command_queue.append(move(command));
|
||||
m_current_command++;
|
||||
|
||||
auto promise = Promise<Optional<Response>>::construct();
|
||||
auto promise = Promise<Response>::construct();
|
||||
m_pending_promises.append(promise);
|
||||
|
||||
if (m_pending_promises.size() == 1) {
|
||||
|
@ -189,22 +189,22 @@ NonnullRefPtr<Promise<Optional<Response>>> Client::send_command(Command&& comman
|
|||
}
|
||||
|
||||
template<typename T>
|
||||
NonnullRefPtr<Promise<Optional<T>>> cast_promise(NonnullRefPtr<Promise<Optional<Response>>> promise_variant)
|
||||
NonnullRefPtr<Promise<T>> cast_promise(NonnullRefPtr<Promise<Response>> promise_variant)
|
||||
{
|
||||
auto new_promise = promise_variant->map<Optional<T>>(
|
||||
[](Optional<Response>& variant) {
|
||||
return move(variant->get<T>());
|
||||
auto new_promise = promise_variant->map<T>(
|
||||
[](Response& variant) {
|
||||
return move(variant.get<T>());
|
||||
});
|
||||
return new_promise;
|
||||
}
|
||||
|
||||
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::login(StringView username, StringView password)
|
||||
NonnullRefPtr<Promise<SolidResponse>> Client::login(StringView username, StringView password)
|
||||
{
|
||||
auto command = Command { CommandType::Login, m_current_command, { serialize_astring(username), serialize_astring(password) } };
|
||||
return cast_promise<SolidResponse>(send_command(move(command)));
|
||||
}
|
||||
|
||||
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::list(StringView reference_name, StringView mailbox)
|
||||
NonnullRefPtr<Promise<SolidResponse>> Client::list(StringView reference_name, StringView mailbox)
|
||||
{
|
||||
auto command = Command { CommandType::List, m_current_command,
|
||||
{ DeprecatedString::formatted("\"{}\"", reference_name),
|
||||
|
@ -212,7 +212,7 @@ NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::list(StringView referenc
|
|||
return cast_promise<SolidResponse>(send_command(move(command)));
|
||||
}
|
||||
|
||||
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::lsub(StringView reference_name, StringView mailbox)
|
||||
NonnullRefPtr<Promise<SolidResponse>> Client::lsub(StringView reference_name, StringView mailbox)
|
||||
{
|
||||
auto command = Command { CommandType::ListSub, m_current_command,
|
||||
{ DeprecatedString::formatted("\"{}\"", reference_name),
|
||||
|
@ -220,19 +220,19 @@ NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::lsub(StringView referenc
|
|||
return cast_promise<SolidResponse>(send_command(move(command)));
|
||||
}
|
||||
|
||||
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::fetch(FetchCommand request, bool uid)
|
||||
NonnullRefPtr<Promise<SolidResponse>> Client::fetch(FetchCommand request, bool uid)
|
||||
{
|
||||
auto command = Command { uid ? CommandType::UIDFetch : CommandType::Fetch, m_current_command, { request.serialize() } };
|
||||
return cast_promise<SolidResponse>(send_command(move(command)));
|
||||
}
|
||||
|
||||
NonnullRefPtr<Promise<Optional<Response>>> Client::send_simple_command(CommandType type)
|
||||
NonnullRefPtr<Promise<Response>> Client::send_simple_command(CommandType type)
|
||||
{
|
||||
auto command = Command { type, m_current_command, {} };
|
||||
return send_command(move(command));
|
||||
}
|
||||
|
||||
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::select(StringView string)
|
||||
NonnullRefPtr<Promise<SolidResponse>> Client::select(StringView string)
|
||||
{
|
||||
auto command = Command { CommandType::Select, m_current_command, { serialize_astring(string) } };
|
||||
return cast_promise<SolidResponse>(send_command(move(command)));
|
||||
|
@ -255,8 +255,7 @@ ErrorOr<void> Client::handle_parsed_response(ParseStatus&& parse_status)
|
|||
if (parse_status.response.has_value()) {
|
||||
m_expecting_response = false;
|
||||
should_send_next = parse_status.response->has<SolidResponse>();
|
||||
m_pending_promises.first()->resolve(move(parse_status.response));
|
||||
m_pending_promises.remove(0);
|
||||
m_pending_promises.take_first()->resolve(parse_status.response.release_value());
|
||||
}
|
||||
|
||||
if (should_send_next && !m_command_queue.is_empty()) {
|
||||
|
@ -286,25 +285,25 @@ ErrorOr<void> Client::send_next_command()
|
|||
return {};
|
||||
}
|
||||
|
||||
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::examine(StringView string)
|
||||
NonnullRefPtr<Promise<SolidResponse>> Client::examine(StringView string)
|
||||
{
|
||||
auto command = Command { CommandType::Examine, m_current_command, { serialize_astring(string) } };
|
||||
return cast_promise<SolidResponse>(send_command(move(command)));
|
||||
}
|
||||
|
||||
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::create_mailbox(StringView name)
|
||||
NonnullRefPtr<Promise<SolidResponse>> Client::create_mailbox(StringView name)
|
||||
{
|
||||
auto command = Command { CommandType::Create, m_current_command, { serialize_astring(name) } };
|
||||
return cast_promise<SolidResponse>(send_command(move(command)));
|
||||
}
|
||||
|
||||
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::delete_mailbox(StringView name)
|
||||
NonnullRefPtr<Promise<SolidResponse>> Client::delete_mailbox(StringView name)
|
||||
{
|
||||
auto command = Command { CommandType::Delete, m_current_command, { serialize_astring(name) } };
|
||||
return cast_promise<SolidResponse>(send_command(move(command)));
|
||||
}
|
||||
|
||||
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::store(StoreMethod method, Sequence sequence_set, bool silent, Vector<DeprecatedString> const& flags, bool uid)
|
||||
NonnullRefPtr<Promise<SolidResponse>> Client::store(StoreMethod method, Sequence sequence_set, bool silent, Vector<DeprecatedString> const& flags, bool uid)
|
||||
{
|
||||
StringBuilder data_item_name;
|
||||
switch (method) {
|
||||
|
@ -330,7 +329,7 @@ NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::store(StoreMethod method
|
|||
auto command = Command { uid ? CommandType::UIDStore : CommandType::Store, m_current_command, { sequence_set.serialize(), data_item_name.to_deprecated_string(), flags_builder.to_deprecated_string() } };
|
||||
return cast_promise<SolidResponse>(send_command(move(command)));
|
||||
}
|
||||
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::search(Optional<DeprecatedString> charset, Vector<SearchKey>&& keys, bool uid)
|
||||
NonnullRefPtr<Promise<SolidResponse>> Client::search(Optional<DeprecatedString> charset, Vector<SearchKey>&& keys, bool uid)
|
||||
{
|
||||
Vector<DeprecatedString> args;
|
||||
if (charset.has_value()) {
|
||||
|
@ -344,21 +343,21 @@ NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::search(Optional<Deprecat
|
|||
return cast_promise<SolidResponse>(send_command(move(command)));
|
||||
}
|
||||
|
||||
NonnullRefPtr<Promise<Optional<ContinueRequest>>> Client::idle()
|
||||
NonnullRefPtr<Promise<ContinueRequest>> Client::idle()
|
||||
{
|
||||
auto promise = send_simple_command(CommandType::Idle);
|
||||
return cast_promise<ContinueRequest>(promise);
|
||||
}
|
||||
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::finish_idle()
|
||||
NonnullRefPtr<Promise<SolidResponse>> Client::finish_idle()
|
||||
{
|
||||
auto promise = Promise<Optional<Response>>::construct();
|
||||
auto promise = Promise<Response>::construct();
|
||||
m_pending_promises.append(promise);
|
||||
MUST(send_raw("DONE"sv));
|
||||
m_expecting_response = true;
|
||||
return cast_promise<SolidResponse>(promise);
|
||||
}
|
||||
|
||||
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::status(StringView mailbox, Vector<StatusItemType> const& types)
|
||||
NonnullRefPtr<Promise<SolidResponse>> Client::status(StringView mailbox, Vector<StatusItemType> const& types)
|
||||
{
|
||||
Vector<DeprecatedString> args;
|
||||
for (auto type : types) {
|
||||
|
@ -388,7 +387,7 @@ NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::status(StringView mailbo
|
|||
return cast_promise<SolidResponse>(send_command(move(command)));
|
||||
}
|
||||
|
||||
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::append(StringView mailbox, Message&& message, Optional<Vector<DeprecatedString>> flags, Optional<Core::DateTime> date_time)
|
||||
NonnullRefPtr<Promise<SolidResponse>> Client::append(StringView mailbox, Message&& message, Optional<Vector<DeprecatedString>> flags, Optional<Core::DateTime> date_time)
|
||||
{
|
||||
Vector<DeprecatedString> args = { mailbox };
|
||||
if (flags.has_value()) {
|
||||
|
@ -405,7 +404,7 @@ NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::append(StringView mailbo
|
|||
|
||||
auto continue_req = send_command(Command { CommandType::Append, m_current_command, args });
|
||||
|
||||
auto response_promise = Promise<Optional<Response>>::construct();
|
||||
auto response_promise = Promise<Response>::construct();
|
||||
m_pending_promises.append(response_promise);
|
||||
|
||||
continue_req->on_resolution = [this, message2 { move(message) }](auto&) -> ErrorOr<void> {
|
||||
|
@ -420,27 +419,27 @@ NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::append(StringView mailbo
|
|||
|
||||
return cast_promise<SolidResponse>(response_promise);
|
||||
}
|
||||
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::subscribe(StringView mailbox)
|
||||
NonnullRefPtr<Promise<SolidResponse>> Client::subscribe(StringView mailbox)
|
||||
{
|
||||
auto command = Command { CommandType::Subscribe, m_current_command, { serialize_astring(mailbox) } };
|
||||
return cast_promise<SolidResponse>(send_command(move(command)));
|
||||
}
|
||||
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::unsubscribe(StringView mailbox)
|
||||
NonnullRefPtr<Promise<SolidResponse>> Client::unsubscribe(StringView mailbox)
|
||||
{
|
||||
auto command = Command { CommandType::Unsubscribe, m_current_command, { serialize_astring(mailbox) } };
|
||||
return cast_promise<SolidResponse>(send_command(move(command)));
|
||||
}
|
||||
NonnullRefPtr<Promise<Optional<Response>>> Client::authenticate(StringView method)
|
||||
NonnullRefPtr<Promise<Response>> Client::authenticate(StringView method)
|
||||
{
|
||||
auto command = Command { CommandType::Authenticate, m_current_command, { method } };
|
||||
return send_command(move(command));
|
||||
}
|
||||
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::rename(StringView from, StringView to)
|
||||
NonnullRefPtr<Promise<SolidResponse>> Client::rename(StringView from, StringView to)
|
||||
{
|
||||
auto command = Command { CommandType::Rename, m_current_command, { serialize_astring(from), serialize_astring(to) } };
|
||||
return cast_promise<SolidResponse>(send_command(move(command)));
|
||||
}
|
||||
NonnullRefPtr<Promise<Optional<SolidResponse>>> Client::copy(Sequence sequence_set, StringView name, bool uid)
|
||||
NonnullRefPtr<Promise<SolidResponse>> Client::copy(Sequence sequence_set, StringView name, bool uid)
|
||||
{
|
||||
auto command = Command {
|
||||
uid ? CommandType::UIDCopy : CommandType::Copy, m_current_command, { sequence_set.serialize(), serialize_astring(name) }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue