-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Model.hs
59 lines (47 loc) 路 1.39 KB
/
Model.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
{- |
Copyright: (c) 2022 Dmitrii Kovanikov
SPDX-License-Identifier: MPL-2.0
Maintainer: Dmitrii Kovanikov <kovanikov@gmail.com>
Data types to model the domain of the @cabal@ output.
-}
module DrCabal.Model
( Line (..)
, Status (..)
, Entry (..)
) where
import Data.Aeson (FromJSON (..), ToJSON (..), object, withObject, withText, (.:), (.=))
data Line = Line
{ lineTime :: Word64
, lineLine :: ByteString
} deriving stock (Show)
data Status
= Downloading
| Downloaded
| Starting
| Building
| Installing
| Completed
deriving stock (Show, Read, Eq, Ord, Enum, Bounded)
instance ToJSON Status where
toJSON = toJSON @Text . show
instance FromJSON Status where
parseJSON = withText "Status" $ \(toString -> t) -> case readMaybe t of
Nothing -> fail $ "Unexpected status: " <> t
Just status -> pure status
data Entry = Entry
{ entryStatus :: Status
, entryStart :: Word64
, entryLibrary :: Text
} deriving stock (Show)
instance ToJSON Entry where
toJSON Entry{..} = object
[ "status" .= entryStatus
, "startTime" .= entryStart
, "library" .= entryLibrary
]
instance FromJSON Entry where
parseJSON = withObject "Entry" $ \o -> do
entryStatus <- o .: "status"
entryStart <- o .: "startTime"
entryLibrary <- o .: "library"
pure Entry{..}