summaryrefslogtreecommitdiffstats
path: root/Notmuch/SearchResult.hs
blob: 3dbf6bc10e7951a42bc44ad62a7d512fdcfb8983 (plain)
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
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
module Notmuch.SearchResult where

import Control.Applicative
import Data.Aeson
import Data.Text
import Data.Time.Clock
import Data.Time.Clock.POSIX


newtype ThreadID = ThreadID String
  deriving (Show,Read,Eq,FromJSON,ToJSON)


-- | A single entry returned from the notmuch search command.
data SearchResult = SearchResult {
      searchThread :: ThreadID
    , searchTime :: UTCTime
    , searchDateRel :: Text
    , searchSubject :: Text
    , searchAuthors :: Text
    , searchQuery :: [Maybe Text] -- TODO (Text, Maybe Text)
    , searchTags :: [Text]
    , searchMatched :: Int
    , searchTotal :: Int
    }
  deriving (Show)


instance Eq SearchResult where
    s1 == s2 =
        searchThread s1 == searchThread s2


instance FromJSON SearchResult where
    parseJSON (Object v) = SearchResult <$> v .: "thread"
                                        <*> (posixSecondsToUTCTime . fromInteger <$> v .: "timestamp")
                                        <*> v .: "date_relative"
                                        <*> v .:? "subject" .!= ""
                                        <*> v .:? "authors" .!= ""
                                        <*> v .:? "query" .!= []
                                        <*> v .: "tags"
                                        <*> v .: "matched"
                                        <*> v .: "total"
    parseJSON x = fail $ "Error parsing search: " ++ show x

--instance ToJSON SearchResult where
--    toJSON s = object [ "thread" .= searchThread s
--                      , "time" .= searchTime s
--                      , "date_relative" .= searchDateRel s
--                      , "subject" .= searchSubject s
--                      , "authors" .= searchAuthors s
--                      , "tags" .= searchTags s
--                      , "matched" .= searchMatched s
--                      , "total" .= searchTotal s
--                      ]