{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
module Data.Conduit.JsonRpc.Methods
( Method(..)
, MethodError(..)
, NamedMethod
, method
, Methods
, fromList
, lookup )
where
import Data.Aeson
import qualified Data.HashMap.Strict as M
import Data.Text (Text)
import Prelude hiding (lookup)
data Method m where
Method :: forall i m o. (FromJSON i, ToJSON o)
=> (i -> m (Either MethodError o)) -> Method m
data MethodError = MethodError !Int !Text
deriving (MethodError -> MethodError -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: MethodError -> MethodError -> Bool
$c/= :: MethodError -> MethodError -> Bool
== :: MethodError -> MethodError -> Bool
$c== :: MethodError -> MethodError -> Bool
Eq, Int -> MethodError -> ShowS
[MethodError] -> ShowS
MethodError -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [MethodError] -> ShowS
$cshowList :: [MethodError] -> ShowS
show :: MethodError -> String
$cshow :: MethodError -> String
showsPrec :: Int -> MethodError -> ShowS
$cshowsPrec :: Int -> MethodError -> ShowS
Show)
newtype NamedMethod m = NamedMethod { forall (m :: * -> *). NamedMethod m -> (Text, Method m)
unWrap :: (Text, Method m) }
method :: (FromJSON i, ToJSON o)
=> Text
-> (i -> m (Either MethodError o))
-> NamedMethod m
method :: forall i o (m :: * -> *).
(FromJSON i, ToJSON o) =>
Text -> (i -> m (Either MethodError o)) -> NamedMethod m
method Text
name i -> m (Either MethodError o)
f = forall (m :: * -> *). (Text, Method m) -> NamedMethod m
NamedMethod (Text
name, forall i (m :: * -> *) o.
(FromJSON i, ToJSON o) =>
(i -> m (Either MethodError o)) -> Method m
Method i -> m (Either MethodError o)
f)
newtype Methods m = Methods (M.HashMap Text (Method m))
fromList :: [NamedMethod m] -> Methods m
fromList :: forall (m :: * -> *). [NamedMethod m] -> Methods m
fromList = forall (m :: * -> *). HashMap Text (Method m) -> Methods m
Methods forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map forall (m :: * -> *). NamedMethod m -> (Text, Method m)
unWrap
lookup :: Methods m -> Text -> Maybe (Method m)
lookup :: forall (m :: * -> *). Methods m -> Text -> Maybe (Method m)
lookup (Methods HashMap Text (Method m)
m) Text
name = forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
M.lookup Text
name HashMap Text (Method m)
m