module fahrplanparser;
import std.algorithm : map;
import std.array : empty, front;
import std.conv : to;
import std.datetime : dur, TimeOfDay, DateTimeException;
import std.string : format;
import kxml.xml : readDocument, XmlNode;
import substitution;
private:
enum departureNodeName = "dp";
enum timeNodeName = "t";
enum realTimeNodeName = "rt";
enum departuresXPath = "/efa/dps/" ~ departureNodeName;
template timeXPath(string _timeNodeName = timeNodeName)
{
enum timeXPath = "/st/" ~ _timeNodeName;
}
enum useRealTimeXPath = "/realtime";
enum lineXPath = "/m/nu";
enum directionXPath = "/m/des";
public:
auto parsedFahrplan(in string data)
{
// dfmt off
return data.readDocument
.parseXPath(departuresXPath)
.map!(dp => ["departure" : "%02s:%02s".format(dp.departureTime.hour, dp.departureTime.minute),
"delay" : dp.delay.total!"minutes".to!string,
"line": dp.parseXPath(lineXPath).front.getCData,
"direction": dp.parseXPath(directionXPath).front.getCData.substitute]);
// dfmt on
}
@system unittest
{
import std.array : array;
auto xml = "";
assert(xml.parsedFahrplan.array == []);
xml = "";
assert(xml.parsedFahrplan.array == []);
xml = "112246Wernerwerkstraße";
assert(xml.parsedFahrplan.array == [["direction" : "Wernerwerkstraße",
"line" : "6", "departure" : "12:24", "delay" : "18"]]);
xml = "012246Wernerwerkstraße";
assert(xml.parsedFahrplan.array == [["direction" : "Wernerwerkstraße",
"line" : "6", "departure" : "12:24", "delay" : "0"]]);
xml = "012246Wernerwerkstraße1135311Burgweinting";
assert(xml.parsedFahrplan.array == [["direction" : "Wernerwerkstraße", "line" : "6",
"departure" : "12:24", "delay" : "0"], ["direction" : "Burgweinting",
"line" : "11", "departure" : "13:53", "delay" : "3"]]);
}
private:
class UnexpectedValueException(T) : Exception
{
this(T t, string node) @safe pure
{
super(`Unexpected value "%s" for node "%s"`.format(t, node));
}
}
class CouldNotFindeNodeException : Exception
{
this(string node) @safe pure
{
super(`Could not find node "%s"`.format(node));
}
}
auto departureTime(string _timeNodeName = timeNodeName)(XmlNode dp)
in
{
assert(dp.getName == departureNodeName);
}
body
{
auto timeNodes = dp.parseXPath(timeXPath!_timeNodeName);
if(timeNodes.empty)
throw new CouldNotFindeNodeException(_timeNodeName);
return TimeOfDay.fromISOString(timeNodes.front.getCData ~ "00");
}
@system unittest
{
import std.exception : assertThrown;
auto xml = "0000".readDocument.parseXPath("/dp").front;
assert(xml.departureTime == TimeOfDay(0, 0));
xml = "0013".readDocument.parseXPath("/dp").front;
assert(xml.departureTime == TimeOfDay(0, 13));
xml = "1100".readDocument.parseXPath("/dp").front;
assert(xml.departureTime == TimeOfDay(11, 00));
xml = "1242".readDocument.parseXPath("/dp").front;
assert(xml.departureTime == TimeOfDay(12, 42));
xml = "2359".readDocument.parseXPath("/dp").front;
assert(xml.departureTime == TimeOfDay(23, 59));
assertThrown!DateTimeException("2400".readDocument.parseXPath("/dp")
.front.departureTime);
assertThrown!DateTimeException("0061".readDocument.parseXPath("/dp")
.front.departureTime);
assertThrown!DateTimeException("2567".readDocument.parseXPath("/dp")
.front.departureTime);
assertThrown!DateTimeException("".readDocument.parseXPath("/dp")
.front.departureTime);
assertThrown!DateTimeException("0".readDocument.parseXPath("/dp")
.front.departureTime);
assertThrown!DateTimeException("00".readDocument.parseXPath("/dp")
.front.departureTime);
assertThrown!DateTimeException("000000".readDocument.parseXPath("/dp")
.front.departureTime);
assertThrown!DateTimeException("00:00".readDocument.parseXPath("/dp")
.front.departureTime);
assertThrown!DateTimeException("abcd".readDocument.parseXPath("/dp")
.front.departureTime);
}
auto delay(XmlNode dp)
in
{
assert(dp.getName == departureNodeName);
}
body
{
immutable useRealtimeString = dp.parseXPath(useRealTimeXPath).front.getCData;
if (useRealtimeString == "0")
return dur!"minutes"(0);
else if (useRealtimeString == "1")
{
try
{
immutable expectedTime = dp.departureTime;
immutable realTime = dp.departureTime!realTimeNodeName;
auto timeDiff = realTime - expectedTime;
if (timeDiff < dur!"minutes"(0))
timeDiff = dur!"hours"(24) + timeDiff;
return timeDiff;
}
catch (CouldNotFindeNodeException e)
{
return dur!"minutes"(0);
}
}
else
throw new UnexpectedValueException!string(useRealtimeString, "realtime");
}
@system unittest
{
import std.exception : assertThrown;
import core.exception : AssertError;
auto xml = "0".readDocument.parseXPath("/dp").front;
assert(xml.delay == dur!"minutes"(0));
xml = "".readDocument.parseXPath("/dp").front;
assertThrown!(UnexpectedValueException!string)(xml.delay);
xml = "2".readDocument.parseXPath("/dp").front;
assertThrown!(UnexpectedValueException!string)(xml.delay);
xml = "a".readDocument.parseXPath("/dp").front;
assertThrown!(UnexpectedValueException!string)(xml.delay);
xml = "1".readDocument.parseXPath("/dp").front;
assertThrown!AssertError(xml.delay);
xml = "1".readDocument.parseXPath("/dp").front;
assertThrown!DateTimeException(xml.delay);
xml = "1".readDocument.parseXPath("/dp").front;
assertThrown!AssertError(xml.delay);
xml = "".readDocument.parseXPath("/dp").front;
assertThrown!AssertError(xml.delay);
xml = "1".readDocument.parseXPath("/dp")
.front;
assertThrown!DateTimeException(xml.delay);
xml = "1".readDocument.parseXPath("/dp")
.front;
assertThrown!DateTimeException(xml.delay);
xml = "10000".readDocument.parseXPath("/dp")
.front;
assertThrown!DateTimeException(xml.delay);
xml = "10000"
.readDocument.parseXPath("/dp").front;
assert(xml.delay == dur!"minutes"(0));
xml = "10000"
.readDocument.parseXPath("/dp").front;
assert(xml.delay == dur!"minutes"(1));
xml = "11751"
.readDocument.parseXPath("/dp").front;
assert(xml.delay == dur!"minutes"(2));
xml = "11000"
.readDocument.parseXPath("/dp").front;
assert(xml.delay == dur!"minutes"(10));
xml = "11242"
.readDocument.parseXPath("/dp").front;
assert(xml.delay == dur!"minutes"(19));
xml = "11242"
.readDocument.parseXPath("/dp").front;
assert(xml.delay == dur!"minutes"(678));
xml = "12359"
.readDocument.parseXPath("/dp").front;
assert(xml.delay == dur!"minutes"(1));
}