-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommon.hs
More file actions
64 lines (52 loc) · 1.29 KB
/
Common.hs
File metadata and controls
64 lines (52 loc) · 1.29 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
module Common (
toLazyBS,
getWord,
c2w,
w2c,
getCommand,
handleAny,
catchAny,
ignoreError,
always,
LBS.toStrict,
Password,
Address,
Port
) where
import Data.ByteString as BS
import Data.ByteString.Internal as BS
import qualified Data.ByteString.Lazy as LBS
import Data.ByteString.Builder
import Data.Word8
import Data.Attoparsec.ByteString as A
import Data.Conduit
import Control.Exception
type Password = ByteString
type Address = ByteString
type Port = Int
toLazyBS :: ByteString -> LBS.ByteString
toLazyBS = toLazyByteString . byteString
getWord :: Parser ByteString
getWord = do
let pred = \x -> isSpace x || x == c2w ';'
A.takeWhile pred
takeTill pred
getCommand :: Parser [ByteString]
getCommand = do
next <- peekWord8'
if next == c2w ';'
then anyWord8 >> return []
else do
skipWhile isSpace
w <- takeTill $ \x -> isSpace x || x == c2w ';'
if not $ BS.null w
then fmap (w:) getCommand
else return []
catchAny :: IO a -> (SomeException -> IO a) -> IO a
catchAny = catch
handleAny :: (SomeException -> IO a) -> IO a -> IO a
handleAny = handle
ignoreError :: IO () -> IO ()
ignoreError = handleAny $ const $ return ()
always :: IO () -> IO () -> IO ()
always e a = handleAny (const e) a