summaryrefslogtreecommitdiff
path: root/src/Network/KRPC/Method.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/KRPC/Method.hs')
-rw-r--r--src/Network/KRPC/Method.hs61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/Network/KRPC/Method.hs b/src/Network/KRPC/Method.hs
new file mode 100644
index 00000000..54aa8ef0
--- /dev/null
+++ b/src/Network/KRPC/Method.hs
@@ -0,0 +1,61 @@
1{-# LANGUAGE RankNTypes #-}
2{-# LANGUAGE MultiParamTypeClasses #-}
3{-# LANGUAGE FunctionalDependencies #-}
4{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5{-# LANGUAGE ScopedTypeVariables #-}
6module Network.KRPC.Method
7 ( Method (..)
8 , KRPC (..)
9 ) where
10
11import Data.BEncode (BEncode)
12import Data.Monoid
13import Data.String
14import Data.Typeable
15import Network.KRPC.Message
16
17
18-- | Method datatype used to describe name, parameters and return
19-- values of procedure. Client use a method to /invoke/, server
20-- /implements/ the method to make the actual work.
21--
22-- We use the following fantom types to ensure type-safiety:
23--
24-- * param: Type of method parameters. Ordinary Tuple type used
25-- to specify more than one parameter, so for example @Method
26-- (Int, Int) result@ will take two arguments.
27--
28-- * result: Type of return value of the method. Similarly,
29-- tuple used to specify more than one return value, so for
30-- exsample @Method (Foo, Bar) (Bar, Foo)@ will take two arguments
31-- and return two values.
32--
33newtype Method param result = Method MethodName
34 deriving (Eq, Ord, IsString, BEncode)
35
36instance (Typeable a, Typeable b) => Show (Method a b) where
37 showsPrec _ = showsMethod
38
39showsMethod :: forall a. forall b. Typeable a => Typeable b
40 => Method a b -> ShowS
41showsMethod (Method name) =
42 shows name <>
43 showString " :: " <>
44 shows paramsTy <>
45 showString " -> " <>
46 shows valuesTy
47 where
48 impossible = error "KRPC.showsMethod: impossible"
49 paramsTy = typeOf (impossible :: a)
50 valuesTy = typeOf (impossible :: b)
51
52-- | Example:
53-- @
54-- data Ping = Ping Text deriving BEncode
55-- data Pong = Pong Text deriving BEncode
56--
57-- instance KRPC Ping Pong where
58-- method = "ping"
59-- @
60class (BEncode req, BEncode resp) => KRPC req resp | req -> resp where
61 method :: Method req resp