]> git.rkrishnan.org Git - yorgey.git/blob - hw2/Log.hs
starting materials for hw2
[yorgey.git] / hw2 / Log.hs
1 -- CIS 194 Homework 2
2
3 module Log where
4
5 import Control.Applicative
6
7 data MessageType = Info
8                  | Warning
9                  | Error Int
10   deriving (Show, Eq)
11
12 type TimeStamp = Int
13
14 data LogMessage = LogMessage MessageType TimeStamp String
15                 | Unknown String
16   deriving (Show, Eq)
17
18 data MessageTree = Leaf
19                  | Node MessageTree LogMessage MessageTree
20   deriving (Show, Eq)
21
22 -- | @testParse p n f@ tests the log file parser @p@ by running it
23 --   on the first @n@ lines of file @f@.
24 testParse :: (String -> [LogMessage])
25           -> Int
26           -> FilePath
27           -> IO [LogMessage]
28 testParse parse n file = take n . parse <$> readFile file
29
30 -- | @testWhatWentWrong p w f@ tests the log file parser @p@ and
31 --   warning message extractor @w@ by running them on the log file
32 --   @f@.
33 testWhatWentWrong :: (String -> [LogMessage])
34                   -> ([LogMessage] -> [String])
35                   -> FilePath
36                   -> IO [String]
37 testWhatWentWrong parse whatWentWrong file
38   = whatWentWrong . parse <$> readFile file