2015-12-20 14:35:04 +00:00
|
|
|
module Test.Instances where
|
|
|
|
|
|
|
|
import Data.YAML.Foreign.Encode
|
2017-05-20 21:32:55 +00:00
|
|
|
import Data.Argonaut.Core (toObject, toString)
|
2019-10-19 21:13:45 +00:00
|
|
|
import Data.Argonaut.Decode.Combinators (getField)
|
2017-05-20 21:32:55 +00:00
|
|
|
import Data.Argonaut.Decode.Class (class DecodeJson)
|
|
|
|
import Data.Either (Either(..))
|
2018-09-08 20:46:12 +00:00
|
|
|
import Data.Generic.Rep (class Generic)
|
|
|
|
import Data.Generic.Rep.Eq (genericEq)
|
|
|
|
import Data.Generic.Rep.Show (genericShow)
|
2017-05-20 21:32:55 +00:00
|
|
|
import Data.Maybe (maybe)
|
|
|
|
import Prelude (class Eq, class Show, bind, pure, ($))
|
2015-12-20 14:35:04 +00:00
|
|
|
|
|
|
|
data Point = Point Int Int
|
|
|
|
|
|
|
|
data Mobility
|
|
|
|
= Fix
|
|
|
|
| Flex
|
|
|
|
|
|
|
|
data GeoObject = GeoObject
|
|
|
|
{ name :: String
|
|
|
|
, scale :: Number
|
|
|
|
, points :: Array Point
|
|
|
|
, mobility :: Mobility
|
|
|
|
, coverage :: Number
|
|
|
|
}
|
|
|
|
|
2018-09-08 20:46:12 +00:00
|
|
|
derive instance genericGeoObject :: Generic GeoObject _
|
|
|
|
instance showGeoObject :: Show GeoObject where show = genericShow
|
|
|
|
instance eqGeoObject :: Eq GeoObject where eq = genericEq
|
2015-12-20 14:35:04 +00:00
|
|
|
|
2018-09-08 20:46:12 +00:00
|
|
|
derive instance genericPoint :: Generic Point _
|
|
|
|
instance showPoint :: Show Point where show = genericShow
|
|
|
|
instance eqPoint :: Eq Point where eq = genericEq
|
2015-12-20 14:35:04 +00:00
|
|
|
|
2018-09-08 20:46:12 +00:00
|
|
|
derive instance genericMobility :: Generic Mobility _
|
|
|
|
instance showMobility :: Show Mobility where show = genericShow
|
|
|
|
instance eqMobility :: Eq Mobility where eq = genericEq
|
2015-12-20 14:35:04 +00:00
|
|
|
|
2017-05-20 21:32:55 +00:00
|
|
|
instance geoJson :: DecodeJson GeoObject where
|
|
|
|
decodeJson s = do
|
|
|
|
obj <- maybe (Left "GeoObject is not an object.") Right (toObject s)
|
|
|
|
name <- getField obj "Name"
|
|
|
|
scale <- getField obj "Scale"
|
|
|
|
points <- getField obj "Points"
|
|
|
|
mobility <- getField obj "Mobility"
|
|
|
|
coverage <- getField obj "Coverage"
|
|
|
|
pure $ GeoObject { name, scale, points, mobility, coverage }
|
|
|
|
|
|
|
|
instance mobilityJson :: DecodeJson Mobility where
|
|
|
|
decodeJson s = do
|
|
|
|
mob <- maybe (Left "Mobility is not a string.") Right (toString s)
|
|
|
|
case mob of
|
|
|
|
"Fix" -> pure Fix
|
|
|
|
"Flex" -> pure Flex
|
|
|
|
_ -> Left "Mobility must be either Flex or Fix"
|
2015-12-20 14:35:04 +00:00
|
|
|
|
2017-05-20 21:32:55 +00:00
|
|
|
instance pointJson :: DecodeJson Point where
|
|
|
|
decodeJson s = do
|
|
|
|
obj <- maybe (Left "Point is not an object.") Right (toObject s)
|
|
|
|
x <- getField obj "X"
|
|
|
|
y <- getField obj "Y"
|
|
|
|
pure $ Point x y
|
2015-12-20 14:35:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
instance pointToYAML :: ToYAML Point where
|
|
|
|
toYAML (Point x y) =
|
|
|
|
object
|
|
|
|
[ "X" := x
|
|
|
|
, "Y" := y
|
|
|
|
]
|
|
|
|
|
|
|
|
instance mobilityToYAML :: ToYAML Mobility where
|
|
|
|
toYAML Fix = toYAML "Fix"
|
|
|
|
toYAML Flex = toYAML "Flex"
|
|
|
|
|
|
|
|
instance archiObjectToYAML :: ToYAML GeoObject where
|
|
|
|
toYAML (GeoObject o) =
|
|
|
|
object
|
|
|
|
[ "Name" := o.name
|
|
|
|
, "Scale" := o.scale
|
|
|
|
, "Points" := o.points
|
|
|
|
, "Mobility" := o.mobility
|
|
|
|
, "Coverage" := o.coverage
|
|
|
|
]
|