ctucx.git: nimhafas

[nimlang] hafas-client library

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
import ../types
import ./date
import options
import json

proc parseStopoverPart(common: CommonData, mode: string, h: HafasStopParams): StopoverPart =
  
  if (mode != "arrival"):
    result.plannedPlatform = h.dPlatfS
    result.prognosedPlatform = h.dPlatfR
    result.plannedTime  = parseDate(common, h.dTimeS, h.dTZOffset)
    result.prognosedTime = parseDate(common, h.dTimeR, h.dTZOffset)
  else:
    result.plannedPlatform   = h.aPlatfS
    result.prognosedPlatform = h.aPlatfR
    result.plannedTime       = parseDate(common, h.aTimeS, h.aTZOffset)
    result.prognosedTime     = parseDate(common, h.aTimeR, h.aTZOffset)

proc mkParseStopover*(common: CommonData): proc =
  proc parseStopover(s: JsonNode): Stopover =
    let typeStr = s{"type"}.getStr()
    if typeStr != "N":
      echo pretty(s)
      raise newException(CatchableError, "Unimplemented hafas stopover type: " & typeStr)

    let h = s.to(HafasStopParams)
    result.stop = common.points[s{"locX"}.getInt()].stop.get
    result.cancelled    = h.aCncl.get(false) or h.dCncl.get(false)
    result.arrival      = common.parseStopoverPart("arrival", h)
    result.departure    = common.parseStopoverPart("departure", h)

  return parseStopover