summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/WebDriver/Response.cpp
blob: a44e17f5784230997f0f46beb26efaa97da8bfce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
 * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibIPC/Decoder.h>
#include <LibIPC/Encoder.h>
#include <LibWeb/WebDriver/Response.h>

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<>
bool IPC::encode(Encoder& encoder, Web::WebDriver::Response const& response)
{
    response.visit(
        [](Empty) { VERIFY_NOT_REACHED(); },
        [&](JsonValue const& value) {
            encoder << ResponseType::Success;
            encoder << value;
        },
        [&](Web::WebDriver::Error const& error) {
            encoder << ResponseType::Error;
            encoder << error.http_status;
            encoder << error.error;
            encoder << error.message;
            encoder << error.data;
        });

    return true;
}

template<>
ErrorOr<void> IPC::decode(Decoder& decoder, Web::WebDriver::Response& response)
{
    ResponseType type {};
    TRY(decoder.decode(type));

    switch (type) {
    case ResponseType::Success: {
        JsonValue value;
        TRY(decoder.decode(value));

        response = move(value);
        break;
    }

    case ResponseType::Error: {
        Web::WebDriver::Error error {};
        TRY(decoder.decode(error.http_status));
        TRY(decoder.decode(error.error));
        TRY(decoder.decode(error.message));
        TRY(decoder.decode(error.data));

        response = move(error);
        break;
    }
    }

    return {};
}