1 module dud.pkgdescription.udas;
2 
3 import std.format : format;
4 
5 import dud.pkgdescription;
6 
7 @safe pure:
8 
9 struct SDL(alias In, alias Out) {
10 @safe pure:
11 	string name;
12 
13 	this(string name) @nogc nothrow @safe pure {
14 		this.name = name;
15 	}
16 }
17 
18 struct JSON(alias In, alias Out) {
19 @safe pure:
20 	string name;
21 
22 	this(string name) @nogc nothrow @safe pure {
23 		this.name = name;
24 	}
25 }
26 
27 mixin(buildGetName("JSON"));
28 mixin(buildGetName("SDL"));
29 
30 @safe pure unittest {
31 	static assert(SDLName!"ddoxFilterArgs" == "x:ddoxFilterArgs",
32 			SDLName!"ddoxFilterArgs");
33 }
34 
35 private string buildGetName(string type) {
36 	return q{
37 template %1$sName(string key) {
38 	enum attr = __traits(getAttributes,
39 			__traits(getMember, PackageDescription, key));
40 
41 	static if(attr.length == 1) {
42 		alias First = attr[0];
43 		alias FirstType = typeof(First);
44 		static if(is(FirstType : %1$s!Args, Args...)
45 				&& attr[0].name.length != 0)
46 		{
47 			enum %1$sName = attr[0].name;
48 		} else {
49 			enum %1$sName = key;
50 		}
51 	} else static if(attr.length == 2) {
52 		alias First = attr[0];
53 		alias FirstType = typeof(First);
54 		alias Second = attr[1];
55 		alias SecondType = typeof(Second);
56 		static if(is(FirstType : %1$s!Args1, Args1...)
57 				&& attr[0].name.length != 0)
58 		{
59 			enum %1$sName = attr[0].name;
60 		} else static if(is(SecondType == %1$s!Args2, Args2...)
61 				&& attr[1].name.length != 0)
62 		{
63 			enum %1$sName = attr[1].name;
64 		} else {
65 			enum %1$sName = key;
66 		}
67 	} else {
68 		enum %1$sName = key;
69 	}
70 }
71 }.format(type);
72 }
73 
74 mixin(buildGetPut("JSON", "Get", 0));
75 mixin(buildGetPut("JSON", "Put", 1));
76 mixin(buildGetPut("SDL", "Get", 0));
77 mixin(buildGetPut("SDL", "Put", 1));
78 
79 private string buildGetPut(string type, string op, size_t idx) {
80 	return q{
81 template %1$s%2$s(string key) {
82 	enum attr = __traits(getAttributes,
83 			__traits(getMember, PackageDescription, key));
84 
85 	static if(attr.length == 1) {
86 		alias First = attr[0];
87 		alias FirstType = typeof(First);
88 		static if(is(FirstType : %1$s!Args, Args...)) {
89 			alias %1$s%2$s = Args[%3$u];
90 		} else {
91 			static assert(false, "No %1$s op %2$s alias found for \"" ~ key ~ "\"");
92 		}
93 	} else static if(attr.length == 2) {
94 		alias First = attr[0];
95 		alias FirstType = typeof(First);
96 		alias Second = attr[1];
97 		alias SecondType = typeof(Second);
98 		static if(is(FirstType : %1$s!Args, Args...)) {
99 			alias %1$s%2$s = Args[%3$u];
100 		} else static if(is(SecondType : %1$s!Args, Args...)) {
101 			alias %1$s%2$s = Args[%3$u];
102 		} else {
103 			static assert(false, "No %1$s op %2$s alias found for \"" ~ key ~ "\"");
104 		}
105 	} else {
106 		static assert(false, "No %1$s op %2$s alias found for \"" ~ key ~ "\"");
107 	}
108 }}.format(type, op, idx);
109 }