Skip to content

Commit

Permalink
de: Fix std.Uri deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ibokuri committed Apr 21, 2024
1 parent 805c4be commit c8967a0
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 21 deletions.
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
},
.dependencies = .{
.protest = .{
.url = "https://github.com/ibokuri/protest/archive/bae398b701e763ef18480aa3cfcca103716996de.tar.gz",
.hash = "1220c70517d1bc1b3734c9c4fe81045b1927320b2590d8af28be32883f5fd4d3af8d",
.url = "https://github.com/ibokuri/protest/archive/339da6d86a9f1d00178e2b697d04ac8d2ba3f453.tar.gz",
.hash = "122023b5e4aa0d3dde9cbdcd09f3ac529ac37a10c2b26be82f1caf6d3792328fc9bd",
},
},
}
28 changes: 17 additions & 11 deletions src/de/blocks/uri.zig
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ test "deserialize - std.Uri" {
.scheme = "foo",
.user = null,
.password = null,
.host = "example.com",
.host = .{ .raw = "example.com" },
.port = 8042,
.path = "/over/there",
.query = "name=ferret",
.fragment = "nose",
.path = .{ .raw = "/over/there" },
.query = .{ .raw = "name=ferret" },
.fragment = .{ .raw = "nose" },
},
},
.{
Expand All @@ -65,7 +65,7 @@ test "deserialize - std.Uri" {
.password = null,
.host = null,
.port = null,
.path = "example:animal:ferret:nose",
.path = .{ .raw = "example:animal:ferret:nose" },
.query = null,
.fragment = null,
},
Expand All @@ -88,25 +88,31 @@ test "deserialize - std.Uri" {
defer result.deinit();

try require.equalf(t.want.scheme, result.value.scheme, "Test case: {s}", .{t.name});
try require.equalf(t.want.port, result.value.port, "Test case: {s}", .{t.name});
try require.equalf(t.want.path, result.value.path, "Test case: {s}", .{t.name});

if (t.want.host) |host| {
try require.notNullf(result.value.host, "Test case: {s}", .{t.name});
try require.equalf(host, result.value.host.?, "Test case: {s}", .{t.name});
}
if (t.want.user) |user| {
try require.notNullf(result.value.user, "Test case: {s}", .{t.name});
try require.equalf(user, result.value.user.?, "Test case: {s}", .{t.name});
}

if (t.want.password) |password| {
try require.notNullf(result.value.password, "Test case: {s}", .{t.name});
try require.equalf(password, result.value.password.?, "Test case: {s}", .{t.name});
}

if (t.want.host) |host| {
try require.notNullf(result.value.host, "Test case: {s}", .{t.name});
try require.equalf(host, result.value.host.?, "Test case: {s}", .{t.name});
}

try require.equalf(t.want.port, result.value.port, "Test case: {s}", .{t.name});

try require.equalf(t.want.path, result.value.path, "Test case: {s}", .{t.name});

if (t.want.query) |query| {
try require.notNullf(result.value.query, "Test case: {s}", .{t.name});
try require.equalf(query, result.value.query.?, "Test case: {s}", .{t.name});
}

if (t.want.fragment) |fragment| {
try require.notNullf(result.value.fragment, "Test case: {s}", .{t.name});
try require.equalf(fragment, result.value.fragment.?, "Test case: {s}", .{t.name});
Expand Down
63 changes: 55 additions & 8 deletions src/de/impls/visitor/uri.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,69 @@ fn visitString(
.heap => used = true,
.stack, .managed => {
uri.scheme = try ally.dupe(u8, uri.scheme);
uri.path = try ally.dupe(u8, uri.path);

if (uri.host) |host| {
uri.host = try ally.dupe(u8, host);
}
if (uri.user) |user| {
uri.user = try ally.dupe(u8, user);
uri.user = switch (user) {
.raw => |v| std.Uri.Component{
.raw = try ally.dupe(u8, v),
},
.percent_encoded => |v| std.Uri.Component{
.percent_encoded = try ally.dupe(u8, v),
},
};
}

if (uri.password) |password| {
uri.password = try ally.dupe(u8, password);
uri.password = switch (password) {
.raw => |v| std.Uri.Component{
.raw = try ally.dupe(u8, v),
},
.percent_encoded => |v| std.Uri.Component{
.percent_encoded = try ally.dupe(u8, v),
},
};
}

if (uri.host) |host| {
uri.host = switch (host) {
.raw => |v| std.Uri.Component{
.raw = try ally.dupe(u8, v),
},
.percent_encoded => |v| std.Uri.Component{
.percent_encoded = try ally.dupe(u8, v),
},
};
}

uri.path = switch (uri.path) {
.raw => |v| std.Uri.Component{
.raw = try ally.dupe(u8, v),
},
.percent_encoded => |v| std.Uri.Component{
.percent_encoded = try ally.dupe(u8, v),
},
};

if (uri.query) |query| {
uri.query = try ally.dupe(u8, query);
uri.query = switch (query) {
.raw => |v| std.Uri.Component{
.raw = try ally.dupe(u8, v),
},
.percent_encoded => |v| std.Uri.Component{
.percent_encoded = try ally.dupe(u8, v),
},
};
}

if (uri.fragment) |fragment| {
uri.fragment = try ally.dupe(u8, fragment);
uri.fragment = switch (fragment) {
.raw => |v| std.Uri.Component{
.raw = try ally.dupe(u8, v),
},
.percent_encoded => |v| std.Uri.Component{
.percent_encoded = try ally.dupe(u8, v),
},
};
}
},
}
Expand Down

0 comments on commit c8967a0

Please sign in to comment.