Function parse [src]

Converts UTF-8 text to a BuildId.

Prototype

pub fn parse(text: []const u8) !BuildId

Parameters

text: []const u8

Example

test parse { try std.testing.expectEqual(BuildId.md5, try parse("md5")); try std.testing.expectEqual(BuildId.none, try parse("none")); try std.testing.expectEqual(BuildId.fast, try parse("fast")); try std.testing.expectEqual(BuildId.uuid, try parse("uuid")); try std.testing.expectEqual(BuildId.sha1, try parse("sha1")); try std.testing.expectEqual(BuildId.sha1, try parse("tree")); try std.testing.expect(BuildId.initHexString("").eql(try parse("0x"))); try std.testing.expect(BuildId.initHexString("\x12\x34\x56").eql(try parse("0x123456"))); try std.testing.expectError(error.InvalidLength, parse("0x12-34")); try std.testing.expectError(error.InvalidCharacter, parse("0xfoobbb")); try std.testing.expectError(error.InvalidBuildIdStyle, parse("yaddaxxx")); }

Source

pub fn parse(text: []const u8) !BuildId { if (std.mem.eql(u8, text, "none")) { return .none; } else if (std.mem.eql(u8, text, "fast")) { return .fast; } else if (std.mem.eql(u8, text, "uuid")) { return .uuid; } else if (std.mem.eql(u8, text, "sha1") or std.mem.eql(u8, text, "tree")) { return .sha1; } else if (std.mem.eql(u8, text, "md5")) { return .md5; } else if (std.mem.startsWith(u8, text, "0x")) { var result: BuildId = .{ .hexstring = undefined }; const slice = try std.fmt.hexToBytes(&result.hexstring.bytes, text[2..]); result.hexstring.len = @as(u8, @intCast(slice.len)); return result; } return error.InvalidBuildIdStyle; }