Skip to content

Commit

Permalink
Add Codec's Selective instance
Browse files Browse the repository at this point in the history
  • Loading branch information
blackheaven committed Oct 1, 2022
1 parent fd0d021 commit 7df0835
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Toml/Codec/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module Toml.Codec.Types

import Control.Applicative (Alternative (..), liftA2)
import Control.Monad.State (MonadState (..))
import Control.Selective (Selective (..))
import Data.Bifunctor (first)
import Validation (Validation (..))

Expand Down Expand Up @@ -137,6 +138,14 @@ infixl 3 <!>
f <!> g = \a -> f a <|> g a
{-# INLINE (<!>) #-}

-- | @since 1.3.4
instance Selective (Codec i) where
select branched onLeft = Codec
{ codecRead = \o -> (`either` id) <$> codecRead onLeft o <*> codecRead branched o
, codecWrite = \i -> (`either` id) <$> codecWrite onLeft i <*> codecWrite branched i
}
{-# INLINE select #-}

{- | Mutable context for TOML conversion.
We are introducing our own implemetation of state with 'MonadState' instance due
to some limitation in the design connected to the usage of State.
Expand Down
1 change: 1 addition & 0 deletions tomland.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ library
, megaparsec >= 7.0.5 && < 9.3
, mtl ^>= 2.2
, parser-combinators >= 1.1.0 && < 1.4
, selective ^>= 0.4.2
, text ^>= 1.2
, time >= 1.8 && < 1.14
, transformers >= 0.5 && < 0.7
Expand Down

0 comments on commit 7df0835

Please sign in to comment.