Constructor
Parse a config string
Static parse function
The config struct
Config = The type of the struct to try to parse
Simple test case with one category
struct Config { struct Entry { ulong key; string value; } Entry entry; } enum CONFIG_STR = ` ; The first test config [ ENTRY ] value = the value key = 1234567891011 `; auto parser = ConfigParser!Config(CONFIG_STR); assert(parser.entry.key == 1234567891011); assert(parser.entry.value == "the value");
Test case for multiple categories
struct Config { struct Server { string address; ushort port; } Server server; struct Route { string url; string path; uint response_code; } Route route; } enum CONFIG_STR = ` ; ; Server configuration ; [ Server ] ADDRESS = 127.0.0.1 PORT = 32768 ; ; Index route ; [ Route ] URL = /index.html PATH = public/index.html RESPONSE_CODE = 200 `; auto parser = ConfigParser!Config(CONFIG_STR); assert(parser.server.address == "127.0.0.1"); assert(parser.server.port == 32768); assert(parser.route.url == "/index.html"); assert(parser.route.path == "public/index.html"); assert(parser.route.response_code == 200);
Test case for different value types
struct Config { struct MixedValues { uint integer; double decimal; bool flag; string text; } MixedValues mixed_values; } enum CONFIG_STR = ` [MixedValues] integer = 42 decimal = 66.6 flag = true text = This is some text `; auto parser = ConfigParser!Config(CONFIG_STR); assert(parser.mixed_values.integer == 42); assert(parser.mixed_values.decimal == 66.6); assert(parser.mixed_values.flag == true); assert(parser.mixed_values.text == "This is some text");
Error test cases
1 import std.exception; 2 3 /** 4 * Too few categories 5 */ 6 7 struct Config 8 { 9 struct CatOne 10 { 11 uint x; 12 } 13 14 CatOne one; 15 16 struct CatTwo 17 { 18 uint y; 19 uint z; 20 } 21 22 CatTwo two; 23 } 24 25 enum CONFIG_STR_TOO_FEW_CATS = ` 26 [CatOne] 27 x = 1 28 `; 29 30 assertThrown!ConfigException(ConfigParser!Config(CONFIG_STR_TOO_FEW_CATS)); 31 32 /** 33 * Expected category 34 */ 35 36 enum CONFIG_STR_CAT_EXPECTED = ` 37 x = 1 38 [CatOne] 39 x = 1 40 41 [CatTwo] 42 y = 2 43 z = 3 44 `; 45 46 assertThrown!ConfigException(ConfigParser!Config(CONFIG_STR_CAT_EXPECTED)); 47 48 /** 49 * Wrong category name 50 */ 51 52 enum CONFIG_STR_WRONG_CAT = ` 53 [CatOne] 54 x = 1 55 [CatTwoooo] 56 y = 2 57 z = 3 58 `; 59 60 assertThrown!ConfigException(ConfigParser!Config(CONFIG_STR_WRONG_CAT)); 61 62 /** 63 * Empty category 64 */ 65 66 enum CONFIG_STR_EMPTY_CAT = ` 67 [CatOne] 68 [CatTwo] 69 y = 2 70 z = 3 71 `; 72 73 assertThrown!ConfigException(ConfigParser!Config(CONFIG_STR_EMPTY_CAT)); 74 75 /** 76 * Too many fields 77 */ 78 79 enum CONFIG_STR_TOO_MANY_FIELDS = ` 80 [CatOne] 81 x = 1 82 y = 2 83 [CatTwo] 84 y = 2 85 z = 3 86 `; 87 88 assertThrown!ConfigException(ConfigParser!Config(CONFIG_STR_TOO_MANY_FIELDS)); 89 90 /** 91 * Too few fields 92 */ 93 94 enum CONFIG_STR_TOO_FEW_FIELDS = ` 95 [CatOne] 96 x = 1 97 [CatTwo] 98 y = 2 99 `; 100 101 assertThrown!ConfigException(ConfigParser!Config(CONFIG_STR_TOO_FEW_FIELDS)); 102 103 /** 104 * Missing = 105 */ 106 107 enum CONFIG_STR_MISSING_EQUALS = ` 108 [CatOne] 109 x = 1 110 [CatTwo] 111 y 2 112 z = 3 113 `; 114 115 assertThrown!ConfigException(ConfigParser!Config(CONFIG_STR_MISSING_EQUALS)); 116 117 /** 118 * Missing assignment 119 */ 120 121 enum CONFIG_STR_MISSING_ASSIGN = ` 122 [CatOne] 123 x 124 [CatTwo] 125 y = 2 126 z = 3 127 `; 128 129 assertThrown!ConfigException(ConfigParser!Config(CONFIG_STR_MISSING_ASSIGN)); 130 131 /** 132 * Wrong field 133 */ 134 135 enum CONFIG_STR_WRONG_FIELD = ` 136 [CatOne] 137 x = 1 138 [CatTwo] 139 b = 2 140 c = 3 141 `; 142 assertThrown!ConfigException(ConfigParser!Config(CONFIG_STR_WRONG_FIELD)); 143 144 /** 145 * Wrong type 146 */ 147 148 enum CONFIG_STR_WRONG_TYPE = ` 149 [CatOne] 150 x = 1 151 [CatTwo] 152 y = hello 153 z = 3 154 `; 155 156 assertThrown!ConfigException(ConfigParser!Config(CONFIG_STR_WRONG_TYPE));
Config parser struct