|
@@ -1,10 +1,22 @@
|
|
1
|
+{-|
|
|
2
|
+Module : Network.IRC.Client
|
|
3
|
+Description : Extensible configuration for the IRC bot.
|
|
4
|
+Copyright : (c) Abhinav Sarkar, 2014-2015
|
|
5
|
+License : Apache-2.0
|
|
6
|
+Maintainer : abhinav@abhinavsarkar.net
|
|
7
|
+Stability : experimental
|
|
8
|
+Portability : POSIX
|
|
9
|
+
|
|
10
|
+Extensible configuration for the IRC bot.
|
|
11
|
+-}
|
|
12
|
+
|
1
|
13
|
{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, ExistentialQuantification, GADTs #-}
|
2
|
14
|
|
3
|
15
|
module Network.IRC.Configuration
|
4
|
16
|
( Name
|
5
|
17
|
, Value (..)
|
6
|
|
- , Configuration
|
7
|
18
|
, Configurable (..)
|
|
19
|
+ , Configuration
|
8
|
20
|
, fromMap
|
9
|
21
|
, lookup
|
10
|
22
|
, require
|
|
@@ -16,8 +28,10 @@ import qualified ClassyPrelude as P
|
16
|
28
|
import ClassyPrelude hiding (lookup)
|
17
|
29
|
import Data.Maybe (fromJust)
|
18
|
30
|
|
|
31
|
+-- | Name of a configuration property.
|
19
|
32
|
type Name = Text
|
20
|
33
|
|
|
34
|
+-- | Typeclass for the types that can be used as values in 'Configuration'.
|
21
|
35
|
class Configurable a where
|
22
|
36
|
fromValue :: Value -> Maybe a
|
23
|
37
|
|
|
@@ -58,22 +72,31 @@ instance Configurable a => Configurable [a] where
|
58
|
72
|
fromValue = valueToList
|
59
|
73
|
toValue = listToValue
|
60
|
74
|
|
61
|
|
-data Value = String Text
|
62
|
|
- | Number Integer
|
63
|
|
- | Boolean Bool
|
64
|
|
- | List [Value]
|
|
75
|
+-- | Value of a configuration property.
|
|
76
|
+data Value = String Text -- ^ A text value.
|
|
77
|
+ | Number Integer -- ^ An integer value.
|
|
78
|
+ | Boolean Bool -- ^ A boolean value.
|
|
79
|
+ | List [Value] -- ^ A list of values as a value.
|
65
|
80
|
deriving (Eq, Show)
|
66
|
81
|
|
67
|
|
-newtype Configuration = Configuration { configMap :: Map Name Value } deriving (Show)
|
|
82
|
+-- | A configuration data which can be used to look up properties.
|
|
83
|
+newtype Configuration = Configuration { configMap :: Map Name Value } deriving (Show, Eq)
|
68
|
84
|
|
|
85
|
+-- | Creates a 'Configuration' from a map of 'Name's to 'Value's.
|
69
|
86
|
fromMap :: Map Name Value -> Configuration
|
70
|
87
|
fromMap = Configuration
|
71
|
88
|
|
|
89
|
+-- | Looks up a property in the 'Configuration' by the given 'Name'.
|
|
90
|
+-- Returns 'Nothing' if the property is not found or is of wrong type.
|
72
|
91
|
lookup :: (Configurable a) => Name -> Configuration -> Maybe a
|
73
|
92
|
lookup name Configuration {..} = join . map fromValue $ P.lookup name configMap
|
74
|
93
|
|
|
94
|
+-- | Looks up a property in the 'Configuration' by the given 'Name'.
|
|
95
|
+-- Fails with an error if the property is not found or is of wrong type.
|
75
|
96
|
require :: (Configurable a) => Name -> Configuration -> a
|
76
|
97
|
require n = fromJust . lookup n
|
77
|
98
|
|
|
99
|
+-- | Looks up a property in the 'Configuration' by the given 'Name'.
|
|
100
|
+-- Returns the given default if the property is not found or is of wrong type.
|
78
|
101
|
lookupDefault :: (Configurable a) => Name -> Configuration -> a -> a
|
79
|
102
|
lookupDefault n c v = fromMaybe v $ lookup n c
|