Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
reorg the headers for a cleaning design
  • Loading branch information
lemire committed Jan 24, 2025
commit 2fbdd48784b3e3f87c27eaa9584df7803d58e65d
12 changes: 12 additions & 0 deletions include/ada/errors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @file errors.h
* @brief Definitions for the errors.
*/
#ifndef ADA_ERRORS_H
#define ADA_ERRORS_H

#include <cstdint>
namespace ada {
enum class errors : uint8_t { type_error };
} // namespace ada
#endif // ADA_ERRORS_H
3 changes: 2 additions & 1 deletion include/ada/implementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@

#include "ada/url.h"
#include "ada/common_defs.h"
#include "ada/errors.h"
#include "ada/url_pattern_init.h"

namespace ada {
enum class errors : uint8_t { type_error };

template <class result_type = ada::url_aggregator>
using result = tl::expected<result_type, ada::errors>;
Expand Down
2 changes: 1 addition & 1 deletion include/ada/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "ada/expected.h"
#include "ada/url_pattern_regex.h"
#include "ada/url_pattern_init.h"

/**
* @private
Expand All @@ -20,7 +21,6 @@ struct url;
template <url_pattern_regex::regex_concept regex_provider>
class url_pattern;
struct url_pattern_options;
struct url_pattern_init;
enum class errors : uint8_t;
} // namespace ada

Expand Down
81 changes: 4 additions & 77 deletions include/ada/url_pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,88 +18,13 @@
#endif // ADA_TESTING

namespace ada {

namespace parser {
template <typename result_type, typename url_pattern_init,
typename url_pattern_options, typename regex_provider>
tl::expected<result_type, errors> parse_url_pattern_impl(
std::variant<std::string_view, url_pattern_init> input,
const std::string_view* base_url, const url_pattern_options* options);
}

// Important: C++20 allows us to use concept rather than `using` or `typedef
// and allows functions with second argument, which is optional (using either
// std::nullopt or a parameter with default value)
template <typename F>
concept url_pattern_encoding_callback = requires(F f, std::string_view sv) {
{ f(sv) } -> std::same_as<tl::expected<std::string, errors>>;
};

// A structure providing matching patterns for individual components
// of a URL. When a URLPattern is created, or when a URLPattern is
// used to match or test against a URL, the input can be given as
// either a string or a URLPatternInit struct. If a string is given,
// it will be parsed to create a URLPatternInit. The URLPatternInit
// API is defined as part of the URLPattern specification.
struct url_pattern_init {
// @see https://urlpattern.spec.whatwg.org/#process-a-urlpatterninit
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);

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

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

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

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

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

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

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

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

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

bool operator==(const url_pattern_init&) const;

std::optional<std::string> protocol{};
std::optional<std::string> username{};
std::optional<std::string> password{};
std::optional<std::string> hostname{};
std::optional<std::string> port{};
std::optional<std::string> pathname{};
std::optional<std::string> search{};
std::optional<std::string> hash{};
std::optional<std::string> base_url{};
};
} // namespace parser

enum class url_pattern_part_type : uint8_t {
// The part represents a simple fixed text string.
Expand Down Expand Up @@ -330,12 +255,14 @@ class url_pattern {
bool ignore_case_ = false;

template <typename result_type, typename url_pattern_init,
typename url_pattern_options>
typename url_pattern_options, typename regex_provider>
friend tl::expected<result_type, errors> parser::parse_url_pattern_impl(
std::variant<std::string_view, url_pattern_init> input,
const std::string_view* base_url, const url_pattern_options* options);
};



} // namespace ada

#endif
91 changes: 91 additions & 0 deletions include/ada/url_pattern_init.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* @file url_pattern_init.h
* @brief Declaration for the url_pattern_init implementation.
*/
#ifndef ADA_URL_PATTERN_INIT_H
#define ADA_URL_PATTERN_INIT_H

#include "ada/expected.h"
#include "ada/errors.h"
#include <string_view>
#include <string>
#include <optional>

namespace ada {

// Important: C++20 allows us to use concept rather than `using` or `typedef
// and allows functions with second argument, which is optional (using either
// std::nullopt or a parameter with default value)
template <typename F>
concept url_pattern_encoding_callback = requires(F f, std::string_view sv) {
{ f(sv) } -> std::same_as<tl::expected<std::string, errors>>;
};

// A structure providing matching patterns for individual components
// of a URL. When a URLPattern is created, or when a URLPattern is
// used to match or test against a URL, the input can be given as
// either a string or a URLPatternInit struct. If a string is given,
// it will be parsed to create a URLPatternInit. The URLPatternInit
// API is defined as part of the URLPattern specification.
struct url_pattern_init {
// @see https://urlpattern.spec.whatwg.org/#process-a-urlpatterninit
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);

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

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

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

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

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

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

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

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

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

bool operator==(const url_pattern_init&) const;

std::optional<std::string> protocol{};
std::optional<std::string> username{};
std::optional<std::string> password{};
std::optional<std::string> hostname{};
std::optional<std::string> port{};
std::optional<std::string> pathname{};
std::optional<std::string> search{};
std::optional<std::string> hash{};
std::optional<std::string> base_url{};
};
} // namespace ada

#endif // ADA_URL_PATTERN_INIT_H
Loading