Skip to content

Commit 1465187

Browse files
committed
Add Codec's Selective instance
1 parent 561aefd commit 1465187

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/Toml/Codec/Types.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ module Toml.Codec.Types
3535

3636
import Control.Applicative (Alternative (..), liftA2)
3737
import Control.Monad.State (MonadState (..))
38+
import Control.Selective (Selective (..))
3839
import Data.Bifunctor (first)
3940
import Validation (Validation (..))
4041

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

141+
-- | @since 1.3.4
142+
instance Selective (Codec i) where
143+
select branched onLeft = Codec
144+
{ codecRead = \o -> (`either` id) <$> codecRead onLeft o <*> codecRead branched o
145+
, codecWrite = \i -> (`either` id) <$> codecWrite onLeft i <*> codecWrite branched i
146+
}
147+
{-# INLINE select #-}
148+
140149
{- | Mutable context for TOML conversion.
141150
We are introducing our own implemetation of state with 'MonadState' instance due
142151
to some limitation in the design connected to the usage of State.

tomland.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ library
137137
, megaparsec >= 7.0.5 && < 9.3
138138
, mtl ^>= 2.2
139139
, parser-combinators >= 1.1.0 && < 1.4
140+
, selective ^>= 0.4.2
140141
, text >= 1.2 && < 2.1
141142
, time >= 1.8 && < 1.14
142143
, transformers >= 0.5 && < 0.7

0 commit comments

Comments
 (0)