Skip to content

Commit 366bfb9

Browse files
committed
add ada-url wrappers
* also integrate it as module in build.zig rather than direct linking
1 parent 27cadde commit 366bfb9

File tree

2 files changed

+116
-13
lines changed

2 files changed

+116
-13
lines changed

build.zig

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -853,24 +853,24 @@ fn buildCurl(b: *Build, m: *Build.Module) !void {
853853

854854
pub fn buildAda(b: *Build, m: *Build.Module) !void {
855855
const ada_dep = b.dependency("ada-singleheader", .{});
856-
const ada = b.addLibrary(.{
857-
.name = "ada",
858-
.linkage = .static,
859-
.root_module = b.createModule(.{
860-
.target = m.resolved_target,
861-
.optimize = m.optimize,
862-
.link_libcpp = true,
863-
}),
856+
const dep_root = ada_dep.path("");
857+
858+
// Private module that binds ada functions.
859+
const ada_mod = b.createModule(.{
860+
.root_source_file = b.path("vendor/ada/root.zig"),
861+
.target = m.resolved_target,
862+
.optimize = m.optimize,
863+
.link_libcpp = true,
864864
});
865865

866-
// Expose "ada_c.h".
867-
ada.installHeader(ada_dep.path("ada_c.h"), "ada_c.h");
866+
// Expose headers; note that "ada.h" is a C++ header so no use here.
867+
ada_mod.addIncludePath(dep_root);
868868

869-
ada.root_module.addCSourceFiles(.{
870-
.root = ada_dep.path(""),
869+
ada_mod.addCSourceFiles(.{
870+
.root = dep_root,
871871
.files = &.{"ada.cpp"},
872872
.flags = &.{"-std=c++20"},
873873
});
874874

875-
m.linkLibrary(ada);
875+
m.addImport("ada", ada_mod);
876876
}

vendor/ada/root.zig

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
//! Wrappers for ada URL parser.
2+
//! https://github.com/ada-url/ada
3+
4+
const c = @cImport({
5+
@cInclude("ada_c.h");
6+
});
7+
8+
/// Pointer type.
9+
pub const URL = c.ada_url;
10+
pub const String = c.ada_string;
11+
pub const OwnedString = c.ada_owned_string;
12+
/// Pointer type.
13+
pub const URLSearchParams = c.ada_url_search_params;
14+
15+
pub const ParseError = error{Invalid};
16+
17+
pub fn parse(input: []const u8) ParseError!URL {
18+
const url = c.ada_parse(input.ptr, input.len);
19+
if (!c.ada_is_valid(url)) {
20+
free(url);
21+
return error.Invalid;
22+
}
23+
24+
return url;
25+
}
26+
27+
pub fn parseWithBase(input: []const u8, base: []const u8) ParseError!URL {
28+
const url = c.ada_parse_with_base(input.ptr, input.len, base.ptr, base.len);
29+
if (!c.ada_is_valid(url)) {
30+
free(url);
31+
return error.Invalid;
32+
}
33+
34+
return url;
35+
}
36+
37+
pub inline fn free(url: URL) void {
38+
return c.ada_free(url);
39+
}
40+
41+
pub inline fn freeOwnedString(owned: OwnedString) void {
42+
return c.ada_free_owned_string(owned);
43+
}
44+
45+
/// Can return an empty string.
46+
/// Contrary to other getters, returned slice is heap allocated.
47+
pub inline fn getOrigin(url: URL) []const u8 {
48+
const origin = c.ada_get_origin(url);
49+
return origin.data[0..origin.length];
50+
}
51+
52+
/// Can return an empty string.
53+
pub inline fn getHref(url: URL) []const u8 {
54+
const href = c.ada_get_href(url);
55+
return href.data[0..href.length];
56+
}
57+
58+
/// Can return an empty string.
59+
pub inline fn getUsername(url: URL) []const u8 {
60+
const username = c.ada_get_username(url);
61+
return username.data[0..username.length];
62+
}
63+
64+
/// Can return an empty string.
65+
pub inline fn getPassword(url: URL) []const u8 {
66+
const password = c.ada_get_password(url);
67+
return password.data[0..password.length];
68+
}
69+
70+
pub inline fn getPort(url: URL) []const u8 {
71+
const port = c.ada_get_port(url);
72+
return port.data[0..port.length];
73+
}
74+
75+
pub inline fn getHash(url: URL) []const u8 {
76+
const hash = c.ada_get_hash(url);
77+
return hash.data[0..hash.length];
78+
}
79+
80+
pub inline fn getHost(url: URL) []const u8 {
81+
const host = c.ada_get_host(url);
82+
return host.data[0..host.length];
83+
}
84+
85+
pub inline fn getHostname(url: URL) []const u8 {
86+
const hostname = c.ada_get_hostname(url);
87+
return hostname.data[0..hostname.length];
88+
}
89+
90+
pub inline fn getPathname(url: URL) []const u8 {
91+
const pathname = c.ada_get_pathname(url);
92+
return pathname.data[0..pathname.length];
93+
}
94+
95+
pub inline fn getSearch(url: URL) []const u8 {
96+
const search = c.ada_get_search(url);
97+
return search.data[0..search.length];
98+
}
99+
100+
pub inline fn getProtocol(url: URL) []const u8 {
101+
const protocol = c.ada_get_protocol(url);
102+
return protocol.data[0..protocol.length];
103+
}

0 commit comments

Comments
 (0)