yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie Hermaszewskaformatf65d756bf

master
4.6 KiB162 linesraw
1#ifndef SLANG_COMPILER_CORE_JSON_RPC_H
2#define SLANG_COMPILER_CORE_JSON_RPC_H
3
4#include "../core/slang-http.h"
5#include "slang-com-helper.h"
6#include "slang-com-ptr.h"
7#include "slang-json-parser.h"
8#include "slang-json-value.h"
9#include "slang.h"
10
11namespace Slang
12{
13
14/// Struct to hold values associated with JSON-RPC
15struct JSONRPC
16{
17    enum class ErrorCode
18    {
19        ParseError = -32700,     ///< Invalid JSON was received by the server.
20        InvalidRequest = -32600, ///< The JSON sent is not a valid Request object.
21        MethodNotFound = -32601, ///< The method does not exist / is not available.
22        InvalidParams = -32602,  ///< Invalid method parameter(s).
23        InternalError = -32603,  ///< Internal JSON - RPC error.
24
25        ServerImplStart = -32000, ///< Server implementation defined error range
26        ServerImplEnd = -32099,
27    };
28
29    static bool isIdOk(const JSONValue& value)
30    {
31        auto kind = value.getKind();
32        switch (kind)
33        {
34        case JSONValue::Kind::Integer:
35        case JSONValue::Kind::Invalid:
36        case JSONValue::Kind::String:
37            {
38                return true;
39            }
40        }
41        return false;
42    }
43
44    static const UnownedStringSlice jsonRpc;
45    static const UnownedStringSlice jsonRpcVersion;
46    static const UnownedStringSlice id;
47};
48
49struct JSONRPCErrorResponse
50{
51    struct Error
52    {
53        bool isValid() const { return code != 0; }
54
55        Int code = 0;               ///< Value from ErrorCode
56        UnownedStringSlice message; ///< Error message
57
58        static const StructRttiInfo g_rttiInfo;
59    };
60
61    bool isValid() const
62    {
63        return jsonrpc == JSONRPC::jsonRpcVersion && error.isValid() && JSONRPC::isIdOk(id);
64    }
65
66    UnownedStringSlice jsonrpc = JSONRPC::jsonRpcVersion;
67    Error error;
68    JSONValue data; ///< Optional data describing the errro
69    JSONValue id;   ///< Id associated with this request
70
71    static const StructRttiInfo g_rttiInfo;
72};
73
74struct JSONRPCCall
75{
76    bool isValid() const
77    {
78        return method.getLength() > 0 && jsonrpc == JSONRPC::jsonRpcVersion && JSONRPC::isIdOk(id);
79    }
80
81    UnownedStringSlice jsonrpc = JSONRPC::jsonRpcVersion;
82    UnownedStringSlice method; ///< The name of the method
83    JSONValue params;          ///< Can be invalid/array/object
84    JSONValue id;              ///< Id associated with this request
85
86    static const StructRttiInfo g_rttiInfo;
87};
88
89struct JSONResultResponse
90{
91    bool isValid() const { return jsonrpc == JSONRPC::jsonRpcVersion && JSONRPC::isIdOk(id); }
92
93    UnownedStringSlice jsonrpc = JSONRPC::jsonRpcVersion;
94    JSONValue result; ///< The result value
95    JSONValue id;     ///< Id associated with this request
96
97    static const StructRttiInfo g_rttiInfo;
98};
99
100enum class JSONRPCMessageType
101{
102    Invalid,
103    Result,
104    Call,
105    Error,
106    CountOf,
107};
108
109/// Send and receive messages as JSON
110class JSONRPCUtil
111{
112public:
113    /// Determine the response type
114    static JSONRPCMessageType getMessageType(JSONContainer* container, const JSONValue& value);
115
116    /// Parse slice into JSONContainer. outValue is the root of the hierarchy.
117    /// NOTE! Uses and *assumes* there is a source manager on the sink. outValue is likely only
118    /// usable whilst the sourceManger is in scope The sourceLoc can only be interpretted with the
119    /// sourceLoc anyway
120    static SlangResult parseJSON(
121        const UnownedStringSlice& slice,
122        JSONContainer* container,
123        DiagnosticSink* sink,
124        JSONValue& outValue);
125
126    /// Convert value into out
127    static SlangResult convertToNative(
128        JSONContainer* container,
129        const JSONValue& value,
130        DiagnosticSink* sink,
131        const RttiInfo* rttiInfo,
132        void* out);
133    template<typename T>
134    static SlangResult convertToNative(
135        JSONContainer* container,
136        const JSONValue& value,
137        DiagnosticSink* sink,
138        T& out)
139    {
140        return convertToNative(container, value, sink, GetRttiInfo<T>::get(), (void*)&out);
141    }
142
143    /// Convert to JSON
144    static SlangResult convertToJSON(
145        const RttiInfo* rttiInfo,
146        const void* in,
147        DiagnosticSink* sink,
148        StringBuilder& out);
149
150    template<typename T>
151    static SlangResult convertToJSON(const T* in, DiagnosticSink* sink, StringBuilder& out)
152    {
153        return convertToJSON(GetRttiInfo<T>::get(), (const void*)in, sink, out);
154    }
155
156    /// Get an id directly from root (assumed id: is in root object definition).
157    static JSONValue getId(JSONContainer* container, const JSONValue& root);
158};
159
160} // namespace Slang
161
162#endif // SLANG_COMPILER_CORE_JSON_RPC_H