const std = @import("std"); const string = []const u8; const Tag = struct { value: string, pub fn new(tag_v: string) Tag { return Tag{ .value = tag_v }; } // Returns an array of tags from a string, expecting comma-delimited // tag1,tag2,tag3,tag4 pub fn fromStr(allocator: std.mem.Allocator, tags: string) ![]Tag { var tags_split = std.mem.tokenizeScalar(u8, tags, ','); var tags_o = std.ArrayList(Tag).init(allocator); errdefer tags_o.deinit(); while (tags_split.next()) |tag| { try tags_o.append(Tag{ .value = tag }); } return tags_o.toOwnedSlice(); } }; test "Tag.new creates tag with value" { const tag = Tag.new("hello"); try std.testing.expectEqualStrings("hello", tag.value); } test "Tag.fromStr parses single tag" { const tags = try Tag.fromStr(std.testing.allocator, "zig"); defer std.testing.allocator.free(tags); try std.testing.expectEqual(@as(usize, 1), tags.len); try std.testing.expectEqualStrings("zig", tags[0].value); } test "Tag.fromStr parses multiple tags" { const tags = try Tag.fromStr(std.testing.allocator, "tag1,tag2,tag3"); defer std.testing.allocator.free(tags); try std.testing.expectEqual(@as(usize, 3), tags.len); try std.testing.expectEqualStrings("tag1", tags[0].value); try std.testing.expectEqualStrings("tag2", tags[1].value); try std.testing.expectEqualStrings("tag3", tags[2].value); } test "Tag.fromStr empty string returns no tags" { const tags = try Tag.fromStr(std.testing.allocator, ""); defer std.testing.allocator.free(tags); try std.testing.expectEqual(@as(usize, 0), tags.len); }