{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# LANGUAGE LambdaCase #-}
module Yesod.Auth.Simple.Instance.Persist.EmailTextCI where

import ClassyPrelude
import Database.Persist.Sql
import Yesod.Auth.Simple.Types

instance PersistFieldSql Email where
  sqlType :: Proxy Email -> SqlType
sqlType Proxy Email
_ = Text -> SqlType
SqlOther (Text -> SqlType) -> Text -> SqlType
forall a b. (a -> b) -> a -> b
$ [Element Text] -> Text
forall seq. IsSequence seq => [Element seq] -> seq
pack [Char]
[Element Text]
"citext"

instance PersistField Email where
  toPersistValue :: Email -> PersistValue
toPersistValue (Email Text
e) = Text -> PersistValue
forall a. PersistField a => a -> PersistValue
toPersistValue Text
e
  fromPersistValue :: PersistValue -> Either Text Email
fromPersistValue = \case
    -- use `PersistLiteral_ Escaped bs` in newer persistent
    PersistDbSpecific ByteString
bs -> Email -> Either Text Email
forall a b. b -> Either a b
Right (Email -> Either Text Email) -> Email -> Either Text Email
forall a b. (a -> b) -> a -> b
$ Text -> Email
Email (ByteString -> Text
forall textual binary. Utf8 textual binary => binary -> textual
decodeUtf8 ByteString
bs)
    PersistValue
e -> Text -> Either Text Email
forall a b. a -> Either a b
Left (Text -> Either Text Email) -> Text -> Either Text Email
forall a b. (a -> b) -> a -> b
$ [Element Text] -> Text
forall seq. IsSequence seq => [Element seq] -> seq
pack [Char]
[Element Text]
"Not a PersistDbSpecific: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> PersistValue -> Text
forall a. Show a => a -> Text
tshow PersistValue
e