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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE FlexibleInstances #-}
module VDV451 where
import qualified Data.ByteString.Lazy as LB
import qualified Data.ByteString as SB
import Data.ByteString (ByteString)
import Data.Text (Text)
import qualified Data.Text as T
import Data.Vector (Vector)
import qualified Data.Vector as V
import qualified Data.ByteString.Char8 as C8
import GHC.Base (ord)
import Fmt ((+|),(|+))
import Data.Time (Day, toGregorian, UTCTime (..), DiffTime, formatTime, defaultTimeLocale)
import Data.String (IsString)
import GHC.Exts (IsString(..))
import Data.Time.Format.ISO8601 (formatShow)
import Data.Data (Proxy (..))
class ÖPNVEncode a where
encode :: a -> ByteString
default encode :: Show a => a -> ByteString
encode = C8.pack . show
instance ÖPNVEncode Text where
-- hopefully not too naive iso8859-1 encoding
encode text = if T.null unsafe
then C8.pack bytes
else error ("invalid unicode in iso8859-1 string: "+|unsafe|+".")
where (safe, unsafe) = T.span (\c -> ord c <= 0xFF) text
bytes = "\"" <> (concatMap escape (T.unpack safe)) <> "\""
escape c = case c of
'"' -> "\"\""
a -> [a]
instance ÖPNVEncode Day where
encode day = "\""+|crop 2 d|+"."+|crop 2 m|+"."+|crop 4 y|+"\""
where (y,m,d) = toGregorian day
crop n thing = if T.length shown < n
then T.replicate (n - T.length shown) "0" <> shown
else T.takeEnd n shown
where shown = T.pack $ show thing
-- | for things which can be encoded as either string or number,
-- this module defaults to strings. Add this newtype to the schema
-- to make things encode as numbers instead.
newtype AsNumber a = AsNumber a
instance ÖPNVEncode (AsNumber Day) where
encode (AsNumber day) =
encode (d*1000000 + m*10000 + (fromInteger y `mod` 10000))
where (y,m,d) = toGregorian day
instance ÖPNVEncode DiffTime where
encode = C8.pack . formatTime defaultTimeLocale "\"%2H:%2M:%2S\""
instance ÖPNVEncode Int
instance ÖPNVEncode Integer
data ÖPNVBefehl =
MOD | SRC | CHS | VER | IFV
| DVE | FFT | TBL | ATR | FRM
| REC | END | EOF | COM
deriving Show
instance ÖPNVEncode ÖPNVBefehl where
encode MOD = "mod"
encode SRC = "src"
encode CHS = "chs"
encode VER = "ver"
encode IFV = "ifv"
encode DVE = "dve"
encode FFT = "fft"
encode TBL = "tbl"
encode ATR = "atr"
encode FRM = "frm"
encode REC = "rec"
encode END = "end"
encode EOF = "eof"
encode COM = "com"
data ÖPNVType =
ÖChar Int | ÖNum Int
deriving Show
instance ÖPNVEncode ÖPNVType where
encode (ÖChar n) = "char["+|n|+"]"
encode (ÖNum n) = "num["+|n|+".0]"
class ÖPNVDatum a where
tableName :: Proxy a -> ByteString
tableSchema :: Proxy a -> [(ByteString, ÖPNVType, a -> Feld)]
encodeRow :: forall a. ÖPNVDatum a => a -> [Feld]
encodeRow a = fmap (\f -> f a) accessors
where accessors = fmap (\(_,_,a) -> a) (tableSchema (Proxy @a))
tableInfo :: ÖPNVDatum a => Proxy a -> [(ByteString, ÖPNVType)]
tableInfo proxy = fmap (\(n, ty, _) -> (n, ty)) (tableSchema proxy)
data Feld = forall a. ÖPNVEncode a => F a | Raw ByteString
instance IsString Feld where
fromString = Raw . C8.pack
data ÖPNVOptions = ÖPNVOptions
{ öpnvSource :: Text
, öpnvProgramVersion :: Text
, öpnvDataVersion :: Text
} deriving Show
öpnvSchnittstellenDaten :: forall a. ÖPNVDatum a => ÖPNVOptions -> UTCTime -> Vector a -> LB.ByteString
öpnvSchnittstellenDaten ÖPNVOptions{..} time rows = LB.intercalate "\n" $ fmap mkRow
[ [F MOD, "DD.MM.YYYY", "HH:MM:SS", "free"]
, [F SRC, F öpnvSource, F (utctDay time), F (utctDayTime time) ]
, [F CHS, F ("ISO8859-1" :: Text)]
, [F VER, F öpnvProgramVersion]
, [F IFV, F ("1.0" :: Text)]
, [F DVE, F öpnvDataVersion]
, [F FFT, ""] -- this one is probably not needed?
, [F TBL, Raw (tableName (Proxy @a))]
, F ATR : fmap Raw colNames
, F FRM : fmap F colTypes]
<> (fmap (mkRow . ((:) (F REC)) . encodeRow) (V.toList rows))
<> fmap mkRow
[ [F END, F (length rows)]
, [F EOF, "1"]]
where
mkRow :: [Feld] -> LB.ByteString
mkRow = LB.fromStrict
. C8.intercalate "; "
. fmap (\case { (F a) -> encode a; Raw a -> a })
(colNames, colTypes) = unzip (tableInfo (Proxy @a))
|