1 /++
2 Ion Type enumeration and encoding.
3 
4 Macros:
5 AlgorithmREF = $(GREF_ALTTEXT mir-algorithm, $(TT $2), $2, mir, $1)$(NBSP)
6 SUBREF = $(REF_ALTTEXT $(TT $2), $2, mir, ion, $1)$(NBSP)
7 +/
8 module mir.ion.type_code;
9 
10 /++
11 Codes for $(HTTP amzn.github.io/ion-docs/docs/binary.html#typed-value-formats, Typed Value Formats)
12 +/
13 enum IonTypeCode
14 {
15     /++
16     Spec: $(HTTP http://amzn.github.io/ion-docs/docs/binary.html#0-null, 0: null)
17     D_type: $(SUBREF value, IonNull).
18     +/
19     null_,
20 
21     /++
22     Spec: $(HTTP http://amzn.github.io/ion-docs/docs/binary.html#1-bool, 1: bool)
23     D_type: `bool`
24     +/
25     bool_,
26 
27     /++
28     Spec: $(HTTP http://amzn.github.io/ion-docs/docs/binary.html#2-and-3-int, 2 and 3: int)
29     D_type: $(SUBREF value, IonUInt) and $(SUBREF value, IonNInt)
30     +/
31     uInt,
32     /// ditto
33     nInt,
34 
35     /++
36     Spec: $(HTTP http://amzn.github.io/ion-docs/docs/binary.html#4-float, 4: float)
37     D_type: $(SUBREF value, IonFloat)
38     +/
39     float_,
40 
41     /++
42     Spec: $(HTTP http://amzn.github.io/ion-docs/docs/binary.html#5-decimal, 5: decimal)
43     D_type: $(SUBREF value, IonDecimal)
44     +/
45     decimal,
46 
47     /++
48     Spec: $(HTTP http://amzn.github.io/ion-docs/docs/binary.html#6-timestamp, 6: timestamp)
49     D_type: $(SUBREF value, IonTimestamp)
50     +/
51     timestamp,
52 
53     /++
54     Spec: $(HTTP http://amzn.github.io/ion-docs/docs/binary.html#7-symbol, 7: symbol)
55     D_type: $(SUBREF value, IonSymbolID)
56     +/
57     symbol,
58 
59     /++
60     Spec: $(HTTP http://amzn.github.io/ion-docs/docs/binary.html#8-string, 8: string)
61     D_type: `const(char)[]`
62     +/
63     string,
64 
65     /++
66     Spec: $(HTTP http://amzn.github.io/ion-docs/docs/binary.html#9-clob, 9: clob)
67     D_type: $(AlgorithmREF lob, Clob)
68     +/
69     clob,
70 
71     /++
72     Spec: $(HTTP 1http://amzn.github.io/ion-docs/docs/binary.html#0-blob, 10: blob)
73     D_type: $(AlgorithmREF lob, Blob)
74     +/
75     blob,
76 
77     /++
78     Spec: $(HTTP 1http://amzn.github.io/ion-docs/docs/binary.html#1-list, 11: list)
79     D_type: $(SUBREF value, IonList)
80     +/
81     list,
82 
83     /++
84     Spec: $(HTTP 1http://amzn.github.io/ion-docs/docs/binary.html#2-sexp, 12: sexp)
85     D_type: $(SUBREF value, IonSexp)
86     +/
87     sexp,
88 
89     /++
90     Spec: $(HTTP 1http://amzn.github.io/ion-docs/docs/binary.html#3-struct, 13: struct)
91     D_type: $(SUBREF value, IonStruct)
92     +/
93     struct_,
94 
95     /++
96     Spec: $(HTTP 1http://amzn.github.io/ion-docs/docs/binary.html#4-annotations, 14: Annotations)
97     D_type: $(SUBREF value, IonAnnotationWrapper)
98     +/
99     annotations,
100 }
101 
102 /++
103 Returns: text Ion representation of null type code.
104 +/
105 string nullStringOf()(IonTypeCode code) @safe pure nothrow @nogc
106 {
107     final switch(code)
108     {
109         case IonTypeCode.null_:
110             return "null";
111         case IonTypeCode.bool_:
112             return "null.bool";
113         case IonTypeCode.uInt:
114         case IonTypeCode.nInt:
115             return "null.int";
116         case IonTypeCode.float_:
117             return "null.float";
118         case IonTypeCode.decimal:
119             return "null.decimal";
120         case IonTypeCode.timestamp:
121             return "null.timestamp";
122         case IonTypeCode.symbol:
123             return "null.symbol";
124         case IonTypeCode..string:
125             return "null.string";
126         case IonTypeCode.clob:
127             return "null.clob";
128         case IonTypeCode.blob:
129             return "null.blob";
130         case IonTypeCode.list:
131             return "null.list";
132         case IonTypeCode.sexp:
133             return "null.sexp";
134         case IonTypeCode.struct_:
135             return "null.struct";
136         case IonTypeCode.annotations:
137             return "null.annotations"; // invalid
138     }
139 }
140 
141 /++
142 Returns: JSON representation of null value that can be deserialized to an algebraic types.
143 Empty strings, structs, and lists are used instead of `null` value for the corresponding codes.
144 +/
145 string nullStringJsonAlternative()(IonTypeCode code) @safe pure nothrow @nogc
146 {
147     switch(code)
148     {
149         case IonTypeCode.symbol:
150         case IonTypeCode..string:
151         case IonTypeCode.clob:
152             return `""`;
153         case IonTypeCode.list:
154         case IonTypeCode.sexp:
155             return `[]`;
156         case IonTypeCode.struct_:
157             return `{}`;
158         default:
159             return `null`;
160     }
161 }