aboutsummaryrefslogtreecommitdiffstats
path: root/src/libserver/css/css_value.hxx
blob: 882abf9905cebd38d173ee83baf8d480d847496b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*-
 * Copyright 2021 Vsevolod Stakhov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#ifndef RSPAMD_CSS_VALUE_HXX
#define RSPAMD_CSS_VALUE_HXX

#include "libserver/html.h"
#include <string>
#include <variant>
#include <optional>
#include "parse_error.hxx"
#include "css_parser.hxx"
#include "contrib/expected/expected.hpp"

namespace rspamd::css {

struct alignas(int) css_color {
	std::uint8_t r;
	std::uint8_t g;
	std::uint8_t b;

	std::uint8_t alpha;

	constexpr css_color(std::uint8_t _r, std::uint8_t _g, std::uint8_t _b, std::uint8_t _alpha = 255) :
	 	r(_r), g(_g), b(_b), alpha(_alpha) {}
};

/*
 * Simple enum class for display stuff
 */
enum class css_display_value {
	DISPLAY_NORMAL,
	DISPLAY_HIDDEN
};

/*
 * CSS flags
 */
enum class css_flag_value {
	FLAG_INHERIT,
	FLAG_IMPORTANT,
	FLAG_NOTIMPORTANT
};

/*
 * Value handler, uses std::variant instead of polymorphic classes for now
 * for simplicity
 */
struct css_value {
	enum class css_value_type {
		CSS_VALUE_COLOR,
		CSS_VALUE_SIZE,
		CSS_VALUE_DISPLAY,
		CSS_VALUE_FLAG,
		CSS_VALUE_NYI,
	} type;

	std::variant<css_color,
			double,
			css_display_value,
			css_flag_value> value;

	constexpr std::optional<css_color> to_color(void) const {
		if (type == css_value_type::CSS_VALUE_COLOR) {
			return std::get<css_color>(value);
		}

		return std::nullopt;
	}

	constexpr std::optional<double> to_size(void) const {
		if (type == css_value_type::CSS_VALUE_SIZE) {
			return std::get<double>(value);
		}

		return std::nullopt;
	}

	constexpr std::optional<css_display_value> to_display(void) const {
		if (type == css_value_type::CSS_VALUE_DISPLAY) {
			return std::get<css_display_value>(value);
		}

		return std::nullopt;
	}

	constexpr std::optional<css_flag_value> to_flag(void) const {
		if (type == css_value_type::CSS_VALUE_FLAG) {
			return std::get<css_flag_value>(value);
		}

		return std::nullopt;
	}

	constexpr bool is_valid(void) const {
		return (type != css_value_type::CSS_VALUE_NYI);
	}

	static auto from_css_block(const css_consumed_block &bl) -> tl::expected<css_value, css_parse_error>;

	static auto maybe_color_from_string(const std::string_view &input)
		-> std::optional<css_value>;
};

}

#endif //RSPAMD_CSS_VALUE_HXX