Skip to content

Commit 223cf5d

Browse files
committed
merge error enums
1 parent 01ccd73 commit 223cf5d

11 files changed

+115
-139
lines changed

include/ada/implementation.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include "ada/url_aggregator.h"
1818

1919
namespace ada {
20-
enum class errors { generic_error };
20+
enum class errors : uint8_t { type_error };
2121

2222
template <class result_type = ada::url_aggregator>
2323
using result = tl::expected<result_type, ada::errors>;
@@ -58,7 +58,7 @@ bool can_parse(std::string_view input,
5858
* @param options an optional url_pattern_options struct
5959
* @return url_pattern instance
6060
*/
61-
ada_warn_unused tl::expected<url_pattern, url_pattern_errors> parse_url_pattern(
61+
ada_warn_unused tl::expected<url_pattern, errors> parse_url_pattern(
6262
std::variant<std::string_view, url_pattern_init> input,
6363
const std::string_view* base_url = nullptr,
6464
const url_pattern_options* options = nullptr);

include/ada/parser.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct url;
1919
class url_pattern;
2020
struct url_pattern_options;
2121
struct url_pattern_init;
22-
enum class url_pattern_errors : uint8_t;
22+
enum class errors : uint8_t;
2323
} // namespace ada
2424

2525
/**
@@ -51,7 +51,7 @@ extern template url_aggregator parse_url_impl<url_aggregator>(
5151
extern template url parse_url_impl<url>(std::string_view user_input,
5252
const url* base_url);
5353

54-
tl::expected<url_pattern, url_pattern_errors> parse_url_pattern_impl(
54+
tl::expected<url_pattern, errors> parse_url_pattern_impl(
5555
std::variant<std::string_view, url_pattern_init> input,
5656
const std::string_view* base_url, const url_pattern_options* options);
5757

include/ada/url_aggregator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ struct url_aggregator : url_base {
222222
friend url_aggregator parser::parse_url_impl<url_aggregator, false>(
223223
std::string_view, const url_aggregator *);
224224
// url_pattern methods
225-
friend tl::expected<url_pattern, url_pattern_errors> parse_url_pattern_impl(
225+
friend tl::expected<url_pattern, errors> parse_url_pattern_impl(
226226
std::variant<std::string_view, url_pattern_init> input,
227227
const std::string_view *base_url, const url_pattern_options *options);
228228

include/ada/url_pattern.h

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@
1515

1616
namespace ada {
1717

18-
enum class url_pattern_errors : uint8_t { type_error };
19-
2018
namespace parser {
2119
template <typename result_type, typename url_pattern_init,
2220
typename url_pattern_options>
23-
tl::expected<result_type, url_pattern_errors> parse_url_pattern_impl(
21+
tl::expected<result_type, errors> parse_url_pattern_impl(
2422
std::variant<std::string_view, url_pattern_init> input,
2523
const std::string_view* base_url, const url_pattern_options* options);
2624
}
@@ -30,7 +28,7 @@ tl::expected<result_type, url_pattern_errors> parse_url_pattern_impl(
3028
// std::nullopt or a parameter with default value)
3129
template <typename F>
3230
concept url_pattern_encoding_callback = requires(F f, std::string_view sv) {
33-
{ f(sv) } -> std::same_as<tl::expected<std::string, url_pattern_errors>>;
31+
{ f(sv) } -> std::same_as<tl::expected<std::string, errors>>;
3432
};
3533

3634
// A structure providing matching patterns for individual components
@@ -41,7 +39,7 @@ concept url_pattern_encoding_callback = requires(F f, std::string_view sv) {
4139
// API is defined as part of the URLPattern specification.
4240
struct url_pattern_init {
4341
// @see https://urlpattern.spec.whatwg.org/#process-a-urlpatterninit
44-
static tl::expected<url_pattern_init, url_pattern_errors> process(
42+
static tl::expected<url_pattern_init, errors> process(
4543
url_pattern_init init, std::string_view type,
4644
std::optional<std::string_view> protocol = std::nullopt,
4745
std::optional<std::string_view> username = std::nullopt,
@@ -53,36 +51,36 @@ struct url_pattern_init {
5351
std::optional<std::string_view> hash = std::nullopt);
5452

5553
// @see https://urlpattern.spec.whatwg.org/#process-protocol-for-init
56-
static tl::expected<std::string, url_pattern_errors> process_protocol(
54+
static tl::expected<std::string, errors> process_protocol(
5755
std::string_view value, std::string_view type);
5856

5957
// @see https://urlpattern.spec.whatwg.org/#process-username-for-init
60-
static tl::expected<std::string, url_pattern_errors> process_username(
58+
static tl::expected<std::string, errors> process_username(
6159
std::string_view value, std::string_view type);
6260

6361
// @see https://urlpattern.spec.whatwg.org/#process-password-for-init
64-
static tl::expected<std::string, url_pattern_errors> process_password(
62+
static tl::expected<std::string, errors> process_password(
6563
std::string_view value, std::string_view type);
6664

6765
// @see https://urlpattern.spec.whatwg.org/#process-hostname-for-init
68-
static tl::expected<std::string, url_pattern_errors> process_hostname(
66+
static tl::expected<std::string, errors> process_hostname(
6967
std::string_view value, std::string_view type);
7068

7169
// @see https://urlpattern.spec.whatwg.org/#process-port-for-init
72-
static tl::expected<std::string, url_pattern_errors> process_port(
70+
static tl::expected<std::string, errors> process_port(
7371
std::string_view port, std::string_view protocol, std::string_view type);
7472

7573
// @see https://urlpattern.spec.whatwg.org/#process-pathname-for-init
76-
static tl::expected<std::string, url_pattern_errors> process_pathname(
74+
static tl::expected<std::string, errors> process_pathname(
7775
std::string_view value, std::string_view protocol, std::string_view type);
7876

7977
// @see https://urlpattern.spec.whatwg.org/#process-search-for-init
80-
static tl::expected<std::string, url_pattern_errors> process_search(
78+
static tl::expected<std::string, errors> process_search(
8179
std::string_view value, std::string_view type);
8280

8381
// @see https://urlpattern.spec.whatwg.org/#process-hash-for-init
84-
static tl::expected<std::string, url_pattern_errors> process_hash(
85-
std::string_view value, std::string_view type);
82+
static tl::expected<std::string, errors> process_hash(std::string_view value,
83+
std::string_view type);
8684

8785
[[nodiscard]] std::string to_string() const;
8886

@@ -210,7 +208,7 @@ class url_pattern_component {
210208

211209
// @see https://urlpattern.spec.whatwg.org/#compile-a-component
212210
template <url_pattern_encoding_callback F>
213-
static tl::expected<url_pattern_component, url_pattern_errors> compile(
211+
static tl::expected<url_pattern_component, errors> compile(
214212
std::string_view input, F& encoding_callback,
215213
url_pattern_compile_component_options& options);
216214

@@ -263,7 +261,7 @@ class url_pattern {
263261
std::optional<url_pattern_options>&& options);
264262

265263
// @see https://urlpattern.spec.whatwg.org/#dom-urlpattern-exec
266-
tl::expected<std::optional<url_pattern_result>, url_pattern_errors> exec(
264+
tl::expected<std::optional<url_pattern_result>, errors> exec(
267265
url_pattern_input&& input, std::string_view* base_url);
268266
// @see https://urlpattern.spec.whatwg.org/#dom-urlpattern-test
269267
bool test(url_pattern_input&& input, std::string_view* base_url);
@@ -272,7 +270,7 @@ class url_pattern {
272270
* @see https://urlpattern.spec.whatwg.org/#url-pattern-match
273271
* This function expects a valid UTF-8 string if input is a string.
274272
*/
275-
tl::expected<std::optional<url_pattern_result>, url_pattern_errors> match(
273+
tl::expected<std::optional<url_pattern_result>, errors> match(
276274
url_pattern_input&& input, std::string_view* base_url_string);
277275

278276
// @see https://urlpattern.spec.whatwg.org/#dom-urlpattern-protocol
@@ -313,8 +311,7 @@ class url_pattern {
313311

314312
template <typename result_type, typename url_pattern_init,
315313
typename url_pattern_options>
316-
friend tl::expected<result_type, url_pattern_errors>
317-
parser::parse_url_pattern_impl(
314+
friend tl::expected<result_type, errors> parser::parse_url_pattern_impl(
318315
std::variant<std::string_view, url_pattern_init> input,
319316
const std::string_view* base_url, const url_pattern_options* options);
320317
};

include/ada/url_pattern_helpers-inl.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "ada/expected.h"
1010
#include "ada/url_pattern.h"
1111
#include "ada/url_pattern_helpers.h"
12+
#include "ada/implementation.h"
1213

1314
namespace ada::url_pattern_helpers {
1415
inline std::string to_string(token_type type) {
@@ -400,14 +401,14 @@ inline void Tokenizer::add_token_with_defaults(token_type type) {
400401
add_token_with_default_length(type, next_index, index);
401402
}
402403

403-
inline ada_warn_unused std::optional<url_pattern_errors>
404+
inline ada_warn_unused std::optional<errors>
404405
Tokenizer::process_tokenizing_error(size_t next_position,
405406
size_t value_position) {
406407
// If tokenizer’s policy is "strict", then throw a TypeError.
407408
if (policy == token_policy::STRICT) {
408409
ada_log("process_tokenizing_error failed with next_position=",
409410
next_position, " value_position=", value_position);
410-
return url_pattern_errors::type_error;
411+
return errors::type_error;
411412
}
412413
// Assert: tokenizer’s policy is "lenient".
413414
ADA_ASSERT_TRUE(policy == token_policy::LENIENT);
@@ -493,7 +494,7 @@ bool url_pattern_parser<F>::consume_required_token(token_type type) {
493494
}
494495

495496
template <url_pattern_encoding_callback F>
496-
std::optional<url_pattern_errors>
497+
std::optional<errors>
497498
url_pattern_parser<F>::maybe_add_part_from_the_pending_fixed_value() {
498499
// If parser’s pending fixed value is the empty string, then return.
499500
if (pending_fixed_value.empty()) {
@@ -519,7 +520,7 @@ url_pattern_parser<F>::maybe_add_part_from_the_pending_fixed_value() {
519520
}
520521

521522
template <url_pattern_encoding_callback F>
522-
std::optional<url_pattern_errors> url_pattern_parser<F>::add_part(
523+
std::optional<errors> url_pattern_parser<F>::add_part(
523524
std::string_view prefix, Token* name_token, Token* regexp_or_wildcard_token,
524525
std::string_view suffix, Token* modifier_token) {
525526
// Let modifier be "none".
@@ -616,7 +617,7 @@ std::optional<url_pattern_errors> url_pattern_parser<F>::add_part(
616617
// true, then throw a TypeError.
617618
if (std::ranges::any_of(
618619
parts, [&name](const auto& part) { return part.name == name; })) {
619-
return url_pattern_errors::type_error;
620+
return errors::type_error;
620621
}
621622
// Let encoded prefix be the result of running parser’s encoding callback
622623
// given prefix.
@@ -636,10 +637,9 @@ std::optional<url_pattern_errors> url_pattern_parser<F>::add_part(
636637
}
637638

638639
template <url_pattern_encoding_callback F>
639-
tl::expected<std::vector<url_pattern_part>, url_pattern_errors>
640-
parse_pattern_string(std::string_view input,
641-
url_pattern_compile_component_options& options,
642-
F& encoding_callback) {
640+
tl::expected<std::vector<url_pattern_part>, errors> parse_pattern_string(
641+
std::string_view input, url_pattern_compile_component_options& options,
642+
F& encoding_callback) {
643643
ada_log("parse_pattern_string input=", input);
644644
// Let parser be a new pattern parser whose encoding callback is encoding
645645
// callback and segment wildcard regexp is the result of running generate a
@@ -732,7 +732,7 @@ parse_pattern_string(std::string_view input,
732732
// Run consume a required token given parser and "close".
733733
if (!parser.consume_required_token(token_type::CLOSE)) {
734734
ada_log("parser.consume_required_token failed");
735-
return tl::unexpected(url_pattern_errors::type_error);
735+
return tl::unexpected(errors::type_error);
736736
}
737737
// Set modifier token to the result of running try to consume a modifier
738738
// token given parser.
@@ -754,7 +754,7 @@ parse_pattern_string(std::string_view input,
754754
}
755755
// Run consume a required token given parser and "end".
756756
if (!parser.consume_required_token(token_type::END)) {
757-
return tl::unexpected(url_pattern_errors::type_error);
757+
return tl::unexpected(errors::type_error);
758758
}
759759
}
760760
ada_log("parser.parts size is: ", parser.parts.size());

include/ada/url_pattern_helpers.h

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ class url_pattern_parser {
7878
bool consume_required_token(token_type type);
7979
// @see
8080
// https://urlpattern.spec.whatwg.org/#maybe-add-a-part-from-the-pending-fixed-value
81-
std::optional<url_pattern_errors>
82-
maybe_add_part_from_the_pending_fixed_value() ada_warn_unused;
81+
std::optional<errors> maybe_add_part_from_the_pending_fixed_value()
82+
ada_warn_unused;
8383
// @see https://urlpattern.spec.whatwg.org/#add-a-part
84-
std::optional<url_pattern_errors> add_part(
85-
std::string_view prefix, Token* name_token,
86-
Token* regexp_or_wildcard_token, std::string_view suyffix,
87-
Token* modifier_token) ada_warn_unused;
84+
std::optional<errors> add_part(std::string_view prefix, Token* name_token,
85+
Token* regexp_or_wildcard_token,
86+
std::string_view suyffix,
87+
Token* modifier_token) ada_warn_unused;
8888

8989
std::vector<Token> tokens{};
9090
F& encoding_callback;
@@ -121,7 +121,7 @@ class Tokenizer {
121121
void add_token_with_defaults(token_type type);
122122

123123
// @see https://urlpattern.spec.whatwg.org/#process-a-tokenizing-error
124-
std::optional<url_pattern_errors> process_tokenizing_error(
124+
std::optional<errors> process_tokenizing_error(
125125
size_t next_position, size_t value_position) ada_warn_unused;
126126

127127
// has an associated input, a pattern string, initially the empty string.
@@ -154,8 +154,7 @@ struct constructor_string_parser {
154154
bool is_search_prefix();
155155

156156
// @see https://urlpattern.spec.whatwg.org/#parse-a-constructor-string
157-
static tl::expected<url_pattern_init, url_pattern_errors> parse(
158-
std::string_view input);
157+
static tl::expected<url_pattern_init, errors> parse(std::string_view input);
159158

160159
// @see https://urlpattern.spec.whatwg.org/#constructor-string-parser-state
161160
enum class State {
@@ -186,8 +185,7 @@ struct constructor_string_parser {
186185

187186
// @see
188187
// https://urlpattern.spec.whatwg.org/#compute-protocol-matches-a-special-scheme-flag
189-
std::optional<url_pattern_errors>
190-
compute_protocol_matches_special_scheme_flag();
188+
std::optional<errors> compute_protocol_matches_special_scheme_flag();
191189

192190
// @see https://urlpattern.spec.whatwg.org/#next-is-authority-slashes
193191
bool next_is_authority_slashes();
@@ -247,52 +245,44 @@ struct constructor_string_parser {
247245
};
248246

249247
// @see https://urlpattern.spec.whatwg.org/#canonicalize-a-protocol
250-
tl::expected<std::string, url_pattern_errors> canonicalize_protocol(
251-
std::string_view input);
248+
tl::expected<std::string, errors> canonicalize_protocol(std::string_view input);
252249

253250
// @see https://wicg.github.io/urlpattern/#canonicalize-a-username
254-
tl::expected<std::string, url_pattern_errors> canonicalize_username(
255-
std::string_view input);
251+
tl::expected<std::string, errors> canonicalize_username(std::string_view input);
256252

257253
// @see https://wicg.github.io/urlpattern/#canonicalize-a-password
258-
tl::expected<std::string, url_pattern_errors> canonicalize_password(
259-
std::string_view input);
254+
tl::expected<std::string, errors> canonicalize_password(std::string_view input);
260255

261256
// @see https://wicg.github.io/urlpattern/#canonicalize-a-password
262-
tl::expected<std::string, url_pattern_errors> canonicalize_hostname(
263-
std::string_view input);
257+
tl::expected<std::string, errors> canonicalize_hostname(std::string_view input);
264258

265259
// @see https://wicg.github.io/urlpattern/#canonicalize-an-ipv6-hostname
266-
tl::expected<std::string, url_pattern_errors> canonicalize_ipv6_hostname(
260+
tl::expected<std::string, errors> canonicalize_ipv6_hostname(
267261
std::string_view input);
268262

269263
// @see https://wicg.github.io/urlpattern/#canonicalize-a-port
270-
tl::expected<std::string, url_pattern_errors> canonicalize_port(
271-
std::string_view input);
264+
tl::expected<std::string, errors> canonicalize_port(std::string_view input);
272265

273266
// @see https://wicg.github.io/urlpattern/#canonicalize-a-port
274-
tl::expected<std::string, url_pattern_errors> canonicalize_port_with_protocol(
267+
tl::expected<std::string, errors> canonicalize_port_with_protocol(
275268
std::string_view input, std::string_view protocol);
276269

277270
// @see https://wicg.github.io/urlpattern/#canonicalize-a-pathname
278-
tl::expected<std::string, url_pattern_errors> canonicalize_pathname(
279-
std::string_view input);
271+
tl::expected<std::string, errors> canonicalize_pathname(std::string_view input);
280272

281273
// @see https://wicg.github.io/urlpattern/#canonicalize-an-opaque-pathname
282-
tl::expected<std::string, url_pattern_errors> canonicalize_opaque_pathname(
274+
tl::expected<std::string, errors> canonicalize_opaque_pathname(
283275
std::string_view input);
284276

285277
// @see https://wicg.github.io/urlpattern/#canonicalize-a-search
286-
tl::expected<std::string, url_pattern_errors> canonicalize_search(
287-
std::string_view input);
278+
tl::expected<std::string, errors> canonicalize_search(std::string_view input);
288279

289280
// @see https://wicg.github.io/urlpattern/#canonicalize-a-hash
290-
tl::expected<std::string, url_pattern_errors> canonicalize_hash(
291-
std::string_view input);
281+
tl::expected<std::string, errors> canonicalize_hash(std::string_view input);
292282

293283
// @see https://urlpattern.spec.whatwg.org/#tokenize
294-
tl::expected<std::vector<Token>, url_pattern_errors> tokenize(
295-
std::string_view input, token_policy policy);
284+
tl::expected<std::vector<Token>, errors> tokenize(std::string_view input,
285+
token_policy policy);
296286

297287
// @see https://urlpattern.spec.whatwg.org/#process-a-base-url-string
298288
std::string process_base_url_string(std::string_view input,
@@ -310,10 +300,9 @@ constexpr bool is_absolute_pathname(std::string_view input,
310300

311301
// @see https://urlpattern.spec.whatwg.org/#parse-a-pattern-string
312302
template <url_pattern_encoding_callback F>
313-
tl::expected<std::vector<url_pattern_part>, url_pattern_errors>
314-
parse_pattern_string(std::string_view input,
315-
url_pattern_compile_component_options& options,
316-
F& encoding_callback);
303+
tl::expected<std::vector<url_pattern_part>, errors> parse_pattern_string(
304+
std::string_view input, url_pattern_compile_component_options& options,
305+
F& encoding_callback);
317306

318307
// @see https://urlpattern.spec.whatwg.org/#generate-a-pattern-string
319308
std::string generate_pattern_string(

src/implementation.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
namespace ada {
1010

1111
template <class result_type>
12-
ada_warn_unused tl::expected<result_type, ada::errors> parse(
12+
ada_warn_unused tl::expected<result_type, errors> parse(
1313
std::string_view input, const result_type* base_url) {
1414
result_type u =
1515
ada::parser::parse_url_impl<result_type, true>(input, base_url);
1616
if (!u.is_valid) {
17-
return tl::unexpected(errors::generic_error);
17+
return tl::unexpected(errors::type_error);
1818
}
1919
return u;
2020
}
@@ -79,7 +79,7 @@ ada_warn_unused std::string to_string(ada::encoding_type type) {
7979
}
8080
}
8181

82-
ada_warn_unused tl::expected<url_pattern, url_pattern_errors> parse_url_pattern(
82+
ada_warn_unused tl::expected<url_pattern, errors> parse_url_pattern(
8383
std::variant<std::string_view, url_pattern_init> input,
8484
const std::string_view* base_url, const url_pattern_options* options) {
8585
return parser::parse_url_pattern_impl(std::move(input), base_url, options);

0 commit comments

Comments
 (0)