1 
2 module markov.serialize;
3 
4 import markov.chain;
5 import markov.counter;
6 import markov.state;
7 
8 import std.traits;
9 
10 /++
11  + Checks a type is decodable from a given input type.
12  ++/
13 template isDecodable(T, Input)
14 {
15     enum isDecodable =
16         isSomeString!T ||
17         isArray!T ||
18         isAssociativeArray!T ||
19         isBoolean!T ||
20         isNumeric!T ||
21         isSomeChar!T ||
22         hasDecodeProperty!(T, Input);
23 }
24 
25 /++
26  + Checks if a type is encoding into a given output type.
27  ++/
28 template isEncodable(T, Output)
29 {
30     enum isEncodable =
31         isSomeString!T ||
32         isArray!T ||
33         isAssociativeArray!T ||
34         isBoolean!T ||
35         isNumeric!T ||
36         isSomeChar!T ||
37         hasEncodeProperty!(T, Output);
38 }
39 
40 /++
41  + Checks if the type declares a compatible decode property.
42  ++/
43 template hasDecodeProperty(T, Input)
44 {
45     enum hasDecodeProperty = __traits(compiles, {
46         Input encoded = void;
47         T decoded = T.decode(encoded);
48     });
49 }
50 
51 /++
52  + Checks if the type declares a compatible encode property.
53  ++/
54 template hasEncodeProperty(T, Output)
55 {
56     enum hasEncodeProperty = __traits(compiles, {
57         T type = void;
58         Output output = type.encode;
59     });
60 }