diff options
author | Sam T <pxqr.sta@gmail.com> | 2013-05-12 04:47:45 +0400 |
---|---|---|
committer | Sam T <pxqr.sta@gmail.com> | 2013-05-12 04:47:45 +0400 |
commit | e188c26f9e6b548b5170fb86f1bd4beee1f84708 (patch) | |
tree | 530b33a499d94ab35371311031fda921350345f0 /src/Remote/KRPC/Method.hs | |
parent | fd62eb70fe87b471d29cb994a60ad88f58b33ca9 (diff) |
+ Multi param procedures.
Diffstat (limited to 'src/Remote/KRPC/Method.hs')
-rw-r--r-- | src/Remote/KRPC/Method.hs | 61 |
1 files changed, 35 insertions, 26 deletions
diff --git a/src/Remote/KRPC/Method.hs b/src/Remote/KRPC/Method.hs index 420ceacf..8aa6ddc9 100644 --- a/src/Remote/KRPC/Method.hs +++ b/src/Remote/KRPC/Method.hs | |||
@@ -6,6 +6,7 @@ | |||
6 | -- Portability : portable | 6 | -- Portability : portable |
7 | -- | 7 | -- |
8 | {-# LANGUAGE OverloadedStrings #-} | 8 | {-# LANGUAGE OverloadedStrings #-} |
9 | {-# LANGUAGE FlexibleInstances, UndecidableInstances #-} | ||
9 | module Remote.KRPC.Method | 10 | module Remote.KRPC.Method |
10 | ( Method(methodName, methodParams, methodVals) | 11 | ( Method(methodName, methodParams, methodVals) |
11 | , methodQueryScheme, methodRespScheme | 12 | , methodQueryScheme, methodRespScheme |
@@ -14,13 +15,17 @@ module Remote.KRPC.Method | |||
14 | , method | 15 | , method |
15 | 16 | ||
16 | -- * Predefined methods | 17 | -- * Predefined methods |
17 | , idM, composeM | 18 | , idM |
19 | |||
20 | -- * Internal | ||
21 | , Extractable(..) | ||
18 | ) where | 22 | ) where |
19 | 23 | ||
20 | import Prelude hiding ((.), id) | 24 | import Prelude hiding ((.), id) |
21 | import Control.Applicative | 25 | import Control.Applicative |
22 | import Control.Category | 26 | import Control.Category |
23 | import Control.Monad | 27 | import Control.Monad |
28 | import Data.BEncode | ||
24 | import Data.ByteString as B | 29 | import Data.ByteString as B |
25 | import Data.List as L | 30 | import Data.List as L |
26 | import Data.Set as S | 31 | import Data.Set as S |
@@ -38,7 +43,7 @@ import Remote.KRPC.Protocol | |||
38 | -- | 43 | -- |
39 | data Method param result = Method { | 44 | data Method param result = Method { |
40 | -- | Name used in query and | 45 | -- | Name used in query and |
41 | methodName :: [MethodName] | 46 | methodName :: MethodName |
42 | 47 | ||
43 | -- | Description of each parameter in /right to left/ order. | 48 | -- | Description of each parameter in /right to left/ order. |
44 | , methodParams :: [ParamName] | 49 | , methodParams :: [ParamName] |
@@ -46,17 +51,8 @@ data Method param result = Method { | |||
46 | -- | Description of each return value in /right to left/ order. | 51 | -- | Description of each return value in /right to left/ order. |
47 | , methodVals :: [ValName] | 52 | , methodVals :: [ValName] |
48 | } | 53 | } |
49 | |||
50 | instance Category Method where | ||
51 | {-# SPECIALIZE instance Category Method #-} | ||
52 | id = idM | ||
53 | {-# INLINE id #-} | ||
54 | |||
55 | (.) = composeM | ||
56 | {-# INLINE (.) #-} | ||
57 | |||
58 | methodQueryScheme :: Method a b -> KQueryScheme | 54 | methodQueryScheme :: Method a b -> KQueryScheme |
59 | methodQueryScheme = KQueryScheme <$> B.intercalate "." . methodName | 55 | methodQueryScheme = KQueryScheme <$> methodName |
60 | <*> S.fromList . methodParams | 56 | <*> S.fromList . methodParams |
61 | {-# INLINE methodQueryScheme #-} | 57 | {-# INLINE methodQueryScheme #-} |
62 | 58 | ||
@@ -75,19 +71,32 @@ idM :: Method a a | |||
75 | idM = method "id" ["x"] ["y"] | 71 | idM = method "id" ["x"] ["y"] |
76 | {-# INLINE idM #-} | 72 | {-# INLINE idM #-} |
77 | 73 | ||
78 | -- | Pipelining of two or more methods. | 74 | method :: MethodName -> [ParamName] -> [ValName] -> Method param result |
79 | -- | 75 | method = Method |
80 | -- NOTE: composed methods will work only with this implementation of | 76 | {-# INLINE method #-} |
81 | -- KRPC, so both server and client should use this implementation, | ||
82 | -- otherwise you more likely get the 'ProtocolError'. | ||
83 | -- | ||
84 | composeM :: Method b c -> Method a b -> Method a c | ||
85 | composeM g h = Method (methodName g ++ methodName h) | ||
86 | (methodParams h) | ||
87 | (methodVals g) | ||
88 | {-# INLINE composeM #-} | ||
89 | 77 | ||
90 | 78 | ||
91 | method :: MethodName -> [ParamName] -> [ValName] -> Method param result | 79 | |
92 | method name = Method [name] | 80 | class Extractable a where |
93 | {-# INLINE method #-} \ No newline at end of file | 81 | injector :: a -> [BEncode] |
82 | extractor :: [BEncode] -> Result a | ||
83 | |||
84 | instance (BEncodable a, BEncodable b) => Extractable (a, b) where | ||
85 | {- SPECIALIZE instance (BEncodable a, BEncodable b) => Extractable (a, b) -} | ||
86 | injector (a, b) = [toBEncode a, toBEncode b] | ||
87 | {-# INLINE injector #-} | ||
88 | |||
89 | extractor [a, b] = (,) <$> fromBEncode a <*> fromBEncode b | ||
90 | extractor _ = decodingError "unable to match pair" | ||
91 | {-# INLINE extractor #-} | ||
92 | {- | ||
93 | instance BEncodable a => Extractable a where | ||
94 | {-# SPECIALIZE instance BEncodable a => Extractable a #-} | ||
95 | |||
96 | injector x = [toBEncode x] | ||
97 | {-# INLINE injector #-} | ||
98 | |||
99 | extractor [x] = fromBEncode x | ||
100 | extractor _ = decodingError "unable to match single value" | ||
101 | {-# INLINE extractor #-} | ||
102 | -} \ No newline at end of file | ||