Skip to content

Commit 63eee88

Browse files
committed
Add fractional part to times using custom types
1 parent 4429456 commit 63eee88

File tree

5 files changed

+208
-52
lines changed

5 files changed

+208
-52
lines changed

README.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
D implementation of [Tom's Obvious, Minimal Language](https://github.com/toml-lang/toml/blob/master/README.md) [v0.4.0](https://github.com/toml-lang/toml/blob/master/versions/en/toml-v0.4.0.md)
1+
D implementation of [Tom's Obvious, Minimal Language](https://github.com/toml-lang/toml/blob/master/README.md) version [0.4.0](https://github.com/toml-lang/toml/blob/master/versions/en/toml-v0.4.0.md)
22

33

44
[![Build Status](https://travis-ci.org/Kripth/toml.svg?branch=master)](https://travis-ci.org/Kripth/toml)
@@ -8,6 +8,9 @@ D implementation of [Tom's Obvious, Minimal Language](https://github.com/toml-la
88

99
# Usage
1010

11+
**dub.json**: `"toml": "~>0.4.0-rc.2"`
12+
**dub.sdl**: `dependency "toml" version="~>0.4.0-rc.2"`
13+
1114
```d
1215
import toml;
1316
@@ -32,7 +35,28 @@ import std.file : read;
3235
doc = parseTOML(cast(string)read("/path/to/file.toml"));
3336
```
3437

38+
# Conversion
39+
40+
### JSON
41+
42+
**dub.json**: `"toml:json": "~>0.4.0-rc.2"`
43+
**dub.sdl**: `dependency "toml:json" version="~>0.4.0-rc.2"`
44+
45+
```d
46+
import std.json;
47+
48+
import toml;
49+
import toml.json;
50+
51+
auto json = JSONValue([1, 2, 3]);
52+
assert(toTOML(json).type == TOML_TYPE.ARRAY);
53+
assert(toTOML(json) == [1, 2, 3]);
54+
55+
auto toml = parseTOML(`key = "value"`);
56+
assert(toJSON(toml).type == JSON_TYPE.OBJECT);
57+
assert(toJSON(toml) == JSONValue(["key": "value"]));
58+
```
59+
3560
# Missing features
3661

37-
- Array of tables
38-
- Fractional time in dates
62+
- Nested array of tables

conv/json/toml/json.d

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@
77
* License: $(HTTP https://github.com/Kripth/toml/blob/master/LICENSE, MIT)
88
* Authors: Kripth
99
* References: $(LINK https://github.com/toml-lang/toml/blob/master/README.md)
10-
* Source: $(HTTP https://github.com/Kripth/toml, Kripth/_toml.d)
10+
* Source: $(HTTP https://github.com/Kripth/toml/blob/master/src/conv/json/toml/json.d, toml/conv/_json.d)
1111
*
1212
*/
1313
module toml.json;
1414

1515
import std.json : JSONValue, JSON_TYPE;
1616

17-
import toml.toml : TOMLDocument, TOMLValue, TOML_TYPE;
17+
import toml.toml : TOMLDocument, TOMLValue, TOML_TYPE, TOMLException;
1818

1919
/**
2020
* Converts a TOMLValue to a JSONValue.
21+
* Note: datetimes are converted to strings.
2122
*/
2223
JSONValue toJSON(TOMLValue toml) {
2324
final switch(toml.type) with(TOML_TYPE) {
@@ -52,7 +53,8 @@ JSONValue toJSON(TOMLDocument doc) {
5253
///
5354
unittest {
5455

55-
import std.datetime;
56+
import std.datetime : SysTime, Date;
57+
import toml.datetime : DateTime, TimeOfDay;
5658

5759
assert(toJSON(TOMLValue(true)).type == JSON_TYPE.TRUE);
5860
assert(toJSON(TOMLValue("string")).str == "string");
@@ -68,10 +70,14 @@ unittest {
6870

6971
/**
7072
* Convert a JSONValue to a TOMLValue.
73+
* Throws:
74+
* TOMLException if the array values have different types
75+
* TOMLExcpetion if a floating point value is not finite
76+
* TOMLException if the json value is null
7177
*/
7278
TOMLValue toTOML(JSONValue json) {
7379
final switch(json.type) with(JSON_TYPE) {
74-
case NULL: return TOMLValue("null");
80+
case NULL: throw new TOMLException("JSONValue is null");
7581
case TRUE: return TOMLValue(true);
7682
case FALSE: return TOMLValue(false);
7783
case STRING: return TOMLValue(json.str);
@@ -96,7 +102,11 @@ TOMLValue toTOML(JSONValue json) {
96102
///
97103
unittest {
98104

99-
assert(toTOML(JSONValue.init) == "null");
105+
try {
106+
// null
107+
toTOML(JSONValue.init); assert(0);
108+
} catch(TOMLException) {}
109+
100110
assert(toTOML(JSONValue(true)).type == TOML_TYPE.BOOL);
101111
assert(toTOML(JSONValue(false)) == false);
102112
assert(toTOML(JSONValue("test")) == "test");

src/toml/datetime.d

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Written in the D programming language.
2+
3+
/**
4+
*
5+
* Custom types for TOML's datetimes that add fractional time to D ones.
6+
*
7+
* License: $(HTTP https://github.com/Kripth/toml/blob/master/LICENSE, MIT)
8+
* Authors: Kripth
9+
* References: $(LINK https://github.com/toml-lang/toml/blob/master/README.md)
10+
* Source: $(HTTP https://github.com/Kripth/toml/blob/master/src/toml/datetime.d, toml/_datetime.d)
11+
*
12+
*/
13+
module toml.datetime;
14+
15+
import std.conv : to;
16+
import std.datetime : Duration, dur, DateTimeD = DateTime, Date, TimeOfDayD = TimeOfDay;
17+
18+
struct DateTime {
19+
20+
public Date date;
21+
public TimeOfDay timeOfDay;
22+
23+
public inout @property DateTimeD dateTime() {
24+
return DateTimeD(this.date, this.timeOfDay.timeOfDay);
25+
}
26+
27+
alias dateTime this;
28+
29+
public static pure DateTime fromISOExtString(string str) {
30+
Duration frac;
31+
if(str.length > 19 && str[19] == '.') {
32+
frac = dur!"msecs"(to!ulong(str[20..$]));
33+
str = str[0..19];
34+
}
35+
auto dt = DateTimeD.fromISOExtString(str);
36+
return DateTime(dt.date, TimeOfDay(dt.timeOfDay, frac));
37+
}
38+
39+
public inout string toISOExtString() {
40+
return this.date.toISOExtString() ~ "T" ~ this.timeOfDay.toString();
41+
}
42+
43+
}
44+
45+
struct TimeOfDay {
46+
47+
public TimeOfDayD timeOfDay;
48+
public Duration fracSecs;
49+
50+
alias timeOfDay this;
51+
52+
public static pure TimeOfDay fromISOExtString(string str) {
53+
Duration frac;
54+
if(str.length > 8 && str[8] == '.') {
55+
frac = dur!"msecs"(to!ulong(str[9..$]));
56+
str = str[0..8];
57+
}
58+
return TimeOfDay(TimeOfDayD.fromISOExtString(str), frac);
59+
}
60+
61+
public inout string toISOExtString() {
62+
immutable msecs = this.fracSecs.total!"msecs";
63+
if(msecs != 0) {
64+
return this.timeOfDay.toISOExtString() ~ "." ~ to!string(msecs);
65+
} else {
66+
return this.timeOfDay.toISOExtString();
67+
}
68+
}
69+
70+
}

src/toml/package.d

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
* License: $(HTTP https://github.com/Kripth/toml/blob/master/LICENSE, MIT)
88
* Authors: Kripth
99
* References: $(LINK https://github.com/toml-lang/toml/blob/master/README.md)
10-
* Source: $(HTTP https://github.com/Kripth/toml, Kripth/_toml.d)
10+
* Source: $(HTTP https://github.com/Kripth/toml/blob/master/src/toml/package.d, toml/_package.d)
1111
*
1212
*/
1313
module toml;
1414

15+
public import std.datetime : SysTime, Date;
16+
17+
public import toml.datetime : DateTime, TimeOfDay;
1518
public import toml.toml : TOML_TYPE, TOMLDocument, TOMLValue, parseTOML, TOMLException, TOMLParserException;

0 commit comments

Comments
 (0)