@@ -30,7 +30,7 @@ pub fn getEnumSize(comptime T: type, value: T) usize {
3030 return 1 ; // size of null
3131 }
3232 }
33-
33+
3434 const tag_type = @typeInfo (T ).@"enum" .tag_type ;
3535 const int_value = @intFromEnum (value );
3636 return getIntSize (tag_type , int_value );
@@ -39,23 +39,23 @@ pub fn getEnumSize(comptime T: type, value: T) usize {
3939pub fn packEnum (writer : anytype , comptime T : type , value_or_maybe_null : T ) ! void {
4040 const Type = assertEnumType (T );
4141 const value : Type = try maybePackNull (writer , T , value_or_maybe_null ) orelse return ;
42-
42+
4343 const tag_type = @typeInfo (Type ).@"enum" .tag_type ;
4444 const int_value = @intFromEnum (value );
45-
45+
4646 try packInt (writer , tag_type , int_value );
4747}
4848
4949pub fn unpackEnum (reader : anytype , comptime T : type ) ! T {
5050 const Type = assertEnumType (T );
5151 const tag_type = @typeInfo (Type ).@"enum" .tag_type ;
52-
52+
5353 // Construct the optional tag type to match T's optionality
5454 const OptionalTagType = if (@typeInfo (T ) == .optional ) ? tag_type else tag_type ;
55-
55+
5656 // Use unpackInt directly with the constructed optional tag type
5757 const int_value = try unpackInt (reader , OptionalTagType );
58-
58+
5959 // Handle the optional case
6060 if (@typeInfo (T ) == .optional ) {
6161 if (int_value ) | value | {
@@ -72,15 +72,15 @@ test "getMaxEnumSize" {
7272 const PlainEnum = enum { foo , bar };
7373 const U8Enum = enum (u8 ) { foo = 1 , bar = 2 };
7474 const U16Enum = enum (u16 ) { foo , bar };
75-
75+
7676 try std .testing .expectEqual (2 , getMaxEnumSize (PlainEnum )); // u1 + header
7777 try std .testing .expectEqual (2 , getMaxEnumSize (U8Enum )); // u8 + header
7878 try std .testing .expectEqual (3 , getMaxEnumSize (U16Enum )); // u16 + header
7979}
8080
8181test "getEnumSize" {
8282 const U8Enum = enum (u8 ) { foo = 0 , bar = 150 };
83-
83+
8484 try std .testing .expectEqual (1 , getEnumSize (U8Enum , .foo )); // fits in positive fixint
8585 try std .testing .expectEqual (2 , getEnumSize (U8Enum , .bar )); // requires u8 format
8686}
@@ -89,59 +89,58 @@ test "pack/unpack enum" {
8989 const PlainEnum = enum { foo , bar };
9090 const U8Enum = enum (u8 ) { foo = 1 , bar = 2 };
9191 const U16Enum = enum (u16 ) { alpha = 1000 , beta = 2000 };
92-
92+
9393 // Test plain enum
9494 {
9595 var buffer = std .ArrayList (u8 ){};
9696 defer buffer .deinit (std .testing .allocator );
97-
97+
9898 try packEnum (buffer .writer (std .testing .allocator ), PlainEnum , .bar );
99-
99+
100100 var stream = std .io .fixedBufferStream (buffer .items );
101101 const result = try unpackEnum (stream .reader (), PlainEnum );
102102 try std .testing .expectEqual (PlainEnum .bar , result );
103103 }
104-
104+
105105 // Test enum(u8)
106106 {
107107 var buffer = std .ArrayList (u8 ){};
108108 defer buffer .deinit (std .testing .allocator );
109-
109+
110110 try packEnum (buffer .writer (std .testing .allocator ), U8Enum , .bar );
111-
111+
112112 var stream = std .io .fixedBufferStream (buffer .items );
113113 const result = try unpackEnum (stream .reader (), U8Enum );
114114 try std .testing .expectEqual (U8Enum .bar , result );
115115 }
116-
117- // Test enum(u16)
116+
117+ // Test enum(u16)
118118 {
119119 var buffer = std .ArrayList (u8 ){};
120120 defer buffer .deinit (std .testing .allocator );
121-
121+
122122 try packEnum (buffer .writer (std .testing .allocator ), U16Enum , .alpha );
123-
123+
124124 var stream = std .io .fixedBufferStream (buffer .items );
125125 const result = try unpackEnum (stream .reader (), U16Enum );
126126 try std .testing .expectEqual (U16Enum .alpha , result );
127127 }
128128}
129129
130-
131130test "enum edge cases" {
132131 // Test enum with explicit and auto values
133- const MixedEnum = enum (u8 ) {
132+ const MixedEnum = enum (u8 ) {
134133 first = 10 ,
135134 second , // auto-assigned to 11
136135 third = 20 ,
137136 fourth , // auto-assigned to 21
138137 };
139-
138+
140139 var buffer = std .ArrayList (u8 ){};
141140 defer buffer .deinit (std .testing .allocator );
142-
141+
143142 try packEnum (buffer .writer (std .testing .allocator ), MixedEnum , .second );
144-
143+
145144 var stream = std .io .fixedBufferStream (buffer .items );
146145 const result = try unpackEnum (stream .reader (), MixedEnum );
147146 try std .testing .expectEqual (MixedEnum .second , result );
@@ -151,28 +150,28 @@ test "enum edge cases" {
151150test "optional enum" {
152151 const TestEnum = enum (u8 ) { foo = 1 , bar = 2 };
153152 const OptionalEnum = ? TestEnum ;
154-
153+
155154 // Test non-null optional enum
156155 {
157156 var buffer = std .ArrayList (u8 ){};
158157 defer buffer .deinit (std .testing .allocator );
159-
158+
160159 const value : OptionalEnum = .bar ;
161160 try packEnum (buffer .writer (std .testing .allocator ), OptionalEnum , value );
162-
161+
163162 var stream = std .io .fixedBufferStream (buffer .items );
164163 const result = try unpackEnum (stream .reader (), OptionalEnum );
165164 try std .testing .expectEqual (@as (OptionalEnum , .bar ), result );
166165 }
167-
166+
168167 // Test null optional enum
169168 {
170169 var buffer = std .ArrayList (u8 ){};
171170 defer buffer .deinit (std .testing .allocator );
172-
171+
173172 const value : OptionalEnum = null ;
174173 try packEnum (buffer .writer (std .testing .allocator ), OptionalEnum , value );
175-
174+
176175 var stream = std .io .fixedBufferStream (buffer .items );
177176 const result = try unpackEnum (stream .reader (), OptionalEnum );
178177 try std .testing .expectEqual (@as (OptionalEnum , null ), result );
@@ -182,12 +181,12 @@ test "optional enum" {
182181test "getEnumSize with optional" {
183182 const TestEnum = enum (u8 ) { foo = 0 , bar = 150 };
184183 const OptionalEnum = ? TestEnum ;
185-
184+
186185 // Test non-null optional enum size
187186 const value : OptionalEnum = .bar ;
188187 try std .testing .expectEqual (2 , getEnumSize (OptionalEnum , value )); // requires u8 format
189-
188+
190189 // Test null optional enum size
191190 const null_value : OptionalEnum = null ;
192191 try std .testing .expectEqual (1 , getEnumSize (OptionalEnum , null_value )); // size of null
193- }
192+ }
0 commit comments