Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Codec's Selective instance #415

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -137,6 +137,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 && < 2.1
, time >= 1.8 && < 1.14
, transformers >= 0.5 && < 0.7
Expand Down