/* * Copyright (c) 2022, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include enum class ResponseType : u8 { Success, Error, }; namespace Web::WebDriver { Response::Response(JsonValue&& value) : m_value_or_error(move(value)) { } Response::Response(Error&& error) : m_value_or_error(move(error)) { } } template<> ErrorOr IPC::encode(Encoder& encoder, Web::WebDriver::Response const& response) { return response.visit( [](Empty) -> ErrorOr { VERIFY_NOT_REACHED(); }, [&](JsonValue const& value) -> ErrorOr { TRY(encoder.encode(ResponseType::Success)); TRY(encoder.encode(value)); return {}; }, [&](Web::WebDriver::Error const& error) -> ErrorOr { TRY(encoder.encode(ResponseType::Error)); TRY(encoder.encode(error.http_status)); TRY(encoder.encode(error.error)); TRY(encoder.encode(error.message)); TRY(encoder.encode(error.data)); return {}; }); } template<> ErrorOr IPC::decode(Decoder& decoder) { auto type = TRY(decoder.decode()); switch (type) { case ResponseType::Success: return TRY(decoder.decode()); case ResponseType::Error: { auto http_status = TRY(decoder.decode()); auto error = TRY(decoder.decode()); auto message = TRY(decoder.decode()); auto data = TRY(decoder.decode>()); return Web::WebDriver::Error { http_status, move(error), move(message), move(data) }; } } VERIFY_NOT_REACHED(); }