1 /** 2 This module contains function for manipulating string representations 3 of default high-level rules. 4 */ 5 6 module reggae.rules.defaults; 7 8 9 import std.algorithm: find, canFind, splitter, startsWith; 10 import std.array: replace, array; 11 12 13 private immutable defaultRules = ["_dcompile", "_ccompile", "_cppcompile", "_dlink"]; 14 15 16 private bool isDefaultRule(in string command) @safe pure nothrow { 17 return defaultRules.canFind(command); 18 } 19 20 private string getRule(in string command) @safe pure { 21 return command.splitter.front; 22 } 23 24 bool isDefaultCommand(in string command) @safe pure { 25 return isDefaultRule(command.getRule); 26 } 27 28 string getDefaultRule(in string command) @safe pure { 29 immutable rule = command.getRule; 30 if(!isDefaultRule(rule)) { 31 throw new Exception("Cannot get defaultRule from " ~ command); 32 } 33 34 return rule; 35 } 36 37 38 string[] getDefaultRuleParams(in string command, in string key) @safe pure { 39 return getDefaultRuleParams(command, key, false); 40 } 41 42 43 string[] getDefaultRuleParams(in string command, in string key, string[] ifNotFound) @safe pure { 44 return getDefaultRuleParams(command, key, true, ifNotFound); 45 } 46 47 48 //@trusted because of replace 49 private string[] getDefaultRuleParams(in string command, in string key, 50 bool useIfNotFound, string[] ifNotFound = []) @trusted pure { 51 import std.conv: text; 52 53 auto parts = command.splitter; 54 immutable cmd = parts.front; 55 if(!isDefaultRule(cmd)) { 56 throw new Exception("Cannot get defaultRule from " ~ command); 57 } 58 59 auto fromParamPart = parts.find!(a => a.startsWith(key ~ "=")); 60 if(fromParamPart.empty) { 61 if(useIfNotFound) { 62 return ifNotFound; 63 } else { 64 throw new Exception ("Cannot get default rule from " ~ command); 65 } 66 } 67 68 auto paramPart = fromParamPart.front; 69 auto removeKey = paramPart.replace(key ~ "=", ""); 70 71 return removeKey.splitter(",").array; 72 }