1 module toml_foolery.encode.types.array;
2 
3 import std.traits : isStaticArray, isDynamicArray;
4 import toml_foolery.encode;
5 import toml_foolery.encode.types..string : makesTomlString;
6 
7 
8 package(toml_foolery.encode) enum bool makesTomlArray(T) = (
9     !makesTomlString!T &&
10     (isStaticArray!T || isDynamicArray!T)
11 );
12 
13 /// Serializes static arrays into TOML Array values.
14 package(toml_foolery.encode) void tomlifyValueImpl(T)(
15     const T value,
16     ref Appender!string buffer,
17     immutable string[] parentTables
18 )
19 if (makesTomlArray!T)
20 {
21     buffer.put("[ ");
22     foreach (element; value)
23     {
24         tomlifyValue(element, buffer, []);
25         buffer.put(", ");
26     }
27     buffer.put("]");
28 }
29 
30 @("Encode static arrays of integers")
31 unittest
32 {
33     int[3] arr = [ 1, 2, 3 ];
34     expect(_tomlifyValue(arr)).toEqual("[ 1, 2, 3, ]");
35 }
36 
37 @("Encode static arrays of floats")
38 unittest
39 {
40     real[3] arr = [ 512.5f, 2.0, real.nan ];
41     expect(_tomlifyValue(arr)).toEqual("[ 512.5, 2.0, nan, ]");
42 }
43 
44 @("Encode static arrays of booleans")
45 unittest
46 {
47     bool[2] arr = [ true, false ];
48     expect(_tomlifyValue(arr)).toEqual("[ true, false, ]");
49 }
50 
51 @("Encode static arrays of strings")
52 unittest
53 {
54     string[3] arr = [ "🧞", "hello", "world" ];
55     expect(_tomlifyValue(arr)).toEqual(`[ "🧞", "hello", "world", ]`);
56 }
57 
58 @("Encode 2D static arrays")
59 unittest
60 {
61     int[3][2] arr = [[ 5, 5, 5 ], [ 4, 2, 2 ]];
62     expect(_tomlifyValue(arr)).toEqual(`[ [ 5, 5, 5, ], [ 4, 2, 2, ], ]`);
63 }