Skip to content

Commit

Permalink
feat: add ada_get_schema_type (#503)
Browse files Browse the repository at this point in the history
* feat: add ada_get_schema_type

* fixup: code review
  • Loading branch information
d3lm authored Sep 5, 2023
1 parent fa4ae29 commit 4c63372
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/ada_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ ada_string ada_get_pathname(ada_url result);
ada_string ada_get_search(ada_url result);
ada_string ada_get_protocol(ada_url result);
uint8_t ada_get_host_type(ada_url result);
uint8_t ada_get_schema_type(ada_url result);

// url_aggregator setters
// if ada_is_valid(result)) is false, the setters have no effect
Expand Down
8 changes: 8 additions & 0 deletions src/ada_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,14 @@ uint8_t ada_get_host_type(ada_url result) noexcept {
return r->host_type;
}

uint8_t ada_get_schema_type(ada_url result) noexcept {
ada::result<ada::url_aggregator>& r = get_instance(result);
if (!r) {
return 0;
}
return r->type;
}

bool ada_set_href(ada_url result, const char* input, size_t length) noexcept {
ada::result<ada::url_aggregator>& r = get_instance(result);
if (!r) {
Expand Down
49 changes: 49 additions & 0 deletions tests/ada_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,52 @@ TEST(ada_c, ada_clear_search) {
ada_free(out);
SUCCEED();
}

TEST(ada_c, ada_get_schema_type) {
std::string_view input;
ada_url out;

input = "http://www.google.com";
out = ada_parse(input.data(), input.size());
ASSERT_TRUE(ada_is_valid(out));
ASSERT_EQ(ada_get_schema_type(out), 0);

input = "notspecial://www.google.com";
ada_free(out);
out = ada_parse(input.data(), input.size());
ASSERT_TRUE(ada_is_valid(out));
ASSERT_EQ(ada_get_schema_type(out), 1);

input = "https://www.google.com";
ada_free(out);
out = ada_parse(input.data(), input.size());
ASSERT_TRUE(ada_is_valid(out));
ASSERT_EQ(ada_get_schema_type(out), 2);

input = "ws://www.google.com/ws";
ada_free(out);
out = ada_parse(input.data(), input.size());
ASSERT_TRUE(ada_is_valid(out));
ASSERT_EQ(ada_get_schema_type(out), 3);

input = "ftp://www.google.com/file.txt";
ada_free(out);
out = ada_parse(input.data(), input.size());
ASSERT_TRUE(ada_is_valid(out));
ASSERT_EQ(ada_get_schema_type(out), 4);

input = "wss://www.google.com/wss";
ada_free(out);
out = ada_parse(input.data(), input.size());
ASSERT_TRUE(ada_is_valid(out));
ASSERT_EQ(ada_get_schema_type(out), 5);

input = "file:///foo/bar";
ada_free(out);
out = ada_parse(input.data(), input.size());
ASSERT_TRUE(ada_is_valid(out));
ASSERT_EQ(ada_get_schema_type(out), 6);

ada_free(out);
SUCCEED();
}

0 comments on commit 4c63372

Please sign in to comment.