diff options
Diffstat (limited to 'src/Remote/KRPC/Scheme.hs')
-rw-r--r-- | src/Remote/KRPC/Scheme.hs | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/Remote/KRPC/Scheme.hs b/src/Remote/KRPC/Scheme.hs new file mode 100644 index 00000000..84982649 --- /dev/null +++ b/src/Remote/KRPC/Scheme.hs | |||
@@ -0,0 +1,68 @@ | |||
1 | -- | | ||
2 | -- Copyright : (c) Sam T. 2013 | ||
3 | -- License : MIT | ||
4 | -- Maintainer : pxqr.sta@gmail.com | ||
5 | -- Stability : experimental | ||
6 | -- Portability : portable | ||
7 | -- | ||
8 | -- This module provides message scheme validation for core protocol | ||
9 | -- messages from 'Remote.KRPC.Procotol'. This module should be used | ||
10 | -- with 'Remote.KRPC.Protocol', otherwise (if you use 'Remote.KRPC') | ||
11 | -- this module seems to be useless. | ||
12 | -- | ||
13 | {-# LANGUAGE DefaultSignatures #-} | ||
14 | {-# LANGUAGE TypeSynonymInstances #-} | ||
15 | {-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies #-} | ||
16 | module Remote.KRPC.Scheme | ||
17 | ( KMessage(..) | ||
18 | , KQueryScheme(..) | ||
19 | , KResponseScheme(..) | ||
20 | ) where | ||
21 | |||
22 | import Data.Map as M | ||
23 | import Data.Set | ||
24 | |||
25 | import Remote.KRPC.Protocol | ||
26 | |||
27 | |||
28 | -- | Used to validate any message by its scheme | ||
29 | -- | ||
30 | -- forall m. m `validate` scheme m | ||
31 | -- | ||
32 | class KMessage message scheme | message -> scheme where | ||
33 | -- | Get a message scheme. | ||
34 | scheme :: message -> scheme | ||
35 | |||
36 | -- | Check a message with a scheme. | ||
37 | validate :: message -> scheme -> Bool | ||
38 | |||
39 | default validate :: Eq scheme => message -> scheme -> Bool | ||
40 | validate = (==) . scheme | ||
41 | {-# INLINE validate #-} | ||
42 | |||
43 | |||
44 | instance KMessage KError ErrorCode where | ||
45 | {-# SPECIALIZE instance KMessage KError ErrorCode #-} | ||
46 | scheme = errorCode | ||
47 | {-# INLINE scheme #-} | ||
48 | |||
49 | |||
50 | data KQueryScheme = KQueryScheme { | ||
51 | qscMethod :: MethodName | ||
52 | , qscParams :: Set ParamName | ||
53 | } deriving (Show, Read, Eq, Ord) | ||
54 | |||
55 | instance KMessage KQuery KQueryScheme where | ||
56 | {-# SPECIALIZE instance KMessage KQuery KQueryScheme #-} | ||
57 | scheme q = KQueryScheme (queryMethod q) (M.keysSet (queryArgs q)) | ||
58 | {-# INLINE scheme #-} | ||
59 | |||
60 | |||
61 | newtype KResponseScheme = KResponseScheme { | ||
62 | rscVals :: Set ValName | ||
63 | } deriving (Show, Read, Eq, Ord) | ||
64 | |||
65 | instance KMessage KResponse KResponseScheme where | ||
66 | {-# SPECIALIZE instance KMessage KResponse KResponseScheme #-} | ||
67 | scheme = KResponseScheme . keysSet . respVals | ||
68 | {-# INLINE scheme #-} \ No newline at end of file | ||