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.hs26
1 files changed, 24 insertions, 2 deletions
diff --git a/src/Network/KRPC/Method.hs b/src/Network/KRPC/Method.hs
index 916b38a8..2a791924 100644
--- a/src/Network/KRPC/Method.hs
+++ b/src/Network/KRPC/Method.hs
@@ -7,6 +7,7 @@
7-- 7--
8-- Normally, you don't need to import this module. 8-- Normally, you don't need to import this module.
9-- 9--
10{-# LANGUAGE CPP #-}
10{-# LANGUAGE RankNTypes #-} 11{-# LANGUAGE RankNTypes #-}
11{-# LANGUAGE MultiParamTypeClasses #-} 12{-# LANGUAGE MultiParamTypeClasses #-}
12{-# LANGUAGE GeneralizedNewtypeDeriving #-} 13{-# LANGUAGE GeneralizedNewtypeDeriving #-}
@@ -17,7 +18,11 @@ module Network.KRPC.Method
17 , KRPC (..) 18 , KRPC (..)
18 ) where 19 ) where
19 20
21#ifdef VERSION_bencoding
20import Data.BEncode (BEncode) 22import Data.BEncode (BEncode)
23#else
24import Data.Serialize
25#endif
21import Data.ByteString.Char8 as BC 26import Data.ByteString.Char8 as BC
22import Data.Char 27import Data.Char
23import Data.Monoid 28import Data.Monoid
@@ -38,7 +43,12 @@ import Network.KRPC.Message
38-- * result: Type of return value of the method. 43-- * result: Type of return value of the method.
39-- 44--
40newtype Method param result = Method { methodName :: MethodName } 45newtype Method param result = Method { methodName :: MethodName }
41 deriving (Eq, Ord, IsString, BEncode) 46 deriving ( Eq, Ord
47#ifdef VERSION_bencoding
48 , IsString
49 , BEncode
50#endif
51 )
42 52
43-- | Example: 53-- | Example:
44-- 54--
@@ -49,7 +59,11 @@ instance (Typeable a, Typeable b) => Show (Method a b) where
49 59
50showsMethod :: forall a b. ( Typeable a , Typeable b ) => Method a b -> ShowS 60showsMethod :: forall a b. ( Typeable a , Typeable b ) => Method a b -> ShowS
51showsMethod (Method name) = 61showsMethod (Method name) =
62#ifdef VERSION_bencoding
52 showString (BC.unpack name) <> 63 showString (BC.unpack name) <>
64#else
65 shows (show name) <>
66#endif
53 showString " :: " <> 67 showString " :: " <>
54 shows paramsTy <> 68 shows paramsTy <>
55 showString " -> " <> 69 showString " -> " <>
@@ -72,7 +86,13 @@ showsMethod (Method name) =
72-- method = \"ping\" 86-- method = \"ping\"
73-- @ 87-- @
74-- 88--
75class (Typeable req, BEncode req, Typeable resp, BEncode resp) 89class ( Typeable req, Typeable resp
90#ifdef VERSION_bencoding
91 , BEncode req, BEncode resp
92#else
93 , Serialize req, Serialize resp
94#endif
95 )
76 => KRPC req resp where 96 => KRPC req resp where
77 97
78 -- | Method name. Default implementation uses lowercased @req@ 98 -- | Method name. Default implementation uses lowercased @req@
@@ -80,8 +100,10 @@ class (Typeable req, BEncode req, Typeable resp, BEncode resp)
80 -- 100 --
81 method :: Method req resp 101 method :: Method req resp
82 102
103#ifdef VERSION_bencoding
83 -- TODO add underscores 104 -- TODO add underscores
84 default method :: Typeable req => Method req resp 105 default method :: Typeable req => Method req resp
85 method = Method $ fromString $ L.map toLower $ show $ typeOf hole 106 method = Method $ fromString $ L.map toLower $ show $ typeOf hole
86 where 107 where
87 hole = error "krpc.method: impossible" :: req 108 hole = error "krpc.method: impossible" :: req
109#endif