module Freckle.App.Memcached.MD5
  ( md5CacheKey
  , md5Key
  , md5Text
  ) where

import Freckle.App.Prelude

import qualified Data.ByteString.Lazy as BSL
import qualified Data.Digest.Pure.MD5 as Digest
import Freckle.App.Memcached.CacheKey

md5CacheKey :: Show a => a -> CacheKey
md5CacheKey :: forall a. Show a => a -> CacheKey
md5CacheKey = (String -> CacheKey)
-> (CacheKey -> CacheKey) -> Either String CacheKey -> CacheKey
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> String -> CacheKey
forall a. HasCallStack => String -> a
error String
"md5 is always cacheable") CacheKey -> CacheKey
forall a. a -> a
id (Either String CacheKey -> CacheKey)
-> (a -> Either String CacheKey) -> a -> CacheKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either String CacheKey
cacheKey (Text -> Either String CacheKey)
-> (a -> Text) -> a -> Either String CacheKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Text
forall a. Show a => a -> Text
md5Key

-- | Pack any showable into an md5 encoded text
md5Key :: Show a => a -> Text
md5Key :: forall a. Show a => a -> Text
md5Key = Text -> Text
md5Text (Text -> Text) -> (a -> Text) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
pack (String -> Text) -> (a -> String) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> String
forall a. Show a => a -> String
show

md5Text :: Text -> Text
md5Text :: Text -> Text
md5Text = String -> Text
pack (String -> Text) -> (Text -> String) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MD5Digest -> String
forall a. Show a => a -> String
show (MD5Digest -> String) -> (Text -> MD5Digest) -> Text -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> MD5Digest
Digest.md5 (ByteString -> MD5Digest)
-> (Text -> ByteString) -> Text -> MD5Digest
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
BSL.fromStrict (ByteString -> ByteString)
-> (Text -> ByteString) -> Text -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
encodeUtf8