Skip to content

Commit 3ea676f

Browse files
Explicitly cover todos for unknown bitstring types
1 parent 9e574fe commit 3ea676f

File tree

4 files changed

+84
-7
lines changed

4 files changed

+84
-7
lines changed

src/compiler/internal/generator/expressions.gleam

+1
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ fn generate_bitstring_segment_option(
201201
|> string_builder.prepend("\"Unit\", ")
202202

203203
python.FloatOption -> string_builder.from_string("\"Float\", None")
204+
python.IntOption -> string_builder.from_string("\"Int\", None")
204205
python.BigOption -> string_builder.from_string("\"Big\", None")
205206
python.LittleOption -> string_builder.from_string("\"Little\", None")
206207
python.NativeOption -> string_builder.from_string("\"Native\", None")

src/compiler/internal/transformer/statements.gleam

+7-7
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ fn fold_bitsting_segment_option(
549549
internal.ReversedList(python.BitStringSegmentOption),
550550
) {
551551
case option {
552+
glance.IntOption -> internal.map_state_prepend(state, python.IntOption)
552553
glance.FloatOption -> internal.map_state_prepend(state, python.FloatOption)
553554
glance.LittleOption ->
554555
internal.map_state_prepend(state, python.LittleOption)
@@ -577,13 +578,12 @@ fn fold_bitsting_segment_option(
577578
)
578579
}
579580

580-
glance.SignedOption | glance.UnsignedOption -> {
581-
panic as "Signed and unsigned are not valid when constructing bitstrings"
582-
}
581+
glance.Utf8CodepointOption
582+
| glance.Utf16CodepointOption
583+
| glance.Utf32CodepointOption ->
584+
todo as "codepoints not supported in bitstrings yet"
583585

584-
_ -> {
585-
pprint.debug(option)
586-
todo as "Some bitstring segment options not supported yet"
587-
}
586+
glance.SignedOption | glance.UnsignedOption | glance.BinaryOption ->
587+
panic as "Signed, unsigned, and binary are not valid when constructing bitstrings"
588588
}
589589
}

src/compiler/python.gleam

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ pub type BitStringSegmentOption {
5959
SizeValueOption(Expression)
6060
UnitOption(Int)
6161
FloatOption
62+
IntOption
6263
LittleOption
6364
BigOption
6465
BitStringOption

test/bitstring_test.gleam

+75
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,21 @@ def main():
183183
)
184184
}
185185

186+
pub fn explicit_int_test() {
187+
"pub fn main() {
188+
<<42:int>>
189+
}
190+
"
191+
|> compiler.compile
192+
|> should.be_ok
193+
|> should.equal(
194+
"from gleam_builtins import *
195+
196+
def main():
197+
return gleam_bitstring_segments_to_bytes((42, [(\"Int\", None)]))",
198+
)
199+
}
200+
186201
pub fn bitstring_test() {
187202
// TODO: Pretty sure this should be :bits, not :bit_string,
188203
// but glance has a bug:
@@ -200,3 +215,63 @@ def main():
200215
return gleam_bitstring_segments_to_bytes((gleam_bitstring_segments_to_bytes((3, [])), [(\"BitString\", None)]))",
201216
)
202217
}
218+
219+
pub fn u8_test() {
220+
"pub fn main() {
221+
<<\"hello\":utf8>>
222+
}
223+
"
224+
|> compiler.compile
225+
|> should.be_ok
226+
|> should.equal(
227+
"from gleam_builtins import *
228+
229+
def main():
230+
return gleam_bitstring_segments_to_bytes((\"hello\", [(\"Utf8\", None)]))",
231+
)
232+
}
233+
234+
pub fn u16_test() {
235+
"pub fn main() {
236+
<<\"hello\":utf16>>
237+
}
238+
"
239+
|> compiler.compile
240+
|> should.be_ok
241+
|> should.equal(
242+
"from gleam_builtins import *
243+
244+
def main():
245+
return gleam_bitstring_segments_to_bytes((\"hello\", [(\"Utf16\", None)]))",
246+
)
247+
}
248+
249+
pub fn u32_test() {
250+
"pub fn main() {
251+
<<\"hello\":utf32>>
252+
}
253+
"
254+
|> compiler.compile
255+
|> should.be_ok
256+
|> should.equal(
257+
"from gleam_builtins import *
258+
259+
def main():
260+
return gleam_bitstring_segments_to_bytes((\"hello\", [(\"Utf32\", None)]))",
261+
)
262+
}
263+
264+
pub fn u32_little_test() {
265+
"pub fn main() {
266+
<<\"hello\":utf32-little>>
267+
}
268+
"
269+
|> compiler.compile
270+
|> should.be_ok
271+
|> should.equal(
272+
"from gleam_builtins import *
273+
274+
def main():
275+
return gleam_bitstring_segments_to_bytes((\"hello\", [(\"Utf32\", None), (\"Little\", None)]))",
276+
)
277+
}

0 commit comments

Comments
 (0)