1 module introspection.enum_; 2 3 import std.traits; 4 import std..string; 5 6 import introspection.type; 7 import introspection.location; 8 import introspection.protection; 9 10 version(unittest) { 11 import fluent.asserts; 12 } 13 14 /// 15 struct EnumMember { 16 /// 17 string name; 18 19 /// 20 string value; 21 } 22 23 /// Stores information about enums 24 struct Enum { 25 /// 26 string name; 27 28 /// 29 Type type; 30 31 /// 32 EnumMember[] members; 33 34 /// 35 Location location; 36 37 /// 38 Protection protection; 39 } 40 41 //// 42 Enum describeEnum(T)() if(is(T == enum)){ 43 Enum enum_; 44 45 enum_.name = T.stringof; 46 47 static foreach (i, member; EnumMembers!T) { 48 enum_.members ~= EnumMember(__traits(identifier, EnumMembers!T[i]), member.stringof); 49 } 50 51 enum_.type = describeType!T; 52 53 auto location = __traits(getLocation, T); 54 55 enum_.location = Location(location[0], location[1], location[2]); 56 enum_.protection = __traits(getProtection, T).toProtection; 57 58 return enum_; 59 } 60 61 /// it should describe an enum 62 unittest { 63 enum Type : string { 64 a = "A", b = "B", c = "C" 65 } 66 67 auto result = describeEnum!(Type); 68 69 result.name.should.equal("Type"); 70 71 result.type.name.should.equal("Type"); 72 result.type.isEnum.should.equal(true); 73 74 result.members.length.should.equal(3); 75 result.members[0].name.should.equal("a"); 76 result.members[0].value.should.equal(`"A"`); 77 78 result.location.file.should.equal("source/introspection/enum_.d"); 79 result.location.line.should.be.greaterThan(0); 80 result.location.column.should.equal(3); 81 82 result.protection.should.equal(Protection.public_); 83 }