GCC Code Coverage Report


Directory: ./
File: include/utils/common.hpp
Date: 2024-07-09 12:21:25
Exec Total Coverage
Lines: 19 23 82.6%
Functions: 4 5 80.0%
Branches: 15 20 75.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2024 Muhammad Nawaz
3 * Licensed under the MIT License. See LICENSE file for more information.
4 */
5 // [ END OF LICENSE c6bd0f49d040fca8d8a9cb05868e66aa63f0e2e0 ]
6
7 #ifndef COMMON_HPP
8 #define COMMON_HPP
9
10 #include <string>
11 #include <vector>
12
13 class ValidatorInitExc: public std::exception
14 {
15 private:
16 std::string ex_msg_;
17
18 public:
19 1 explicit ValidatorInitExc(std::string message)
20 1 : ex_msg_(std::move(message))
21 {
22 1 }
23
24 [[nodiscard]] const char* what() const noexcept override
25 {
26 return ex_msg_.c_str();
27 }
28 };
29
30 #define CHECK_ERROR(err) \
31 if (ValidationError::NONE != (err)) { \
32 return err; \
33 }
34
35 struct ParamRange
36 {
37 const char* beg;
38 const char* end;
39 };
40
41 #ifndef VALIDATION_ERROR
42 #define VALIDATION_ERROR
43 enum class ValidationError
44 {
45 NONE = 0,
46 INVALID_METHOD = -1,
47 INVALID_ROUTE = -2,
48 INVALID_PATH_PARAM = -3,
49 INVALID_QUERY_PARAM = -4,
50 INVALID_HEADER_PARAM = -5,
51 INVALID_BODY = -6,
52 INVALID_RSP = -7
53 };
54 #endif
55
56 enum class HttpMethod
57 {
58 GET = 0,
59 POST,
60 PUT,
61 DELETE,
62 HEAD,
63 OPTIONS,
64 PATCH,
65 CONNECT,
66 TRACE,
67 COUNT
68 };
69
70 enum class ParamStyle
71 {
72 SIMPLE, // Path, query, header
73 LABEL, // Path
74 MATRIX, // Path
75 FORM, // Query
76 SPACE_DELIM, // Query
77 PIPE_DELIM, // Query
78 DEEP_OBJ, // Query
79 CONTENT
80 };
81
82 enum class PrimitiveType
83 {
84 BOOLEAN,
85 INTEGER,
86 NUMBER,
87 STRING
88 };
89
90 enum class ExtendedType
91 {
92 BOOLEAN,
93 INTEGER,
94 NUMBER,
95 STRING,
96 ARRAY,
97 OBJECT
98 };
99
100 enum class SecurityType
101 {
102 HTTP_BASIC,
103 HTTP_BEARER,
104 APIKEY_QUERY,
105 APIKEY_HEADER,
106 OAUTH2,
107 OIDC
108 };
109
110 2402 inline const char* Seek(const char* beg, const char* end, const char c)
111 {
112
4/4
✓ Branch 0 taken 19839 times.
✓ Branch 1 taken 633 times.
✓ Branch 2 taken 18070 times.
✓ Branch 3 taken 1769 times.
20472 while (beg < end && *beg != c) {
113 18070 ++beg;
114 }
115 2402 return beg;
116 }
117
118 528 inline std::string EscapeSlash(const std::string& str)
119 {
120 528 std::string escaped_str;
121
2/2
✓ Branch 5 taken 14400 times.
✓ Branch 6 taken 528 times.
14928 for (char ch : str) {
122
2/2
✓ Branch 0 taken 1278 times.
✓ Branch 1 taken 13122 times.
14400 if (ch == '/') {
123
1/2
✓ Branch 1 taken 1278 times.
✗ Branch 2 not taken.
1278 escaped_str += "%2F";
124 } else {
125
1/2
✓ Branch 1 taken 13122 times.
✗ Branch 2 not taken.
13122 escaped_str += ch;
126 }
127 }
128 528 return escaped_str;
129 }
130
131 945 inline std::string JoinReference(const std::vector<std::string>& ref_keys)
132 {
133
1/2
✓ Branch 2 taken 945 times.
✗ Branch 3 not taken.
945 std::string reference = "#";
134
2/2
✓ Branch 4 taken 4589 times.
✓ Branch 5 taken 945 times.
5534 for (const auto& ref_key : ref_keys) {
135
2/4
✓ Branch 1 taken 4589 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4589 times.
✗ Branch 5 not taken.
4589 reference += "/" + ref_key;
136 }
137 945 return reference;
138 }
139
140 #endif // COMMON_HPP
141