Changed the underlying data to be Int-based rather than Float-based
Getty Ritter
8 years ago
4 | 4 | import Data.Eben (Value(..)) |
5 | 5 | import qualified Data.Eben as Eben |
6 | 6 | |
7 |
type Point = ( |
|
7 | type Point = (Int, Int) | |
8 | 8 | |
9 | 9 | data VKImage = VKImage |
10 | 10 | { vkLines :: [[Point]] |
11 | 11 | , vkMeta :: VKMeta |
12 | 12 | } deriving (Eq, Show) |
13 | 13 | |
14 |
data VKMeta = VKMeta { vkSize :: ( |
|
14 | data VKMeta = VKMeta { vkSize :: (Int, Int) } deriving (Eq, Show) | |
15 | 15 | |
16 | 16 | catch :: Maybe a -> String -> Either String a |
17 | 17 | catch (Just a) _ = Right a |
18 | 18 | catch Nothing m = Left m |
19 | 19 | |
20 | 20 | asPoint :: Value -> Either String Point |
21 | asPoint v = do | |
22 | (x:y:_) <- Eben.asList v `catch` "Point value not a list" | |
23 | x' <- Eben.asFloat x `catch` "Point x-value not a float" | |
24 | y' <- Eben.asFloat y `catch` "Point x-value not a float" | |
25 | return (x', y') | |
21 | asPoint v = Eben.fromEben v `catch` "Unable to parse point" | |
26 | 22 | |
27 | 23 | decode :: ByteString -> Either String VKImage |
28 | 24 | decode bs = do |
36 | 32 | return VKImage { vkLines = points |
37 | 33 | , vkMeta = VKMeta { vkSize = (w, h) } |
38 | 34 | } |
35 | ||
36 | encode :: VKImage -> ByteString | |
37 | encode vk = Eben.encode $ Eben.dict | |
38 | [ ("meta", Eben.toEben (vkSize (vkMeta vk))) | |
39 | , ("lines", Eben.toEben (vkLines vk)) | |
40 | ] |