blob: 0aa0354bfc23988164d58d7f7baf1bd339982e44 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
const std = @import("std");
const Self = @This();
name: []const u8,
value: []const u8,
pub fn from(attributes: anytype) []const Self {
const T = @TypeOf(attributes);
const info = @typeInfo(T);
if (info != .@"struct") {
@compileError("struct needed");
}
const fields = info.@"struct".field_names;
var attrs: []const Self = &.{};
for (fields) |name| {
const value = @field(attributes, name);
const next_attr: []const Self = &.{.{ .name = name, .value = value }};
attrs = attrs ++ next_attr;
}
return attrs;
}
pub fn format(self: *const Self, writer: *std.Io.Writer) !void {
try writer.writeAll(self.name);
try writer.writeAll("=\"");
var value: []const u8 = self.value;
while (std.mem.findScalar(u8, value, '"')) |index| {
try writer.writeAll(value[0..index]);
try writer.writeAll(""");
value = value[index + 1..];
}
try writer.writeAll(value);
try writer.writeByte('"');
}
|