{-# LANGUAGE FlexibleContexts #-}
module Toml.Codec.Combinator.Common
( match
, whenLeftBiMapError
) where
import Control.Monad.State (modify)
import Validation (Validation (..))
import Toml.Codec.BiMap (BiMap (..), TomlBiMap, TomlBiMapError)
import Toml.Codec.Error (TomlDecodeError (..))
import Toml.Codec.Types (Codec (..), TomlCodec, TomlEnv, TomlState, eitherToTomlState)
import Toml.Type.AnyValue (AnyValue (..))
import Toml.Type.Key (Key)
import Toml.Type.TOML (TOML (..), insertKeyAnyVal)
import qualified Data.HashMap.Strict as HashMap
match :: forall a . TomlBiMap a AnyValue -> Key -> TomlCodec a
match :: TomlBiMap a AnyValue -> Key -> TomlCodec a
match BiMap{..} key :: Key
key = TomlEnv a -> (a -> TomlState a) -> TomlCodec a
forall i o. TomlEnv o -> (i -> TomlState o) -> Codec i o
Codec TomlEnv a
input a -> TomlState a
output
where
input :: TomlEnv a
input :: TomlEnv a
input = \toml :: TOML
toml -> case Key -> HashMap Key AnyValue -> Maybe AnyValue
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup Key
key (TOML -> HashMap Key AnyValue
tomlPairs TOML
toml) of
Nothing -> [TomlDecodeError] -> Validation [TomlDecodeError] a
forall e a. e -> Validation e a
Failure [Key -> TomlDecodeError
KeyNotFound Key
key]
Just anyVal :: AnyValue
anyVal -> Key
-> Either TomlBiMapError a
-> (a -> Validation [TomlDecodeError] a)
-> Validation [TomlDecodeError] a
forall a b.
Key
-> Either TomlBiMapError a
-> (a -> Validation [TomlDecodeError] b)
-> Validation [TomlDecodeError] b
whenLeftBiMapError Key
key (AnyValue -> Either TomlBiMapError a
backward AnyValue
anyVal) a -> Validation [TomlDecodeError] a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
output :: a -> TomlState a
output :: a -> TomlState a
output a :: a
a = do
AnyValue
anyVal <- Either TomlBiMapError AnyValue -> TomlState AnyValue
forall e a. Either e a -> TomlState a
eitherToTomlState (Either TomlBiMapError AnyValue -> TomlState AnyValue)
-> Either TomlBiMapError AnyValue -> TomlState AnyValue
forall a b. (a -> b) -> a -> b
$ a -> Either TomlBiMapError AnyValue
forward a
a
a
a a -> TomlState () -> TomlState a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ (TOML -> TOML) -> TomlState ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (Key -> AnyValue -> TOML -> TOML
insertKeyAnyVal Key
key AnyValue
anyVal)
whenLeftBiMapError
:: Key
-> Either TomlBiMapError a
-> (a -> Validation [TomlDecodeError] b)
-> Validation [TomlDecodeError] b
whenLeftBiMapError :: Key
-> Either TomlBiMapError a
-> (a -> Validation [TomlDecodeError] b)
-> Validation [TomlDecodeError] b
whenLeftBiMapError key :: Key
key val :: Either TomlBiMapError a
val action :: a -> Validation [TomlDecodeError] b
action = case Either TomlBiMapError a
val of
Right a :: a
a -> a -> Validation [TomlDecodeError] b
action a
a
Left err :: TomlBiMapError
err -> [TomlDecodeError] -> Validation [TomlDecodeError] b
forall e a. e -> Validation e a
Failure [Key -> TomlBiMapError -> TomlDecodeError
BiMapError Key
key TomlBiMapError
err]