yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f65d756bf
master
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{ 17enum class ErrorCode 18 { 19ParseError = -32700 ,///< Invalid JSON was received by the server. 20InvalidRequest = -32600 ,///< The JSON sent is not a valid Request object. 21MethodNotFound = -32601 ,///< The method does not exist / is not available. 22InvalidParams = -32602 ,///< Invalid method parameter(s). 23InternalError = -32603 ,///< Internal JSON - RPC error. 24 25ServerImplStart = -32000 ,///< Server implementation defined error range 26ServerImplEnd = -32099 , 27 }; 28 29static bool isIdOk (const JSONValue & value ) 30 { 31 autokind = value .getKind (); 32switch (kind ) 33 { 34case JSONValue ::Kind ::Integer : 35case JSONValue ::Kind ::Invalid : 36case JSONValue ::Kind ::String : 37 { 38return true; 39 } 40 } 41return false; 42 } 43 44static const UnownedStringSlice jsonRpc ; 45static const UnownedStringSlice jsonRpcVersion ; 46static const UnownedStringSlice id ; 47}; 48 49struct JSONRPCErrorResponse 50{ 51struct Error 52 { 53bool isValid ()const {return code != 0 ; } 54 55Int code = 0 ;///< Value from ErrorCode 56UnownedStringSlice message ;///< Error message 57 58static const StructRttiInfo g_rttiInfo ; 59 }; 60 61bool isValid ()const 62 { 63return jsonrpc == JSONRPC ::jsonRpcVersion && error .isValid ()&& JSONRPC ::isIdOk (id ); 64 } 65 66UnownedStringSlice jsonrpc = JSONRPC ::jsonRpcVersion ; 67Error error ; 68JSONValue data ;///< Optional data describing the errro 69JSONValue id ;///< Id associated with this request 70 71static const StructRttiInfo g_rttiInfo ; 72}; 73 74struct JSONRPCCall 75{ 76bool isValid ()const 77 { 78return method .getLength ()> 0 && jsonrpc == JSONRPC ::jsonRpcVersion && JSONRPC ::isIdOk (id ); 79 } 80 81UnownedStringSlice jsonrpc = JSONRPC ::jsonRpcVersion ; 82UnownedStringSlice method ;///< The name of the method 83JSONValue params ;///< Can be invalid/array/object 84JSONValue id ;///< Id associated with this request 85 86static const StructRttiInfo g_rttiInfo ; 87}; 88 89struct JSONResultResponse 90{ 91bool isValid ()const {return jsonrpc == JSONRPC ::jsonRpcVersion && JSONRPC ::isIdOk (id ); } 92 93UnownedStringSlice jsonrpc = JSONRPC ::jsonRpcVersion ; 94JSONValue result ;///< The result value 95JSONValue id ;///< Id associated with this request 96 97static const StructRttiInfo g_rttiInfo ; 98}; 99 100enum class JSONRPCMessageType 101{ 102Invalid , 103Result , 104Call , 105Error , 106CountOf , 107}; 108 109/// Send and receive messages as JSON 110class JSONRPCUtil 111{ 112public : 113/// Determine the response type 114static 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 120static SlangResult parseJSON ( 121const UnownedStringSlice & slice , 122JSONContainer * container , 123DiagnosticSink * sink , 124JSONValue & outValue ); 125 126/// Convert value into out 127static SlangResult convertToNative ( 128JSONContainer * container , 129const JSONValue & value , 130DiagnosticSink * sink , 131const RttiInfo * rttiInfo , 132void * out ); 133template < typename T > 134static SlangResult convertToNative ( 135JSONContainer * container , 136const JSONValue & value , 137DiagnosticSink * sink , 138T & out ) 139 { 140return convertToNative (container ,value ,sink ,GetRttiInfo < T > ::get (), (void * )& out ); 141 } 142 143/// Convert to JSON 144static SlangResult convertToJSON ( 145const RttiInfo * rttiInfo , 146const void * in , 147DiagnosticSink * sink , 148StringBuilder & out ); 149 150template < typename T > 151static SlangResult convertToJSON (const T * in ,DiagnosticSink * sink ,StringBuilder & out ) 152 { 153return 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). 157static JSONValue getId (JSONContainer * container ,const JSONValue & root ); 158}; 159 160}// namespace Slang 161 162#endif // SLANG_COMPILER_CORE_JSON_RPC_H