Skip to content

Commit 62f3c4f

Browse files
authored
Merge pull request #1733 from danidiaz/danidiaz_add_mkhandler_pattern_synonym
Add `MkHandler` pattern synonym
2 parents 445474f + 956d135 commit 62f3c4f

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

changelog.d/1733

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
synopsis: Add `MkHandler` pattern synonym
2+
prs: #1733
3+
issues: #1732
4+
description: {
5+
Add a bidirectional pattern synonym to construct `Handler a` values from `IO
6+
(Either ServerError a)` ones, and match in the other direction.
7+
}

servant-server/src/Servant/Server.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
{-# LANGUAGE ScopedTypeVariables #-}
66
{-# LANGUAGE TypeFamilies #-}
77
{-# LANGUAGE TypeOperators #-}
8+
{-# LANGUAGE PatternSynonyms #-}
89

910
-- | This module lets you implement 'Server's for defined APIs. You'll
1011
-- most likely just need 'serve'.
@@ -25,6 +26,7 @@ module Servant.Server
2526
, emptyServer
2627
, Handler (..)
2728
, runHandler
29+
, pattern MkHandler
2830

2931
-- * Debugging the server layout
3032
, layout

servant-server/src/Servant/Server/Internal/Handler.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
44
{-# LANGUAGE MultiParamTypeClasses #-}
55
{-# LANGUAGE TypeFamilies #-}
6+
{-# LANGUAGE PatternSynonyms #-}
67
module Servant.Server.Internal.Handler where
78

89
import Prelude ()
@@ -19,7 +20,7 @@ import Control.Monad.IO.Class
1920
import Control.Monad.Trans.Control
2021
(MonadBaseControl (..))
2122
import Control.Monad.Trans.Except
22-
(ExceptT, runExceptT)
23+
(ExceptT(ExceptT), runExceptT)
2324
import Data.String
2425
(fromString)
2526
import GHC.Generics
@@ -51,3 +52,10 @@ instance MonadBaseControl IO Handler where
5152

5253
runHandler :: Handler a -> IO (Either ServerError a)
5354
runHandler = runExceptT . runHandler'
55+
56+
-- | Pattern synonym that matches directly on the inner 'IO' action.
57+
--
58+
-- To lift 'IO' actions that don't carry a 'ServerError', use 'Control.Monad.IO.Class.liftIO' instead.
59+
pattern MkHandler :: IO (Either ServerError a) -> Handler a
60+
pattern MkHandler ioe = Handler (ExceptT ioe)
61+
{-# COMPLETE MkHandler #-}

0 commit comments

Comments
 (0)