Improve format and re-add docstrings

Implements RP suggestions.
This commit is contained in:
Karl Seguin
2025-02-06 09:57:04 +08:00
parent 3d66758507
commit fafd8c4af1
2 changed files with 35 additions and 7 deletions

View File

@@ -157,13 +157,25 @@ fn writeEscapedAttributeValue(writer: anytype, value: []const u8) !void {
const testing = std.testing; const testing = std.testing;
test "dump.writeHTML" { test "dump.writeHTML" {
try testWriteHTML("<div id=\"content\">Over 9000!</div>", "<div id=\"content\">Over 9000!</div>"); try testWriteHTML(
"<div id=\"content\">Over 9000!</div>",
"<div id=\"content\">Over 9000!</div>",
);
try testWriteHTML("<root><!-- a comment --></root>", "<root><!-- a comment --></root>"); try testWriteHTML(
"<root><!-- a comment --></root>",
"<root><!-- a comment --></root>",
);
try testWriteHTML("<p>&lt; &gt; &amp;</p>", "<p>&lt; &gt; &amp;</p>"); try testWriteHTML(
"<p>&lt; &gt; &amp;</p>",
"<p>&lt; &gt; &amp;</p>",
);
try testWriteHTML("<p id=\"&quot;&gt;&lt;&amp;&quot;''\">wat?</p>", "<p id='\">&lt;&amp;&quot;&#39;&apos;'>wat?</p>"); try testWriteHTML(
"<p id=\"&quot;&gt;&lt;&amp;&quot;''\">wat?</p>",
"<p id='\">&lt;&amp;&quot;&#39;&apos;'>wat?</p>",
);
try testWriteFullHTML( try testWriteFullHTML(
\\<!DOCTYPE html> \\<!DOCTYPE html>
@@ -173,8 +185,12 @@ test "dump.writeHTML" {
, "<html><title>It's over what?</title><meta name=a value=\"b\">\n<body>9000"); , "<html><title>It's over what?</title><meta name=a value=\"b\">\n<body>9000");
} }
fn testWriteHTML(comptime expected: []const u8, src: []const u8) !void { fn testWriteHTML(comptime expected_body: []const u8, src: []const u8) !void {
return testWriteFullHTML("<!DOCTYPE html>\n<html><head></head><body>" ++ expected ++ "</body></html>\n", src); const expected =
"<!DOCTYPE html>\n<html><head></head><body>" ++
expected_body ++
"</body></html>\n";
return testWriteFullHTML(expected, src);
} }
fn testWriteFullHTML(comptime expected: []const u8, src: []const u8) !void { fn testWriteFullHTML(comptime expected: []const u8, src: []const u8) !void {

View File

@@ -17,8 +17,14 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std"); const std = @import("std");
// ----
const Type = std.builtin.Type; const Type = std.builtin.Type;
// Union
// -----
// Generate a flatten tagged Union from a Tuple
pub fn Union(interfaces: anytype) type { pub fn Union(interfaces: anytype) type {
// @setEvalBranchQuota(10000); // @setEvalBranchQuota(10000);
const tuple = Tuple(interfaces){}; const tuple = Tuple(interfaces){};
@@ -57,7 +63,7 @@ pub fn Union(interfaces: anytype) type {
.decls = &.{}, .decls = &.{},
.is_exhaustive = true, .is_exhaustive = true,
}; };
const enum_T = @Type(std.builtin.Type{ .Enum = enum_info }); const enum_T = @Type(.{ .Enum = enum_info });
// third iteration to generate union type // third iteration to generate union type
var union_fields: [fields.len]Type.UnionField = undefined; var union_fields: [fields.len]Type.UnionField = undefined;
@@ -81,6 +87,12 @@ pub fn Union(interfaces: anytype) type {
} }); } });
} }
// Tuple
// -----
// Flattens and depuplicates a list of nested tuples. For example
// input: {A, B, {C, B, D}, {A, E}}
// output {A, B, C, D, E}
pub fn Tuple(args: anytype) type { pub fn Tuple(args: anytype) type {
@setEvalBranchQuota(100000); @setEvalBranchQuota(100000);