Debian Codegraph @

LspCpp/include/LibLsp/JsonRpc/MessageJsonHandler.h

raw
1 #pragma once
2 #include <string>
3 #include <map>
4 #include <functional>
5 #include <LibLsp/JsonRpc/message.h>
6 class Reader;
7
8 using GenericRequestJsonHandler = std::function<std::unique_ptr<LspMessage>(Reader&)>;
9 using GenericResponseJsonHandler = std::function<std::unique_ptr<LspMessage>(Reader&)>;
10 using GenericNotificationJsonHandler = std::function<std::unique_ptr<LspMessage>(Reader&)>;
11
12 class MessageJsonHandler
13 {
14 public:
15 std::map<std::string, GenericRequestJsonHandler> method2request;
16 std::map<std::string, GenericResponseJsonHandler> method2response;
17 std::map<std::string, GenericNotificationJsonHandler> method2notification;
18
19 GenericRequestJsonHandler const* GetRequestJsonHandler(char const* methodInfo) const
20 {
21 auto const findIt = method2request.find(methodInfo);
22 return findIt == method2request.end() ? nullptr : &findIt->second;
23 }
24
25 void SetRequestJsonHandler(std::string const& methodInfo, GenericRequestJsonHandler handler)
26 {
27 method2request[methodInfo] = handler;
28 }
29
30 GenericResponseJsonHandler const* GetResponseJsonHandler(char const* methodInfo) const
31 {
32 auto const findIt = method2response.find(methodInfo);
33 return findIt == method2response.end() ? nullptr : &findIt->second;
34 }
35
36 void SetResponseJsonHandler(std::string const& methodInfo, GenericResponseJsonHandler handler)
37 {
38 method2response[methodInfo] = handler;
39 }
40
41 GenericNotificationJsonHandler const* GetNotificationJsonHandler(char const* methodInfo) const
42 {
43 auto const findIt = method2notification.find(methodInfo);
44 return findIt == method2notification.end() ? nullptr : &findIt->second;
45 }
46
47 void SetNotificationJsonHandler(std::string const& methodInfo, GenericNotificationJsonHandler handler)
48 {
49 method2notification[methodInfo] = handler;
50 }
51
52 std::unique_ptr<LspMessage> parseResponseMessage(std::string const&, Reader&);
53 std::unique_ptr<LspMessage> parseRequstMessage(std::string const&, Reader&);
54 bool resovleResponseMessage(Reader&, std::pair<std::string, std::unique_ptr<LspMessage>>& result);
55 std::unique_ptr<LspMessage> parseNotificationMessage(std::string const&, Reader&);
56 };