Skip to content

Commit

Permalink
generator started
Browse files Browse the repository at this point in the history
  • Loading branch information
burner committed Dec 21, 2023
1 parent 9a3801d commit 218211b
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 3 deletions.
2 changes: 1 addition & 1 deletion fakerjs2generator/dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"description": "faker.js to D converter",
"license": "GPL3",
"name": "fakerjs2generator"
}
}
2 changes: 2 additions & 0 deletions fakerjs2generator/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import std.stdio;

import parser;
import defis;
import generator;

void main() {
writeln("Edit source/app.d to start your project.");
Language en = parseLanguage("en");
generate(en, "en");
}
2 changes: 0 additions & 2 deletions fakerjs2generator/source/defis.d
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module defis;

import std.typecons : Nullable;

struct Mustache {
string str;
}
Expand Down
100 changes: 100 additions & 0 deletions fakerjs2generator/source/generator.d
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;
}
}
}

0 comments on commit 218211b

Please sign in to comment.