Improve the DX of TrackedString

This commit is contained in:
Lyssieth 2024-12-07 04:03:17 +02:00
parent e9b80e659c
commit 4d73010238
Signed by untrusted user who does not match committer: lyssieth
GPG key ID: 200268854934CFAB
3 changed files with 11 additions and 10 deletions

View file

@ -1,7 +1,7 @@
const std = @import("std"); const std = @import("std");
const cham = @import("chameleon"); const cham = @import("chameleon");
const TrackedString = @import("../util/trackedString.zig").TrackedString; const TrackedString = @import("../util/utils.zig").TrackedString;
const log = std.log; const log = std.log;
@ -238,13 +238,13 @@ test "logFn works" {
defer deinit(); defer deinit();
try config.addScope(.{ try config.addScope(.{
.scope = TrackedString.initConst("someScope"), .scope = TrackedString.constant("someScope"),
.rename = TrackedString.initConst("some rename"), .rename = TrackedString.constant("some rename"),
.color = .green, .color = .green,
}); });
try config.addScope(.{ try config.addScope(.{
.scope = TrackedString.initConst("other"), .scope = TrackedString.constant("other"),
.rename = TrackedString.initConst("other rename"), .rename = TrackedString.constant("other rename"),
.color = .blue, .color = .blue,
}); });

View file

@ -4,14 +4,14 @@ pub const TrackedString = struct {
data: []const u8, data: []const u8,
kind: AllocKind, kind: AllocKind,
pub fn initAlloc(value: []const u8, allocator: std.mem.Allocator) !TrackedString { pub fn alloc(value: []const u8, allocator: std.mem.Allocator) !TrackedString {
return .{ return .{
.data = try allocator.dupe(u8, value), .data = try allocator.dupe(u8, value),
.kind = .{ .Allocated = allocator }, .kind = .{ .Allocated = allocator },
}; };
} }
pub fn initConst(comptime value: []const u8) TrackedString { pub fn constant(comptime value: []const u8) TrackedString {
return .{ return .{
.data = value, .data = value,
.kind = .{ .Constant = {} }, .kind = .{ .Constant = {} },
@ -64,13 +64,13 @@ const t = std.testing;
test "the different kinds work" { test "the different kinds work" {
const a = t.allocator; const a = t.allocator;
var strOne = try TrackedString.initAlloc("hello, world", a); var strOne = try TrackedString.alloc("hello, world", a);
defer strOne.deinit(); defer strOne.deinit();
try t.expectEqualStrings("hello, world", strOne.data); try t.expectEqualStrings("hello, world", strOne.data);
try t.expectEqual(AllocKind{ .Allocated = a }, strOne.kind); try t.expectEqual(AllocKind{ .Allocated = a }, strOne.kind);
var strTwo = TrackedString.initConst("hello, world"); var strTwo = TrackedString.constant("hello, world");
defer strTwo.deinit(); defer strTwo.deinit();
try t.expectEqualStrings("hello, world", strTwo.data); try t.expectEqualStrings("hello, world", strTwo.data);

View file

@ -1,4 +1,5 @@
pub const str = @import("trackedString.zig"); const str = @import("./trackedString.zig");
pub const TrackedString = str.TrackedString;
comptime { comptime {
const std = @import("std"); const std = @import("std");