never executed always true always false
    1 {-|
    2 Module: Flaw.Editor.Entity.Internal
    3 Description: Entity internals.
    4 License: MIT
    5 -}
    6 
    7 module Flaw.Editor.Entity.Internal
    8   ( hashTextDecl
    9   ) where
   10 
   11 import qualified Crypto.Hash as C
   12 import qualified Data.ByteArray as BA
   13 import qualified Data.ByteString as B
   14 import Data.IORef
   15 import qualified Data.Text as T
   16 import qualified Data.Text.Encoding as T
   17 import Language.Haskell.TH
   18 import qualified Language.Haskell.TH.Syntax as TH
   19 
   20 import Flaw.Build
   21 
   22 -- | Calculate hash of text and store it in a top-level declaration.
   23 hashTextDecl :: String -> TypeQ -> (ExpQ -> ExpQ) -> T.Text -> ExpQ
   24 hashTextDecl declPrefix declType f str = do
   25   -- add hash as top decl
   26   cnt <- runIO $ atomicModifyIORef TH.counter $ \c -> (c + 1, c)
   27   n <- newName $ declPrefix <> show cnt
   28   TH.addTopDecls =<< sequence
   29     [ pragInlD n NoInline FunLike AllPhases
   30     , sigD n declType
   31     , valD (varP n) (normalB $ f $ embedExp (BA.convert (C.hash (T.encodeUtf8 str) :: C.Digest C.SHA1) :: B.ByteString)) []
   32     ]
   33   varE n