-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model.hs
53 lines (45 loc) · 1.56 KB
/
Model.hs
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
{-# LANGUAGE StandaloneDeriving #-}
module Model where
#include "include/imports.h"
import Prelude
import Yesod
import Data.Text (Text)
import qualified Data.Text as T
import Database.Persist.Quasi
import Codec.Game.Puz
import Database.Persist.Store
--data Sq = B | E | C Char | Rebus Text deriving (Eq,Read,Show)
newtype GridV = GridV { unGridV :: [Square] } deriving (Eq, Read, Show)
instance PersistField GridV where
toPersistValue (GridV sqs) = PersistText (T.pack $ map go sqs)
where
go Black = '.'
go (Letter Nothing _) = ' '
go (Letter (Just c) _) = c
fromPersistValue (PersistText t) = Right . GridV $ map go (T.unpack t)
where
go '.' = Black
go ' ' = Letter Nothing Plain
go c = Letter (Just c) Plain
fromPersistValue _ = Left "I only want text"
sqlType _ = SqlString
textToGridV = GridV . map go . T.unpack
where go ' ' = Letter Nothing Plain
go '.' = Black
go c = Letter (Just c) Plain
gridVToText = T.pack . map go . unGridV
where go (Letter (Just c) Plain) = c
go (Letter Nothing Plain) = ' '
go Black = '.'
go _ = '#'
{-deriving instance Read Puzzle
deriving instance Read Dir-}
deriving instance Read Square
deriving instance Read Style
$(derivePersistField "Square")
-- You can define all of your database entities in the entities file.
-- You can find more information on persistent and how to declare entities
-- at:
-- http://www.yesodweb.com/book/persistent/
share [mkPersist sqlSettings, mkMigrate "migrateAll"]
$(persistFileWith lowerCaseSettings "config/models")