Ada 3.0.1
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.
36 // @see https://urlpattern.spec.whatwg.org/#process-a-urlpatterninit
37 static tl::expected<url_pattern_init, errors> process(
38 url_pattern_init init, std::string_view type,
39 std::optional<std::string_view> protocol = std::nullopt,
40 std::optional<std::string_view> username = std::nullopt,
41 std::optional<std::string_view> password = std::nullopt,
42 std::optional<std::string_view> hostname = std::nullopt,
43 std::optional<std::string_view> port = std::nullopt,
44 std::optional<std::string_view> pathname = std::nullopt,
45 std::optional<std::string_view> search = std::nullopt,
46 std::optional<std::string_view> hash = std::nullopt);
47
48 // @see https://urlpattern.spec.whatwg.org/#process-protocol-for-init
49 static tl::expected<std::string, errors> process_protocol(
50 std::string_view value, std::string_view type);
51
52 // @see https://urlpattern.spec.whatwg.org/#process-username-for-init
53 static tl::expected<std::string, errors> process_username(
54 std::string_view value, std::string_view type);
55
56 // @see https://urlpattern.spec.whatwg.org/#process-password-for-init
57 static tl::expected<std::string, errors> process_password(
58 std::string_view value, std::string_view type);
59
60 // @see https://urlpattern.spec.whatwg.org/#process-hostname-for-init
61 static tl::expected<std::string, errors> process_hostname(
62 std::string_view value, std::string_view type);
63
64 // @see https://urlpattern.spec.whatwg.org/#process-port-for-init
65 static tl::expected<std::string, errors> process_port(
66 std::string_view port, std::string_view protocol, std::string_view type);
67
68 // @see https://urlpattern.spec.whatwg.org/#process-pathname-for-init
69 static tl::expected<std::string, errors> process_pathname(
70 std::string_view value, std::string_view protocol, std::string_view type);
71
72 // @see https://urlpattern.spec.whatwg.org/#process-search-for-init
73 static tl::expected<std::string, errors> process_search(
74 std::string_view value, std::string_view type);
75
76 // @see https://urlpattern.spec.whatwg.org/#process-hash-for-init
77 static tl::expected<std::string, errors> process_hash(std::string_view value,
78 std::string_view type);
79
80#if ADA_TESTING
81 friend void PrintTo(const url_pattern_init& init, std::ostream* os) {
82 *os << "protocol: '" << init.protocol.value_or("undefined") << "', ";
83 *os << "username: '" << init.username.value_or("undefined") << "', ";
84 *os << "password: '" << init.password.value_or("undefined") << "', ";
85 *os << "hostname: '" << init.hostname.value_or("undefined") << "', ";
86 *os << "port: '" << init.port.value_or("undefined") << "', ";
87 *os << "pathname: '" << init.pathname.value_or("undefined") << "', ";
88 *os << "search: '" << init.search.value_or("undefined") << "', ";
89 *os << "hash: '" << init.hash.value_or("undefined") << "', ";
90 *os << "base_url: '" << init.base_url.value_or("undefined") << "', ";
91 }
92#endif // ADA_TESTING
93
94 bool operator==(const url_pattern_init&) const;
95
96 std::optional<std::string> protocol{};
97 std::optional<std::string> username{};
98 std::optional<std::string> password{};
99 std::optional<std::string> hostname{};
100 std::optional<std::string> port{};
101 std::optional<std::string> pathname{};
102 std::optional<std::string> search{};
103 std::optional<std::string> hash{};
104 std::optional<std::string> base_url{};
105};
106} // namespace ada
107
108#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, std::string_view type)
static tl::expected< std::string, errors > process_username(std::string_view value, std::string_view type)
static tl::expected< std::string, errors > process_port(std::string_view port, std::string_view protocol, std::string_view type)
static tl::expected< std::string, errors > process_hostname(std::string_view value, std::string_view type)
std::optional< std::string > protocol
static tl::expected< url_pattern_init, errors > process(url_pattern_init init, std::string_view 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
static tl::expected< std::string, errors > process_hash(std::string_view value, std::string_view type)
bool operator==(const url_pattern_init &) const
static tl::expected< std::string, errors > process_password(std::string_view value, std::string_view type)
std::optional< std::string > username
std::optional< std::string > pathname
static tl::expected< std::string, errors > process_search(std::string_view value, std::string_view type)
std::optional< std::string > hash
static tl::expected< std::string, errors > process_protocol(std::string_view value, std::string_view type)