module IHP.ValidationSupport.Types where

import IHP.Prelude
import qualified Data.Text as Text

data ValidatorResult = Success | Failure !Text deriving (Int -> ValidatorResult -> ShowS
[ValidatorResult] -> ShowS
ValidatorResult -> String
(Int -> ValidatorResult -> ShowS)
-> (ValidatorResult -> String)
-> ([ValidatorResult] -> ShowS)
-> Show ValidatorResult
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ValidatorResult] -> ShowS
$cshowList :: [ValidatorResult] -> ShowS
show :: ValidatorResult -> String
$cshow :: ValidatorResult -> String
showsPrec :: Int -> ValidatorResult -> ShowS
$cshowsPrec :: Int -> ValidatorResult -> ShowS
Show, ValidatorResult -> ValidatorResult -> Bool
(ValidatorResult -> ValidatorResult -> Bool)
-> (ValidatorResult -> ValidatorResult -> Bool)
-> Eq ValidatorResult
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ValidatorResult -> ValidatorResult -> Bool
$c/= :: ValidatorResult -> ValidatorResult -> Bool
== :: ValidatorResult -> ValidatorResult -> Bool
$c== :: ValidatorResult -> ValidatorResult -> Bool
Eq)

{-# INLINE isSuccess #-}
isSuccess :: ValidatorResult -> Bool
isSuccess ValidatorResult
Success = Bool
True
isSuccess ValidatorResult
_       = Bool
False

{-# INLINE isFailure #-}
isFailure :: ValidatorResult -> Bool
isFailure Failure {} = Bool
True
isFailure ValidatorResult
_  = Bool
False

{-# INLINE attachValidatorResult #-}
attachValidatorResult :: (KnownSymbol field, HasField "meta" model MetaBag, SetField "meta" model MetaBag) => Proxy field -> ValidatorResult -> model -> model
attachValidatorResult :: Proxy field -> ValidatorResult -> model -> model
attachValidatorResult Proxy field
field ValidatorResult
Success model
record = model
record
attachValidatorResult Proxy field
field (Failure Text
message) model
record = Proxy field -> Text -> model -> model
forall (field :: Symbol) model.
(KnownSymbol field, HasField "meta" model MetaBag,
 SetField "meta" model MetaBag) =>
Proxy field -> Text -> model -> model
attachFailure Proxy field
field Text
message model
record

{-# INLINE attachFailure #-}
attachFailure :: (KnownSymbol field, HasField "meta" model MetaBag, SetField "meta" model MetaBag) => Proxy field -> Text -> model -> model
attachFailure :: Proxy field -> Text -> model -> model
attachFailure Proxy field
field !Text
message = Proxy "meta" -> (MetaBag -> MetaBag) -> model -> model
forall k model (name :: Symbol) value (updateFunction :: k).
(KnownSymbol name, HasField name model value,
 SetField name model value) =>
Proxy name -> (value -> value) -> model -> model
modify IsLabel "meta" (Proxy "meta")
Proxy "meta"
#meta MetaBag -> MetaBag
prependAnnotation
    where
        prependAnnotation :: MetaBag -> MetaBag
        prependAnnotation :: MetaBag -> MetaBag
prependAnnotation = Proxy "annotations"
-> ([(Text, Text)] -> [(Text, Text)]) -> MetaBag -> MetaBag
forall k model (name :: Symbol) value (updateFunction :: k).
(KnownSymbol name, HasField name model value,
 SetField name model value) =>
Proxy name -> (value -> value) -> model -> model
modify IsLabel "annotations" (Proxy "annotations")
Proxy "annotations"
#annotations (\[(Text, Text)]
a -> (Text, Text)
annotation(Text, Text) -> [(Text, Text)] -> [(Text, Text)]
forall a. a -> [a] -> [a]
:[(Text, Text)]
a)
        annotation :: (Text, Text)
annotation = (String -> Text
Text.pack (Proxy field -> String
forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal Proxy field
field), Text
message)

{-# INLINE getValidationFailure #-}
getValidationFailure :: (KnownSymbol field, HasField "meta" model MetaBag) => Proxy field -> model -> Maybe Text
getValidationFailure :: Proxy field -> model -> Maybe Text
getValidationFailure Proxy field
field model
model =
        model
model
            model -> (model -> MetaBag) -> MetaBag
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> Proxy "meta" -> model -> MetaBag
forall model (name :: Symbol) value.
(KnownSymbol name, HasField name model value) =>
Proxy name -> model -> value
get IsLabel "meta" (Proxy "meta")
Proxy "meta"
#meta
            MetaBag -> (MetaBag -> [(Text, Text)]) -> [(Text, Text)]
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> Proxy "annotations" -> MetaBag -> [(Text, Text)]
forall model (name :: Symbol) value.
(KnownSymbol name, HasField name model value) =>
Proxy name -> model -> value
get IsLabel "annotations" (Proxy "annotations")
Proxy "annotations"
#annotations
            [(Text, Text)] -> ([(Text, Text)] -> Maybe Text) -> Maybe Text
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> Text -> [(Text, Text)] -> Maybe Text
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
fieldName
    where
        fieldName :: Text
fieldName = String -> Text
Text.pack (Proxy field -> String
forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal Proxy field
field)