Skip to content

Commit 7df0835

Browse files
committed
Add Codec's Selective instance
1 parent fd0d021 commit 7df0835

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
@@ -133,6 +133,7 @@ library
133133
, megaparsec >= 7.0.5 && < 9.3
134134
, mtl ^>= 2.2
135135
, parser-combinators >= 1.1.0 && < 1.4
136+
, selective ^>= 0.4.2
136137
, text ^>= 1.2
137138
, time >= 1.8 && < 1.14
138139
, transformers >= 0.5 && < 0.7

0 commit comments

Comments
 (0)