module Toml.Codec.Di
( dimap
, dioptional
, diwrap
, dimatch
, (.=)
) where
import Control.Applicative (Alternative (..))
import Data.Coerce (Coercible, coerce)
import Toml.Codec.Types (Codec (..), TomlCodec, (<!>))
dimap
:: (b -> a)
-> (a -> b)
-> TomlCodec a
-> TomlCodec b
dimap :: forall b a. (b -> a) -> (a -> b) -> TomlCodec a -> TomlCodec b
dimap b -> a
f a -> b
g TomlCodec a
codec = Codec
{ codecRead :: TomlEnv b
codecRead = (a -> b)
-> Validation [TomlDecodeError] a -> Validation [TomlDecodeError] b
forall a b.
(a -> b)
-> Validation [TomlDecodeError] a -> Validation [TomlDecodeError] b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
g (Validation [TomlDecodeError] a -> Validation [TomlDecodeError] b)
-> (TOML -> Validation [TomlDecodeError] a) -> TomlEnv b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TomlCodec a -> TOML -> Validation [TomlDecodeError] a
forall i o. Codec i o -> TomlEnv o
codecRead TomlCodec a
codec
, codecWrite :: b -> TomlState b
codecWrite = (a -> b) -> TomlState a -> TomlState b
forall a b. (a -> b) -> TomlState a -> TomlState b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
g (TomlState a -> TomlState b)
-> (b -> TomlState a) -> b -> TomlState b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TomlCodec a -> a -> TomlState a
forall i o. Codec i o -> i -> TomlState o
codecWrite TomlCodec a
codec (a -> TomlState a) -> (b -> a) -> b -> TomlState a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> a
f
}
{-# INLINE dimap #-}
dioptional
:: TomlCodec a
-> TomlCodec (Maybe a)
dioptional :: forall a. TomlCodec a -> TomlCodec (Maybe a)
dioptional Codec{a -> TomlState a
TomlEnv a
codecRead :: forall i o. Codec i o -> TomlEnv o
codecWrite :: forall i o. Codec i o -> i -> TomlState o
codecRead :: TomlEnv a
codecWrite :: a -> TomlState a
..} = Codec
{ codecRead :: TomlEnv (Maybe a)
codecRead = (a -> Maybe a)
-> Validation [TomlDecodeError] a
-> Validation [TomlDecodeError] (Maybe a)
forall a b.
(a -> b)
-> Validation [TomlDecodeError] a -> Validation [TomlDecodeError] b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Maybe a
forall a. a -> Maybe a
Just (Validation [TomlDecodeError] a
-> Validation [TomlDecodeError] (Maybe a))
-> TomlEnv a -> TomlEnv (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TomlEnv a
codecRead TomlEnv (Maybe a) -> TomlEnv (Maybe a) -> TomlEnv (Maybe a)
forall (f :: * -> *) a x.
Alternative f =>
(a -> f x) -> (a -> f x) -> a -> f x
<!> \TOML
_ -> Maybe a -> Validation [TomlDecodeError] (Maybe a)
forall a. a -> Validation [TomlDecodeError] a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
forall a. Maybe a
Nothing
, codecWrite :: Maybe a -> TomlState (Maybe a)
codecWrite = (a -> TomlState a) -> Maybe a -> TomlState (Maybe a)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Maybe a -> f (Maybe b)
traverse a -> TomlState a
codecWrite
}
{-# INLINE dioptional #-}
diwrap
:: forall b a .
(Coercible a b)
=> TomlCodec a
-> TomlCodec b
diwrap :: forall b a. Coercible a b => TomlCodec a -> TomlCodec b
diwrap = TomlCodec a -> TomlCodec b
forall a b. Coercible a b => a -> b
coerce
{-# INLINE diwrap #-}
dimatch
:: (b -> Maybe a)
-> (a -> b)
-> TomlCodec a
-> TomlCodec b
dimatch :: forall b a.
(b -> Maybe a) -> (a -> b) -> TomlCodec a -> TomlCodec b
dimatch b -> Maybe a
match a -> b
ctor TomlCodec a
codec = Codec
{ codecRead :: TomlEnv b
codecRead = (a -> b)
-> Validation [TomlDecodeError] a -> Validation [TomlDecodeError] b
forall a b.
(a -> b)
-> Validation [TomlDecodeError] a -> Validation [TomlDecodeError] b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
ctor (Validation [TomlDecodeError] a -> Validation [TomlDecodeError] b)
-> (TOML -> Validation [TomlDecodeError] a) -> TomlEnv b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TomlCodec a -> TOML -> Validation [TomlDecodeError] a
forall i o. Codec i o -> TomlEnv o
codecRead TomlCodec a
codec
, codecWrite :: b -> TomlState b
codecWrite = \b
c -> case b -> Maybe a
match b
c of
Maybe a
Nothing -> TomlState b
forall a. TomlState a
forall (f :: * -> *) a. Alternative f => f a
empty
Just a
d -> a -> b
ctor (a -> b) -> TomlState a -> TomlState b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TomlCodec a -> a -> TomlState a
forall i o. Codec i o -> i -> TomlState o
codecWrite TomlCodec a
codec a
d
}
{-# INLINE dimatch #-}
infixl 5 .=
(.=) :: Codec field a -> (object -> field) -> Codec object a
Codec field a
codec .= :: forall field a object.
Codec field a -> (object -> field) -> Codec object a
.= object -> field
getter = Codec field a
codec { codecWrite = codecWrite codec . getter }
{-# INLINE (.=) #-}