Error on invalid string passed to Uuid.parse
This commit is contained in:
parent
f56782a323
commit
a7e4ff1777
1 changed files with 6 additions and 2 deletions
|
@ -23,6 +23,7 @@ pub const Uuid = struct {
|
||||||
data: [16]u8,
|
data: [16]u8,
|
||||||
|
|
||||||
pub const Nil = Uuid{ .data = @bitCast([16]u8, @as(u128, 0)) };
|
pub const Nil = Uuid{ .data = @bitCast([16]u8, @as(u128, 0)) };
|
||||||
|
pub const StringLen = 36;
|
||||||
|
|
||||||
pub fn eql(lhs: Uuid, rhs: Uuid) bool {
|
pub fn eql(lhs: Uuid, rhs: Uuid) bool {
|
||||||
return std.mem.eql(u8, &lhs.data, &rhs.data);
|
return std.mem.eql(u8, &lhs.data, &rhs.data);
|
||||||
|
@ -51,9 +52,10 @@ pub const Uuid = struct {
|
||||||
|
|
||||||
pub const ParseError = error{
|
pub const ParseError = error{
|
||||||
InvalidCharacter,
|
InvalidCharacter,
|
||||||
|
InvalidLength,
|
||||||
};
|
};
|
||||||
pub fn parse(str: []const u8) ParseError!Uuid {
|
pub fn parse(str: []const u8) ParseError!Uuid {
|
||||||
if (str.len < 36) return error.InvalidCharacter;
|
if (str.len != StringLen) return error.InvalidLength;
|
||||||
|
|
||||||
var uuid: Uuid = undefined;
|
var uuid: Uuid = undefined;
|
||||||
var str_i: usize = 0;
|
var str_i: usize = 0;
|
||||||
|
@ -110,8 +112,10 @@ test "format uuid" {
|
||||||
};
|
};
|
||||||
try std.testing.expectFmt("f75f5160-12d3-42c2-a81d-ad2245b7a74b", "{}", .{uuid});
|
try std.testing.expectFmt("f75f5160-12d3-42c2-a81d-ad2245b7a74b", "{}", .{uuid});
|
||||||
|
|
||||||
try std.testing.expectError(error.InvalidCharacter, Uuid.parse("fsdfs"));
|
try std.testing.expectError(error.InvalidLength, Uuid.parse("fsdfs"));
|
||||||
try std.testing.expectError(error.InvalidCharacter, Uuid.parse("00000000-0000-0000-xxxx-000000000000"));
|
try std.testing.expectError(error.InvalidCharacter, Uuid.parse("00000000-0000-0000-xxxx-000000000000"));
|
||||||
|
try std.testing.expectError(error.InvalidLength, Uuid.parse("00000000-0000-0000-0000-000000000000fsdfs"));
|
||||||
|
try std.testing.expectError(error.InvalidCharacter, Uuid.parse("00000000-0000x0000-0000-000000000000"));
|
||||||
}
|
}
|
||||||
|
|
||||||
test "roundtrip random uuid" {
|
test "roundtrip random uuid" {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue