@@ -6,6 +6,7 @@ const Direction = enum { Left, Right };
66const RotationParseError = error {
77 InvalidDirection ,
88 InvalidAmount ,
9+ InvalidLength ,
910};
1011
1112fn Rotation (comptime T : type ) type {
@@ -16,6 +17,9 @@ fn Rotation(comptime T: type) type {
1617}
1718
1819fn parseFromString (comptime T : type , input : []const u8 ) RotationParseError ! Rotation (T ) {
20+ if (input .len < 2 ) {
21+ return RotationParseError .InvalidLength ;
22+ }
1923 const direction : Direction = switch (input [0 ]) {
2024 'L' = > .Left ,
2125 'R' = > .Right ,
@@ -66,7 +70,7 @@ test "parse to struct" {
6670
6771test "parse to struct with error" {
6872 const asText = "G23" ;
69- try std .testing .expectError (error .InvalidDirection , parseFromString (u32 , asText ));
73+ try std .testing .expectError (RotationParseError .InvalidDirection , parseFromString (u32 , asText ));
7074}
7175
7276test "parse multiple lines" {
@@ -93,5 +97,10 @@ test "parse multiple lines with error fails early" {
9397 \\R25
9498 ;
9599 const allocator = std .testing .allocator ;
96- try std .testing .expectError (error .InvalidDirection , parseAllLines (u32 , allocator , input ));
100+ try std .testing .expectError (RotationParseError .InvalidDirection , parseAllLines (u32 , allocator , input ));
101+ }
102+
103+ test "parse with invalid length" {
104+ const asText = "G" ;
105+ try std .testing .expectError (RotationParseError .InvalidLength , parseFromString (u32 , asText ));
97106}
0 commit comments