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 
34 
35 
36 
37 
import ../types
import bitops

proc parseProduct*(cls: int): Product =
  var tmp = cls
  var res = 0
  while tmp > 1:
    tmp = tmp shr 1
    res += 1

  return Product(res)

proc parseProducts*(pCls: int): Products =
  return Products(
    nationalExp: pCls.testBit(0),
    national:        pCls.testBit(1),
    regionalExp:     pCls.testBit(2),
    regional:        pCls.testBit(3),
    suburban:        pCls.testBit(4),
    bus:             pCls.testBit(5),
    ferry:           pCls.testBit(6),
    subway:          pCls.testBit(7),
    tram:            pCls.testBit(8),
    taxi:            pCls.testBit(9),
  )

proc formatProducts*(p: Products): int =
  if p.nationalExp: result.setBit(0)
  if p.national:        result.setBit(1)
  if p.regionalExp:     result.setBit(2)
  if p.regional:        result.setBit(3)
  if p.suburban:        result.setBit(4)
  if p.bus:             result.setBit(5)
  if p.ferry:           result.setBit(6)
  if p.subway:          result.setBit(7)
  if p.tram:            result.setBit(8)
  if p.taxi:            result.setBit(9)