-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
103 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,4 @@ | |
"description": "faker.js to D converter", | ||
"license": "GPL3", | ||
"name": "fakerjs2generator" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
module defis; | ||
|
||
import std.typecons : Nullable; | ||
|
||
struct Mustache { | ||
string str; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
module generator; | ||
|
||
import std.array : appender; | ||
import std.algorithm.iteration : filter; | ||
import std.algorithm.searching : endsWith; | ||
import std.exception : enforce; | ||
import std.conv : to; | ||
import std.uni : toUpper; | ||
import std.format : formattedWrite; | ||
import std.range : empty; | ||
import std.traits : FieldNameTuple; | ||
import std.stdio; | ||
|
||
import defis; | ||
|
||
void generate(Language lang, string langName) { | ||
auto app = appender!string(); | ||
traverse(lang, app, []); | ||
writeln(app.data); | ||
} | ||
|
||
void traverse(T,Out)(T t, ref Out o, string[] path) { | ||
static if(T.stringof.endsWith("Folder") || is(T == Language)) { | ||
static foreach(string mem; [FieldNameTuple!(T)].filter!(m => !m.empty)) {{ | ||
traverse(__traits(getMember, t, mem), o, path ~ mem); | ||
}} | ||
} else { | ||
static if(is(T == string[])) { | ||
genStringArray(t, o, path); | ||
} | ||
static if(is(T == Number[])) { | ||
genNumberArray(t, o, path); | ||
} | ||
} | ||
} | ||
|
||
void genNumberArray(Out)(Number[] strs, ref Out o, string[] path) { | ||
} | ||
|
||
void genStringArray(Out)(string[] strs, ref Out o, string[] path) { | ||
iformat(o, 1, "string %s() {\n", pathToFuncName(path)); | ||
iformat(o, 2, "const string[] strs =\n\t\t[ "); | ||
str80(o, strs, 2); | ||
o.put(" ];\n\n"); | ||
iformat(o, 2, "return choice(strs, this.rnd);\n\t}\n"); | ||
} | ||
|
||
string pathToFuncName(string[] path) { | ||
enforce(path.length > 1, to!string(path)); | ||
string ret = path[0]; | ||
foreach(it; path[1 .. $]) { | ||
char up = to!char(it[0].toUpper()); | ||
string ta = up ~ it[1 .. $]; | ||
ret ~= ta; | ||
} | ||
return ret; | ||
} | ||
|
||
void iformat(Out,Args...)(ref Out o, size_t indent, string f, Args args) { | ||
foreach(_; 0 .. indent) { | ||
o.put('\t'); | ||
} | ||
formattedWrite(o, f, args); | ||
} | ||
|
||
void str80(Out)(ref Out o, string[] strs, size_t tabs) { | ||
size_t curStrCount; | ||
size_t curLength = tabs * 4; | ||
size_t curLineCount = 0; | ||
foreach(idx, s; strs) { | ||
if(curStrCount == 0) { | ||
if(idx > 0) { | ||
o.put(", "); | ||
curLength += 2; | ||
} | ||
o.put('"'); | ||
o.put(s); | ||
o.put('"'); | ||
curLength += s.length + 2; | ||
++curStrCount; | ||
} else { | ||
if(curLength > 80) { | ||
curLength = tabs * 4; | ||
o.put('\n'); | ||
foreach(_; 0 .. tabs) { | ||
o.put('\t'); | ||
} | ||
curStrCount = 0; | ||
} | ||
if(idx > 0) { | ||
o.put(", "); | ||
curLength += 2; | ||
} | ||
o.put('"'); | ||
o.put(s); | ||
o.put('"'); | ||
curLength += s.length + 2; | ||
} | ||
} | ||
} |