Skip to content

Commit 9deb524

Browse files
committed
introduce ada-url to build system
Also add ada-url bindings.
1 parent fb6fbff commit 9deb524

File tree

3 files changed

+204
-0
lines changed

3 files changed

+204
-0
lines changed

build.zig

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ fn addDependencies(b: *Build, mod: *Build.Module, opts: *Build.Step.Options) !vo
384384
try buildMbedtls(b, mod);
385385
try buildNghttp2(b, mod);
386386
try buildCurl(b, mod);
387+
try buildAda(b, mod);
387388

388389
switch (target.result.os.tag) {
389390
.macos => {
@@ -849,3 +850,34 @@ fn buildCurl(b: *Build, m: *Build.Module) !void {
849850
},
850851
});
851852
}
853+
854+
pub fn buildAda(b: *Build, m: *Build.Module) !void {
855+
const ada_dep = b.dependency("ada-singleheader", .{});
856+
857+
const ada_mod = b.createModule(.{
858+
.root_source_file = b.path("vendor/ada/root.zig"),
859+
});
860+
861+
const ada_lib = b.addLibrary(.{
862+
.name = "ada",
863+
.root_module = b.createModule(.{
864+
.link_libcpp = true,
865+
.target = m.resolved_target,
866+
.optimize = m.optimize,
867+
}),
868+
.linkage = .static,
869+
});
870+
871+
ada_lib.addCSourceFile(.{
872+
.file = ada_dep.path("ada.cpp"),
873+
.flags = &.{ "-std=c++20", "-O3" },
874+
.language = .cpp,
875+
});
876+
877+
ada_lib.installHeader(ada_dep.path("ada_c.h"), "ada_c.h");
878+
879+
// Link the library to ada module.
880+
ada_mod.linkLibrary(ada_lib);
881+
// Expose ada module to main module.
882+
m.addImport("ada", ada_mod);
883+
}

build.zig.zon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,9 @@
99
.hash = "v8-0.0.0-xddH63bVAwBSEobaUok9J0er1FqsvEujCDDVy6ItqKQ5",
1010
},
1111
//.v8 = .{ .path = "../zig-v8-fork" }
12+
.@"ada-singleheader" = .{
13+
.url = "https://github.com/ada-url/ada/releases/download/v3.3.0/singleheader.zip",
14+
.hash = "N-V-__8AAPmhFAAw64ALjlzd5YMtzpSrmZ6KymsT84BKfB4s",
15+
},
1216
},
1317
}

vendor/ada/root.zig

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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+
pub const URLComponents = c.ada_url_components;
9+
pub const URLOmitted = c.ada_url_omitted;
10+
pub const String = c.ada_string;
11+
pub const OwnedString = c.ada_owned_string;
12+
/// Pointer types.
13+
pub const URL = c.ada_url;
14+
pub const URLSearchParams = c.ada_url_search_params;
15+
16+
pub const ParseError = error{Invalid};
17+
18+
pub fn parse(input: []const u8) ParseError!URL {
19+
const url = c.ada_parse(input.ptr, input.len);
20+
if (!c.ada_is_valid(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+
return error.Invalid;
31+
}
32+
33+
return url;
34+
}
35+
36+
pub inline fn getComponents(url: URL) *const URLComponents {
37+
return c.ada_get_components(url);
38+
}
39+
40+
pub inline fn free(url: URL) void {
41+
return c.ada_free(url);
42+
}
43+
44+
pub inline fn freeOwnedString(owned: OwnedString) void {
45+
return c.ada_free_owned_string(owned);
46+
}
47+
48+
/// Returns true if given URL is valid.
49+
pub inline fn isValid(url: URL) bool {
50+
return c.ada_is_valid(url);
51+
}
52+
53+
/// Creates a new `URL` from given `URL`.
54+
pub inline fn copy(url: URL) URL {
55+
return c.ada_copy(url);
56+
}
57+
58+
/// Contrary to other getters, this heap allocates.
59+
pub inline fn getOriginNullable(url: URL) OwnedString {
60+
return c.ada_get_origin(url);
61+
}
62+
63+
pub inline fn getHrefNullable(url: URL) String {
64+
return c.ada_get_href(url);
65+
}
66+
67+
pub inline fn getUsernameNullable(url: URL) String {
68+
return c.ada_get_username(url);
69+
}
70+
71+
pub inline fn getPasswordNullable(url: URL) String {
72+
return c.ada_get_password(url);
73+
}
74+
75+
pub inline fn getSearchNullable(url: URL) String {
76+
return c.ada_get_search(url);
77+
}
78+
79+
pub inline fn getPortNullable(url: URL) String {
80+
return c.ada_get_port(url);
81+
}
82+
83+
pub inline fn getHashNullable(url: URL) String {
84+
return c.ada_get_hash(url);
85+
}
86+
87+
pub inline fn getHostNullable(url: URL) String {
88+
return c.ada_get_host(url);
89+
}
90+
91+
pub inline fn getHostnameNullable(url: URL) String {
92+
return c.ada_get_hostname(url);
93+
}
94+
95+
pub inline fn getPathnameNullable(url: URL) String {
96+
return c.ada_get_pathname(url);
97+
}
98+
99+
pub inline fn getProtocolNullable(url: URL) String {
100+
return c.ada_get_protocol(url);
101+
}
102+
103+
pub inline fn setHref(url: URL, input: []const u8) bool {
104+
return c.ada_set_href(url, input.ptr, input.len);
105+
}
106+
107+
pub inline fn setHost(url: URL, input: []const u8) bool {
108+
return c.ada_set_host(url, input.ptr, input.len);
109+
}
110+
111+
pub inline fn setHostname(url: URL, input: []const u8) bool {
112+
return c.ada_set_hostname(url, input.ptr, input.len);
113+
}
114+
115+
pub inline fn setProtocol(url: URL, input: []const u8) bool {
116+
return c.ada_set_protocol(url, input.ptr, input.len);
117+
}
118+
119+
pub inline fn setUsername(url: URL, input: []const u8) bool {
120+
return c.ada_set_username(url, input.ptr, input.len);
121+
}
122+
123+
pub inline fn setPassword(url: URL, input: []const u8) bool {
124+
return c.ada_set_password(url, input.ptr, input.len);
125+
}
126+
127+
pub inline fn setPort(url: URL, input: []const u8) bool {
128+
return c.ada_set_port(url, input.ptr, input.len);
129+
}
130+
131+
pub inline fn setPathname(url: URL, input: []const u8) bool {
132+
return c.ada_set_pathname(url, input.ptr, input.len);
133+
}
134+
135+
pub inline fn setSearch(url: URL, input: []const u8) void {
136+
return c.ada_set_search(url, input.ptr, input.len);
137+
}
138+
139+
pub inline fn setHash(url: URL, input: []const u8) void {
140+
return c.ada_set_hash(url, input.ptr, input.len);
141+
}
142+
143+
pub inline fn clearHash(url: URL) void {
144+
return c.ada_clear_hash(url);
145+
}
146+
147+
pub inline fn clearSearch(url: URL) void {
148+
return c.ada_clear_search(url);
149+
}
150+
151+
pub inline fn clearPort(url: URL) void {
152+
return c.ada_clear_port(url);
153+
}
154+
155+
pub const Scheme = struct {
156+
pub const http: u8 = 0;
157+
pub const not_special: u8 = 1;
158+
pub const https: u8 = 2;
159+
pub const ws: u8 = 3;
160+
pub const ftp: u8 = 4;
161+
pub const wss: u8 = 5;
162+
pub const file: u8 = 6;
163+
};
164+
165+
/// Returns one of the constants defined in `Scheme`.
166+
pub inline fn getSchemeType(url: URL) u8 {
167+
return c.ada_get_scheme_type(url);
168+
}

0 commit comments

Comments
 (0)