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;
websocketcrow::RuleParameterTraits17     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 
serverSentEventcrow::RuleParameterTraits26     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 
methodscrow::RuleParameterTraits34     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>
methodscrow::RuleParameterTraits46     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 
notFoundcrow::RuleParameterTraits58     self_t& notFound()
59     {
60         self_t* self = static_cast<self_t*>(this);
61         self->isNotFound = true;
62         self->methodsBitfield = 0;
63         return *self;
64     }
65 
methodNotAllowedcrow::RuleParameterTraits66     self_t& methodNotAllowed()
67     {
68         self_t* self = static_cast<self_t*>(this);
69         self->isMethodNotAllowed = true;
70         self->methodsBitfield = 0;
71         return *self;
72     }
73 
privilegescrow::RuleParameterTraits74     self_t& privileges(
75         const std::initializer_list<std::initializer_list<const char*>>& p)
76     {
77         self_t* self = static_cast<self_t*>(this);
78         for (const std::initializer_list<const char*>& privilege : p)
79         {
80             self->privilegesSet.emplace_back(privilege);
81         }
82         return *self;
83     }
84 
85     template <size_t N, typename... MethodArgs>
privilegescrow::RuleParameterTraits86     self_t& privileges(const std::array<redfish::Privileges, N>& p)
87     {
88         self_t* self = static_cast<self_t*>(this);
89         for (const redfish::Privileges& privilege : p)
90         {
91             self->privilegesSet.emplace_back(privilege);
92         }
93         return *self;
94     }
95 };
96 } // namespace crow
97