-
Notifications
You must be signed in to change notification settings - Fork 0
/
Config.hs
45 lines (37 loc) · 1.11 KB
/
Config.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
module Config where
import Text.ParserCombinators.Parsec (ParseError, Parser, string, many, noneOf)
import Text.Parsec.String (parseFromFile)
import Text.Parsec.Char (endOfLine)
import Control.Applicative ((*>), (<*))
data Config = Config {
server :: String
, port :: Int
, nick :: String
, channels :: [String]
} deriving (Show)
defaultConfig :: Config
defaultConfig = Config {
server = "irc.freenode.net"
, port = 6667
, nick = "HaskellHawk-defualt"
, channels = ["#test"]
}
content :: Parser String
content = many (noneOf "\r\n")
field :: String -> Parser String
field f = string f *> string " = " *> content <* endOfLine
-- TODO: Make it so the fields don't have to be in order.
configParser :: Parser Config
configParser = do
server' <- field "server"
port' <- field "port"
nick' <- field "nick"
channels' <- field "channels"
return Config {
server = server'
, port = read port'
, nick = nick'
, channels = [channels']
}
readConfig :: String -> IO (Either ParseError Config)
readConfig = parseFromFile configParser