added template constraint to loadSubstitutionFile

This commit is contained in:
Johannes Loher 2017-04-10 22:49:20 +02:00
parent 86d8860767
commit 446f9a096b

View file

@ -1,12 +1,16 @@
module substitution; module substitution;
import std.file: slurp; import std.file : slurp;
import std.meta : AliasSeq;
import std.traits : Parameters;
public: public:
void loadSubstitutionFile(alias slurpFun = slurp)(string fileName) void loadSubstitutionFile(alias slurpFun = slurp)(string fileName)
if (is(Parameters!(slurpFun!(string, string)) == AliasSeq!(string, const char[])))
{ {
import std.algorithm.iteration : each; import std.algorithm.iteration : each;
auto data = slurpFun!(string, string)(fileName, `"%s" = "%s"`); auto data = slurpFun!(string, string)(fileName, `"%s" = "%s"`);
map = (string[string]).init; map = (string[string]).init;
data.each!(pair => map[pair[0]] = pair[1]); data.each!(pair => map[pair[0]] = pair[1]);
@ -14,8 +18,7 @@ void loadSubstitutionFile(alias slurpFun = slurp)(string fileName)
@safe unittest @safe unittest
{ {
import std.algorithm: canFind; import std.typecons : Tuple, tuple;
import std.typecons: Tuple, tuple;
static Tuple!(string, string)[] mockSlurpEmpty(Type1, Type2)(string filename, in char[] format) static Tuple!(string, string)[] mockSlurpEmpty(Type1, Type2)(string filename, in char[] format)
{ {
@ -25,7 +28,8 @@ void loadSubstitutionFile(alias slurpFun = slurp)(string fileName)
loadSubstitutionFile!mockSlurpEmpty(""); loadSubstitutionFile!mockSlurpEmpty("");
assert(map.length == 0); assert(map.length == 0);
static Tuple!(string, string)[] mockSlurpEmptyEntry(Type1, Type2)(string filename, in char[] format) static Tuple!(string, string)[] mockSlurpEmptyEntry(Type1, Type2)(string filename,
in char[] format)
{ {
return [tuple("", "")]; return [tuple("", "")];
} }
@ -35,7 +39,8 @@ void loadSubstitutionFile(alias slurpFun = slurp)(string fileName)
assert(map.length == 1); assert(map.length == 1);
assert(map[""] == ""); assert(map[""] == "");
static Tuple!(string, string)[] mockSlurpSingleEntry(Type1, Type2)(string filename, in char[] format) static Tuple!(string, string)[] mockSlurpSingleEntry(Type1, Type2)(string filename,
in char[] format)
{ {
return [tuple("foo", "bar")]; return [tuple("foo", "bar")];
} }
@ -45,7 +50,8 @@ void loadSubstitutionFile(alias slurpFun = slurp)(string fileName)
assert(map.length == 1); assert(map.length == 1);
assert(map["foo"] == "bar"); assert(map["foo"] == "bar");
static Tuple!(string, string)[] mockSlurpMultipleEntries(Type1, Type2)(string filename, in char[] format) static Tuple!(string, string)[] mockSlurpMultipleEntries(Type1, Type2)(
string filename, in char[] format)
{ {
return [tuple("", ""), tuple("0", "1"), tuple("Text in", "wird durch diesen ersetzt")]; return [tuple("", ""), tuple("0", "1"), tuple("Text in", "wird durch diesen ersetzt")];
} }