Ada 3.1.0
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
url_pattern_init.h
Go to the documentation of this file.
1
5#ifndef ADA_URL_PATTERN_INIT_H
6#define ADA_URL_PATTERN_INIT_H
7
8#include "ada/expected.h"
9#include "ada/errors.h"
10
11#include <string_view>
12#include <string>
13#include <optional>
14
15#if ADA_TESTING
16#include <iostream>
17#endif // ADA_TESTING
18
19namespace ada {
20
21// Important: C++20 allows us to use concept rather than `using` or `typedef
22// and allows functions with second argument, which is optional (using either
23// std::nullopt or a parameter with default value)
24template <typename F>
25concept url_pattern_encoding_callback = requires(F f, std::string_view sv) {
26 { f(sv) } -> std::same_as<tl::expected<std::string, errors>>;
27};
28
29// A structure providing matching patterns for individual components
30// of a URL. When a URLPattern is created, or when a URLPattern is
31// used to match or test against a URL, the input can be given as
32// either a string or a URLPatternInit struct. If a string is given,
33// it will be parsed to create a URLPatternInit. The URLPatternInit
34// API is defined as part of the URLPattern specification.
35// All provided strings must be valid UTF-8.
37 enum class process_type : uint8_t {
40 };
41
42 // All strings must be valid UTF-8.
43 // @see https://urlpattern.spec.whatwg.org/#process-a-urlpatterninit
44 static tl::expected<url_pattern_init, errors> process(
46 std::optional<std::string_view> protocol = std::nullopt,
47 std::optional<std::string_view> username = std::nullopt,
48 std::optional<std::string_view> password = std::nullopt,
49 std::optional<std::string_view> hostname = std::nullopt,
50 std::optional<std::string_view> port = std::nullopt,
51 std::optional<std::string_view> pathname = std::nullopt,
52 std::optional<std::string_view> search = std::nullopt,
53 std::optional<std::string_view> hash = std::nullopt);
54
55 // @see https://urlpattern.spec.whatwg.org/#process-protocol-for-init
56 static tl::expected<std::string, errors> process_protocol(
57 std::string_view value, process_type type);
58
59 // @see https://urlpattern.spec.whatwg.org/#process-username-for-init
60 static tl::expected<std::string, errors> process_username(
61 std::string_view value, process_type type);
62
63 // @see https://urlpattern.spec.whatwg.org/#process-password-for-init
64 static tl::expected<std::string, errors> process_password(
65 std::string_view value, process_type type);
66
67 // @see https://urlpattern.spec.whatwg.org/#process-hostname-for-init
68 static tl::expected<std::string, errors> process_hostname(
69 std::string_view value, process_type type);
70
71 // @see https://urlpattern.spec.whatwg.org/#process-port-for-init
72 static tl::expected<std::string, errors> process_port(
73 std::string_view port, std::string_view protocol, process_type type);
74
75 // @see https://urlpattern.spec.whatwg.org/#process-pathname-for-init
76 static tl::expected<std::string, errors> process_pathname(
77 std::string_view value, std::string_view protocol, process_type type);
78
79 // @see https://urlpattern.spec.whatwg.org/#process-search-for-init
80 static tl::expected<std::string, errors> process_search(
81 std::string_view value, process_type type);
82
83 // @see https://urlpattern.spec.whatwg.org/#process-hash-for-init
84 static tl::expected<std::string, errors> process_hash(std::string_view value,
85 process_type type);
86
87#if ADA_TESTING
88 friend void PrintTo(const url_pattern_init& init, std::ostream* os) {
89 *os << "protocol: '" << init.protocol.value_or("undefined") << "', ";
90 *os << "username: '" << init.username.value_or("undefined") << "', ";
91 *os << "password: '" << init.password.value_or("undefined") << "', ";
92 *os << "hostname: '" << init.hostname.value_or("undefined") << "', ";
93 *os << "port: '" << init.port.value_or("undefined") << "', ";
94 *os << "pathname: '" << init.pathname.value_or("undefined") << "', ";
95 *os << "search: '" << init.search.value_or("undefined") << "', ";
96 *os << "hash: '" << init.hash.value_or("undefined") << "', ";
97 *os << "base_url: '" << init.base_url.value_or("undefined") << "', ";
98 }
99#endif // ADA_TESTING
100
101 bool operator==(const url_pattern_init&) const;
102 // If present, must be valid UTF-8.
103 std::optional<std::string> protocol{};
104 // If present, must be valid UTF-8.
105 std::optional<std::string> username{};
106 // If present, must be valid UTF-8.
107 std::optional<std::string> password{};
108 // If present, must be valid UTF-8.
109 std::optional<std::string> hostname{};
110 // If present, must be valid UTF-8.
111 std::optional<std::string> port{};
112 // If present, must be valid UTF-8.
113 std::optional<std::string> pathname{};
114 // If present, must be valid UTF-8.
115 std::optional<std::string> search{};
116 // If present, must be valid UTF-8.
117 std::optional<std::string> hash{};
118 // If present, must be valid UTF-8.
119 std::optional<std::string> base_url{};
120};
121} // namespace ada
122
123#endif // ADA_URL_PATTERN_INIT_H
Definitions for the errors.
Definition ada_idna.h:13
std::optional< std::string > port
static tl::expected< std::string, errors > process_pathname(std::string_view value, std::string_view protocol, process_type type)
std::optional< std::string > protocol
static tl::expected< url_pattern_init, errors > process(url_pattern_init init, process_type type, std::optional< std::string_view > protocol=std::nullopt, std::optional< std::string_view > username=std::nullopt, std::optional< std::string_view > password=std::nullopt, std::optional< std::string_view > hostname=std::nullopt, std::optional< std::string_view > port=std::nullopt, std::optional< std::string_view > pathname=std::nullopt, std::optional< std::string_view > search=std::nullopt, std::optional< std::string_view > hash=std::nullopt)
std::optional< std::string > password
std::optional< std::string > base_url
std::optional< std::string > hostname
std::optional< std::string > search
bool operator==(const url_pattern_init &) const
static tl::expected< std::string, errors > process_protocol(std::string_view value, process_type type)
static tl::expected< std::string, errors > process_username(std::string_view value, process_type type)
std::optional< std::string > username
static tl::expected< std::string, errors > process_password(std::string_view value, process_type type)
static tl::expected< std::string, errors > process_port(std::string_view port, std::string_view protocol, process_type type)
static tl::expected< std::string, errors > process_hash(std::string_view value, process_type type)
std::optional< std::string > pathname
std::optional< std::string > hash
static tl::expected< std::string, errors > process_search(std::string_view value, process_type type)
static tl::expected< std::string, errors > process_hostname(std::string_view value, process_type type)