-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframe.zig
More file actions
62 lines (55 loc) · 1.91 KB
/
frame.zig
File metadata and controls
62 lines (55 loc) · 1.91 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//! Frame: one record in the breadcrumb chain. Holds a context message,
//! optional attributes, and a source location captured at the fail
//! site. The frame's strings are owned by the parent Context's arena.
const std = @import("std");
/// One key/value attribute attached to a frame.
pub const Attr = struct {
key: []const u8,
value: AttrValue,
};
/// Tagged value type for attribute payloads. Strings are owned by the
/// frame's arena; primitives are stored inline.
pub const AttrValue = union(enum) {
str: []const u8,
int: i64,
uint: u64,
float: f64,
boolean: bool,
};
/// One breadcrumb frame. `msg` and any string attributes are heap-copied
/// into the owning Context's arena. The frame itself owns no allocations
/// directly: the arena does.
pub const Frame = struct {
msg: []const u8,
attrs: []Attr,
file: []const u8,
line: u32,
};
test "Attr fits expected size and shape" {
const a: Attr = .{ .key = "k", .value = .{ .int = 42 } };
try std.testing.expectEqualStrings("k", a.key);
try std.testing.expectEqual(@as(i64, 42), a.value.int);
}
test "AttrValue covers string, int, uint, float, bool" {
const s: AttrValue = .{ .str = "hello" };
const i: AttrValue = .{ .int = -1 };
const u: AttrValue = .{ .uint = 1 };
const f: AttrValue = .{ .float = 1.5 };
const b: AttrValue = .{ .boolean = true };
try std.testing.expect(s == .str);
try std.testing.expect(i == .int);
try std.testing.expect(u == .uint);
try std.testing.expect(f == .float);
try std.testing.expect(b == .boolean);
}
test "Frame fields are addressable" {
const f: Frame = .{
.msg = "loading config",
.attrs = &.{},
.file = "src/config.zig",
.line = 42,
};
try std.testing.expectEqualStrings("loading config", f.msg);
try std.testing.expectEqual(@as(usize, 0), f.attrs.len);
try std.testing.expectEqual(@as(u32, 42), f.line);
}