Skip to content

Commit

Permalink
add @safe scope pure attributes everywhere, dip1000 compatibility (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
WebFreak001 authored Nov 30, 2022
1 parent 1377a91 commit 421e592
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 77 deletions.
3 changes: 2 additions & 1 deletion dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ authors "Kripth" "Orfeo Da Vià" "WebFreak001"
license "MIT"
subPackage "json"


configuration "library" {
sourcePaths "src"
}
Expand All @@ -14,6 +13,8 @@ configuration "unittest" {
targetType "executable"
mainSourceFile "tests/ut_main.d"

dflags "-dip1000"

sourcePaths "tests"
importPaths "tests"

Expand Down
2 changes: 2 additions & 0 deletions json/src/toml/json.d
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ JSONValue toJSON(TOMLDocument doc) {
}

///
@safe pure
unittest {

import std.datetime : SysTime, Date;
Expand Down Expand Up @@ -109,6 +110,7 @@ private TOMLValue[string] toTOMLObject(JSONValue json) {
}

///
@safe pure
unittest {

try {
Expand Down
14 changes: 9 additions & 5 deletions src/toml/datetime.d
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ struct DateTime {
public Date date;
public TimeOfDay timeOfDay;

public inout @property DateTimeD dateTime() {
@safe scope:

public pure inout @property DateTimeD dateTime() {
return DateTimeD(this.date, this.timeOfDay.timeOfDay);
}

alias dateTime this;

public static pure DateTime fromISOExtString(string str) {
public static pure DateTime fromISOExtString(scope const(char)[] str) {
Duration frac;
if (str.length > 19 && str[19] == '.') {
frac = dur!"msecs"(to!ulong(str[20 .. $]));
Expand All @@ -36,7 +38,7 @@ struct DateTime {
return DateTime(dt.date, TimeOfDay(dt.timeOfDay, frac));
}

public inout string toISOExtString() {
public pure inout string toISOExtString() {
return this.date.toISOExtString() ~ "T" ~ this.timeOfDay.toString();
}

Expand All @@ -47,9 +49,11 @@ struct TimeOfDay {
public TimeOfDayD timeOfDay;
public Duration fracSecs;

@safe scope:

alias timeOfDay this;

public static pure TimeOfDay fromISOExtString(string str) {
public static pure TimeOfDay fromISOExtString(scope const(char)[] str) {
Duration frac;
if (str.length > 8 && str[8] == '.') {
frac = dur!"msecs"(to!ulong(str[9 .. $]));
Expand All @@ -58,7 +62,7 @@ struct TimeOfDay {
return TimeOfDay(TimeOfDayD.fromISOExtString(str), frac);
}

public inout string toISOExtString() {
public pure inout string toISOExtString() {
immutable msecs = this.fracSecs.total!"msecs";
if (msecs != 0) {
return this.timeOfDay.toISOExtString() ~ "." ~ to!string(msecs);
Expand Down
37 changes: 20 additions & 17 deletions src/toml/serialize.d
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ struct tomlName {
enum tomlIgnored;
// ---

string serializeTOML(T)(T value) {
string serializeTOML(T)(T value) @safe {
auto ret = appender!string;
serializeTOML(value, ret);
return ret.data;
}

void serializeTOML(T, Output)(T value, ref Output output) {
void serializeTOML(T, Output)(T value, ref Output output) @safe {
serializeTOML(value, output, "", "");
}

Expand All @@ -40,7 +40,8 @@ template fieldName(alias field) {
}

void serializeTOML(T, Output)(T value, ref Output output, string indent, string group)
if (isPlainStruct!T && !is(T == MapT[string], MapT) && !is(T == SumType!Types, Types...)) {
@safe
if (isPlainStruct!T && !is(T == MapT[string], MapT) && !is(T == SumType!Types, Types...)) {
foreach (i, ref v; value.tupleof) {
{
static if (isValueSerializable!(typeof(v)) && getUDAs!(value.tupleof[i], tomlIgnored).length == 0 && !isStructArray!(typeof(v))) {
Expand Down Expand Up @@ -184,14 +185,15 @@ enum isPlainStruct(T) = is(T == struct) || is(T == V[string], V);

enum isValueSerializable(T) = !is(T == struct);

void serializeTOMLValue(T, Output)(T value, ref Output output) {
void serializeTOMLValue(T, Output)(T value, ref Output output) @safe {
static if (__traits(compiles, { auto v = TOMLValue(value); })) {
auto v = TOMLValue(value);
v.append(output);
} else
static assert(false, "TODO: serialize value type " ~ T.stringof ~ " not implemented");
}

@safe
unittest {
struct Database {
string host;
Expand Down Expand Up @@ -235,22 +237,23 @@ ports = [1337, 4242, 5555]
database = "mybot"
port = 8080
`, str);
}
}

unittest {
struct Property {
SumType!(int, string) id;
SumType!(int, string)[] attributes;
}
@safe
unittest {
struct Property {
SumType!(int, string) id;
SumType!(int, string)[] attributes;
}

Property[string] props = [
"href": Property(SumType!(int, string)(1), [SumType!(int, string)(1), SumType!(int, string)("foo")],),
"base": Property(SumType!(int, string)("bar"), [SumType!(int, string)(44)],)
];
Property[string] props = [
"href": Property(SumType!(int, string)(1), [SumType!(int, string)(1), SumType!(int, string)("foo")],),
"base": Property(SumType!(int, string)("bar"), [SumType!(int, string)(44)],)
];

auto str = serializeTOML(props);
auto str = serializeTOML(props);

assert(str == `
assert(str == `
[base]
[[base.attributes]]
Expand All @@ -275,4 +278,4 @@ ports = [1337, 4242, 5555]
kind = "int"
value = 1
`, str);
}
}
Loading

0 comments on commit 421e592

Please sign in to comment.