2017-10-10 04:16:12 -04:00
|
|
|
module Util ( compileMustache, entityToTuple, mapFirst ) where
|
2017-10-10 00:16:50 -04:00
|
|
|
|
2017-10-10 04:16:12 -04:00
|
|
|
import Database.Persist ( Entity(..), Key )
|
2017-10-10 00:16:50 -04:00
|
|
|
import Text.Mustache ( Template(..), compileTemplate )
|
|
|
|
import Text.Mustache.Types ( Node(TextBlock) )
|
|
|
|
import Text.Parsec.Error ( ParseError )
|
|
|
|
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Data.HashMap.Lazy as M
|
2017-10-05 01:19:15 -04:00
|
|
|
|
|
|
|
mapFirst :: (a -> a) -> [a] -> [a]
|
|
|
|
mapFirst f (x:xs) = f x : xs
|
|
|
|
mapFirst _ [] = []
|
2017-10-10 00:16:50 -04:00
|
|
|
|
|
|
|
compileMustache :: String -> T.Text -> Template
|
|
|
|
compileMustache n = either errorTemplate id . compileTemplate n
|
|
|
|
|
|
|
|
errorTemplate :: ParseError -> Template
|
|
|
|
errorTemplate err = Template
|
|
|
|
{ name = "error"
|
|
|
|
, ast = [TextBlock . T.pack $ show err]
|
|
|
|
, partials = M.empty
|
|
|
|
}
|
2017-10-10 04:16:12 -04:00
|
|
|
|
|
|
|
entityToTuple :: Entity t -> (Key t, t)
|
|
|
|
entityToTuple (Entity key value) = (key, value)
|