1 #pragma once
2 
3 #include "sserule.hpp"
4 #include "websocketrule.hpp"
5 
6 #include <boost/beast/http/verb.hpp>
7 
8 #include <initializer_list>
9 #include <optional>
10 
11 namespace crow
12 {
13 template <typename T>
14 struct RuleParameterTraits
15 {
16     using self_t = T;
17     WebSocketRule& websocket()
18     {
19         self_t* self = static_cast<self_t*>(this);
20         WebSocketRule* p = new WebSocketRule(self->rule);
21         p->privilegesSet = self->privilegesSet;
22         self->ruleToUpgrade.reset(p);
23         return *p;
24     }
25 
26     SseSocketRule& serverSentEvent()
27     {
28         self_t* self = static_cast<self_t*>(this);
29         SseSocketRule* p = new SseSocketRule(self->rule);
30         self->ruleToUpgrade.reset(p);
31         return *p;
32     }
33 
34     self_t& methods(boost::beast::http::verb method)
35     {
36         self_t* self = static_cast<self_t*>(this);
37         std::optional<HttpVerb> verb = httpVerbFromBoost(method);
38         if (verb)
39         {
40             self->methodsBitfield = 1U << static_cast<size_t>(*verb);
41         }
42         return *self;
43     }
44 
45     template <typename... MethodArgs>
46     self_t& methods(boost::beast::http::verb method, MethodArgs... argsMethod)
47     {
48         self_t* self = static_cast<self_t*>(this);
49         methods(argsMethod...);
50         std::optional<HttpVerb> verb = httpVerbFromBoost(method);
51         if (verb)
52         {
53             self->methodsBitfield |= 1U << static_cast<size_t>(*verb);
54         }
55         return *self;
56     }
57 
58     self_t& notFound()
59     {
60         self_t* self = static_cast<self_t*>(this);
61         self->methodsBitfield = 1U << notFoundIndex;
62         return *self;
63     }
64 
65     self_t& methodNotAllowed()
66     {
67         self_t* self = static_cast<self_t*>(this);
68         self->methodsBitfield = 1U << methodNotAllowedIndex;
69         return *self;
70     }
71 
72     self_t& privileges(
73         const std::initializer_list<std::initializer_list<const char*>>& p)
74     {
75         self_t* self = static_cast<self_t*>(this);
76         for (const std::initializer_list<const char*>& privilege : p)
77         {
78             self->privilegesSet.emplace_back(privilege);
79         }
80         return *self;
81     }
82 
83     template <size_t N, typename... MethodArgs>
84     self_t& privileges(const std::array<redfish::Privileges, N>& p)
85     {
86         self_t* self = static_cast<self_t*>(this);
87         for (const redfish::Privileges& privilege : p)
88         {
89             self->privilegesSet.emplace_back(privilege);
90         }
91         return *self;
92     }
93 };
94 } // namespace crow
95