{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE FlexibleContexts #-}
module Data.TypedEncoding.Internal.Instances.Combinators where
import Data.String
import Data.Proxy
import Text.Read
import Data.TypedEncoding.Internal.Types
import Data.TypedEncoding.Internal.Class.IsStringR
import GHC.TypeLits
foldEnc :: forall (xs2 :: [Symbol]) (xs1 :: [Symbol]) f c s1 s2
. (Foldable f, Functor f)
=> c -> (s1 -> s2 -> s2) -> s2 -> f (Enc xs1 c s1) -> Enc xs2 c s2
foldEnc c f sinit = unsafeSetPayload c . foldr f sinit . fmap getPayload
foldEncStr :: forall (xs2 :: [Symbol]) (xs1 :: [Symbol]) f c s1 s2
. (Foldable f, Functor f, IsString s2)
=> c -> (s1 -> s2 -> s2) -> f (Enc xs1 c s1) -> Enc xs2 c s2
foldEncStr c f = foldEnc c f ""
foldCheckedEnc :: forall (xs2 :: [Symbol]) f c s1 s2
. (Foldable f, Functor f)
=> c -> ([EncAnn] -> s1 -> s2 -> s2) -> s2 -> f (CheckedEnc c s1) -> Enc xs2 c s2
foldCheckedEnc c f sinit = unsafeSetPayload c . foldr (uncurry f) sinit . fmap getCheckedEncPayload
foldCheckedEncStr :: forall (xs2 :: [Symbol]) f c s1 s2 . (Foldable f, Functor f, IsString s2) => c -> ([EncAnn] -> s1 -> s2 -> s2) -> f (CheckedEnc c s1) -> Enc xs2 c s2
foldCheckedEncStr c f = foldCheckedEnc c f ""
splitPayload :: forall (xs2 :: [Symbol]) (xs1 :: [Symbol]) c s1 s2 .
(s1 -> [s2])
-> Enc xs1 c s1
-> [Enc xs2 c s2]
splitPayload f (MkEnc _ c s1) = map (MkEnc Proxy c) (f s1)
splitSomePayload :: forall c s1 s2 .
([EncAnn] -> s1 -> [([EncAnn], s2)])
-> CheckedEnc c s1
-> [CheckedEnc c s2]
splitSomePayload f (MkCheckedEnc ann1 c s1) = map (\(ann2, s2) -> MkCheckedEnc ann2 c s2) (f ann1 s1)
verifyWithRead :: forall a str . (IsStringR str, Read a, Show a) => String -> str -> Either String str
verifyWithRead msg x =
let s = toString x
a :: Maybe a = readMaybe s
check = (show <$> a) == Just s
in if check
then Right x
else Left $ "Payload does not satisfy format " ++ msg ++ ": " ++ s
verifyDynEnc :: forall s str err1 err2 enc a. (KnownSymbol s, Show err1, Show err2) =>
Proxy s
-> (Proxy s -> Either err1 enc)
-> (enc -> str -> Either err2 a)
-> str
-> Either EncodeEx str
verifyDynEnc p findenc decoder str =
do
enc <- asEncodeEx p . findenc $ p
case decoder enc str of
Left err -> Left $ EncodeEx p err
Right r -> Right str