/
Internal.purs
186 lines (161 loc) · 4.3 KB
/
Internal.purs
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
module Tupc.Internal (module Tupc.Internal, module Exported) where
import Prelude as Exported
import Data.Array (uncons, cons) as Exported
import Data.Maybe as Exported
import Data.Tuple as Exported
import Data.Either as Exported
import Data.Foldable as Exported
import Data.Traversable as Exported
import Data.SubRecord (SubRecord) as Exported
import Data.Symbol as Exported
import Data.Map (Map) as Exported
import Control.Monad.Eff as Exported
import Control.Monad.Eff.Console (log, logShow) as Exported
import Control.Monad.Eff.Exception (throw) as Exported
import Prelude
import Data.Maybe
import Data.Tuple
import Data.Map (Map)
import Data.Map as Map
import Data.Argonaut (class EncodeJson, class DecodeJson, decodeJson, (:=), (~>), (.?))
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Eq as Rep
import Data.Generic.Rep.Show as Rep
import Data.SubRecord
data DirectionX
= DirXLeft
| DirXRight
derive instance genericDirectionX :: Generic DirectionX _
instance eqDirectionX :: Eq DirectionX where
eq = Rep.genericEq
instance showDirectionX :: Show DirectionX where
show = Rep.genericShow
data DirectionY
= DirYUp
| DirYDown
derive instance genericDirectionY :: Generic DirectionY _
instance eqDirectionY :: Eq DirectionY where
eq = Rep.genericEq
instance showDirectionY :: Show DirectionY where
show = Rep.genericShow
data OriginX
= OriXLeft
| OriXRight
derive instance genericOriginX :: Generic OriginX _
instance eqOriginX :: Eq OriginX where
eq = Rep.genericEq
instance showOriginX :: Show OriginX where
show = Rep.genericShow
data OriginY
= OriYUp
| OriYDown
derive instance genericOriginY :: Generic OriginY _
instance eqOriginY :: Eq OriginY where
eq = Rep.genericEq
instance showOriginY :: Show OriginY where
show = Rep.genericShow
-- raw json representation of configuration parameters
type JsonConfig =
{ scale :: Int
, scaleX :: Maybe Int
, scaleY :: Maybe Int
, ignore :: Array Char
, ignoreExtra :: Array Char
, originX :: OriginX
, originY :: OriginY
, directionX :: DirectionX
, directionY :: DirectionY
}
-- content in data section
-- example:
-- ["113",
-- "113",
-- "222"]
type Content = Array String
type ConfigContent =
-- lines read from configuration part
-- # should be stripped from the start (TODO: make this into newtype?)
{ config :: Array String
, content :: Content
}
type JsonConfigContent =
{ jsonConfig :: JsonConfig
, content :: Content
}
type SubJsonConfigContent =
{ subJsonConfig :: SubRecord OptParams
, content :: Content
}
newtype Pos = Pos
{ xLeft :: Int
, xRight :: Int
, yTop :: Int
, yBot :: Int
}
derive instance genericPos :: Generic Pos _
instance eqPos :: Eq Pos where
eq = Rep.genericEq
instance showPos :: Show Pos where
show = Rep.genericShow
instance encodeJsonPos :: EncodeJson Pos where
encodeJson (Pos pos) =
"xLeft" := pos.xLeft
~> "xRight" := pos.xRight
~> "yTop" := pos.yTop
~> "yBot" := pos.yBot
instance decodeJsonPos :: DecodeJson Pos where
decodeJson json = do
obj <- decodeJson json
xLeft <- obj .? "xLeft"
xRight <- obj .? "xRight"
yTop <- obj .? "yTop"
yBot <- obj .? "yBot"
pure $ Pos { xLeft, xRight, yTop, yBot }
newtype EnrichedPos = EnrichedPos
{ xLeft :: Int
, xRight :: Int
, yTop :: Int
, yBot :: Int
, xWidth :: Int
, yHeight :: Int
, xCenter :: Int
, yCenter :: Int
}
derive instance genericEnrichedPos :: Generic EnrichedPos _
instance eqEnrichedPos :: Eq EnrichedPos where
eq = Rep.genericEq
instance showEnrichedPos :: Show EnrichedPos where
show = Rep.genericShow
type OptParams =
( scale :: Int
, scaleX :: Maybe Int
, scaleY :: Maybe Int
, ignore :: Array Char
, ignoreExtra :: Array Char
, originX :: OriginX
, originY :: OriginY
, directionX :: DirectionX
, directionY :: DirectionY
)
tupcDefaultsRecord :: JsonConfig
tupcDefaultsRecord =
{ scale: 1
, scaleX: Nothing
, scaleY: Nothing
, ignore: ['+', '-', '|', ' ']
, ignoreExtra: []
, originX: OriXLeft
, originY: OriYUp
, directionX: DirXRight
, directionY: DirYDown
}
tupcDefaults :: Map String String
tupcDefaults = Map.fromFoldable
[ Tuple "scale" "1"
, Tuple "directionX" "Right"
, Tuple "directionY" "Down"
, Tuple "originX" "Left"
, Tuple "originY" "Up"
, Tuple "ignore" "+-|, "
, Tuple "ignoreExtra" ""
]