{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}

module Web.Bugzilla.RedHat.Search
(
  -- * Search operators
  (.==.)
, (./=.)
, (.<.)
, (.<=.)
, (.>.)
, (.>=.)
, (.=~.)
, (./=~.)
, equalsAny
, contains
, containsCase
, containsAny
, containsAll
, changedBefore
, changedAfter
, changedFrom
, changedTo
, changedBy
, contentMatches
, isEmpty
, (.&&.)
, (.||.)
, not'

  -- * Search expressions
, Field (..)
, SearchExpression
, evalSearchExpr
) where

import qualified Data.Text as T
import Data.Time.Clock (UTCTime(..))

import Web.Bugzilla.RedHat.Internal.Search
import Web.Bugzilla.RedHat.Internal.Types

(.==.) :: FieldType a => Field a -> a -> SearchExpression
.==. :: Field a -> a -> SearchExpression
(.==.) = (SearchTerm -> SearchExpression
Term (SearchTerm -> SearchExpression)
-> (a -> SearchTerm) -> a -> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((a -> SearchTerm) -> a -> SearchExpression)
-> (Field a -> a -> SearchTerm) -> Field a -> a -> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Field a -> a -> SearchTerm
forall a b.
(FieldType a, FieldType b) =>
Text -> Field a -> b -> SearchTerm
BinaryOp "equals"
infix 4 .==.

(./=.) :: FieldType a => Field a -> a -> SearchExpression
./=. :: Field a -> a -> SearchExpression
(./=.) = (SearchTerm -> SearchExpression
Term (SearchTerm -> SearchExpression)
-> (a -> SearchTerm) -> a -> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((a -> SearchTerm) -> a -> SearchExpression)
-> (Field a -> a -> SearchTerm) -> Field a -> a -> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Field a -> a -> SearchTerm
forall a b.
(FieldType a, FieldType b) =>
Text -> Field a -> b -> SearchTerm
BinaryOp "notequals"
infix 4 ./=.

(.<.) :: FieldType a => Field a -> a -> SearchExpression
.<. :: Field a -> a -> SearchExpression
(.<.) = (SearchTerm -> SearchExpression
Term (SearchTerm -> SearchExpression)
-> (a -> SearchTerm) -> a -> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((a -> SearchTerm) -> a -> SearchExpression)
-> (Field a -> a -> SearchTerm) -> Field a -> a -> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Field a -> a -> SearchTerm
forall a b.
(FieldType a, FieldType b) =>
Text -> Field a -> b -> SearchTerm
BinaryOp "lessthan"
infix 4 .<.

(.<=.) :: FieldType a => Field a -> a -> SearchExpression
.<=. :: Field a -> a -> SearchExpression
(.<=.) = (SearchTerm -> SearchExpression
Term (SearchTerm -> SearchExpression)
-> (a -> SearchTerm) -> a -> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((a -> SearchTerm) -> a -> SearchExpression)
-> (Field a -> a -> SearchTerm) -> Field a -> a -> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Field a -> a -> SearchTerm
forall a b.
(FieldType a, FieldType b) =>
Text -> Field a -> b -> SearchTerm
BinaryOp "lessthaneq"
infix 4 .<=.

(.>.) :: FieldType a => Field a -> a -> SearchExpression
.>. :: Field a -> a -> SearchExpression
(.>.) = (SearchTerm -> SearchExpression
Term (SearchTerm -> SearchExpression)
-> (a -> SearchTerm) -> a -> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((a -> SearchTerm) -> a -> SearchExpression)
-> (Field a -> a -> SearchTerm) -> Field a -> a -> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Field a -> a -> SearchTerm
forall a b.
(FieldType a, FieldType b) =>
Text -> Field a -> b -> SearchTerm
BinaryOp "greaterthan"
infix 4 .>.

(.>=.) :: FieldType a => Field a -> a -> SearchExpression
.>=. :: Field a -> a -> SearchExpression
(.>=.) = (SearchTerm -> SearchExpression
Term (SearchTerm -> SearchExpression)
-> (a -> SearchTerm) -> a -> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((a -> SearchTerm) -> a -> SearchExpression)
-> (Field a -> a -> SearchTerm) -> Field a -> a -> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Field a -> a -> SearchTerm
forall a b.
(FieldType a, FieldType b) =>
Text -> Field a -> b -> SearchTerm
BinaryOp "greaterthaneq"
infix 4 .>=.

(.=~.) :: FieldType a => Field a -> a -> SearchExpression
.=~. :: Field a -> a -> SearchExpression
(.=~.) = (SearchTerm -> SearchExpression
Term (SearchTerm -> SearchExpression)
-> (a -> SearchTerm) -> a -> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((a -> SearchTerm) -> a -> SearchExpression)
-> (Field a -> a -> SearchTerm) -> Field a -> a -> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Field a -> a -> SearchTerm
forall a b.
(FieldType a, FieldType b) =>
Text -> Field a -> b -> SearchTerm
BinaryOp "regexp"

(./=~.) :: FieldType a => Field a -> a -> SearchExpression
./=~. :: Field a -> a -> SearchExpression
(./=~.) = (SearchTerm -> SearchExpression
Term (SearchTerm -> SearchExpression)
-> (a -> SearchTerm) -> a -> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((a -> SearchTerm) -> a -> SearchExpression)
-> (Field a -> a -> SearchTerm) -> Field a -> a -> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Field a -> a -> SearchTerm
forall a b.
(FieldType a, FieldType b) =>
Text -> Field a -> b -> SearchTerm
BinaryOp "notregexp"

equalsAny :: FieldType a => Field a -> [a] -> SearchExpression
equalsAny :: Field a -> [a] -> SearchExpression
equalsAny = (SearchTerm -> SearchExpression
Term (SearchTerm -> SearchExpression)
-> ([a] -> SearchTerm) -> [a] -> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) (([a] -> SearchTerm) -> [a] -> SearchExpression)
-> (Field a -> [a] -> SearchTerm)
-> Field a
-> [a]
-> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Field a -> [a] -> SearchTerm
forall a b.
(FieldType a, FieldType b) =>
Text -> Field a -> b -> SearchTerm
BinaryOp "anyexact"

contains :: Field T.Text -> T.Text -> SearchExpression
contains :: Field Text -> Text -> SearchExpression
contains = (SearchTerm -> SearchExpression
Term (SearchTerm -> SearchExpression)
-> (Text -> SearchTerm) -> Text -> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((Text -> SearchTerm) -> Text -> SearchExpression)
-> (Field Text -> Text -> SearchTerm)
-> Field Text
-> Text
-> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Field Text -> Text -> SearchTerm
forall a b.
(FieldType a, FieldType b) =>
Text -> Field a -> b -> SearchTerm
BinaryOp "substring"

containsCase :: Field T.Text -> T.Text -> SearchExpression
containsCase :: Field Text -> Text -> SearchExpression
containsCase = (SearchTerm -> SearchExpression
Term (SearchTerm -> SearchExpression)
-> (Text -> SearchTerm) -> Text -> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((Text -> SearchTerm) -> Text -> SearchExpression)
-> (Field Text -> Text -> SearchTerm)
-> Field Text
-> Text
-> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Field Text -> Text -> SearchTerm
forall a b.
(FieldType a, FieldType b) =>
Text -> Field a -> b -> SearchTerm
BinaryOp "casesubstring"

containsAny :: Field T.Text -> [T.Text] -> SearchExpression
containsAny :: Field Text -> [Text] -> SearchExpression
containsAny = (SearchTerm -> SearchExpression
Term (SearchTerm -> SearchExpression)
-> ([Text] -> SearchTerm) -> [Text] -> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) (([Text] -> SearchTerm) -> [Text] -> SearchExpression)
-> (Field Text -> [Text] -> SearchTerm)
-> Field Text
-> [Text]
-> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Field Text -> [Text] -> SearchTerm
forall a b.
(FieldType a, FieldType b) =>
Text -> Field a -> b -> SearchTerm
BinaryOp "anywordssubstr"

containsAll :: Field T.Text -> [T.Text] -> SearchExpression
containsAll :: Field Text -> [Text] -> SearchExpression
containsAll = (SearchTerm -> SearchExpression
Term (SearchTerm -> SearchExpression)
-> ([Text] -> SearchTerm) -> [Text] -> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) (([Text] -> SearchTerm) -> [Text] -> SearchExpression)
-> (Field Text -> [Text] -> SearchTerm)
-> Field Text
-> [Text]
-> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Field Text -> [Text] -> SearchTerm
forall a b.
(FieldType a, FieldType b) =>
Text -> Field a -> b -> SearchTerm
BinaryOp "allwordssubstr"

changedBefore :: FieldType a => Field a -> UTCTime -> SearchExpression
changedBefore :: Field a -> UTCTime -> SearchExpression
changedBefore = (SearchTerm -> SearchExpression
Term (SearchTerm -> SearchExpression)
-> (UTCTime -> SearchTerm) -> UTCTime -> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((UTCTime -> SearchTerm) -> UTCTime -> SearchExpression)
-> (Field a -> UTCTime -> SearchTerm)
-> Field a
-> UTCTime
-> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Field a -> UTCTime -> SearchTerm
forall a b.
(FieldType a, FieldType b) =>
Text -> Field a -> b -> SearchTerm
BinaryOp "changedbefore"

changedAfter :: FieldType a => Field a -> UTCTime -> SearchExpression
changedAfter :: Field a -> UTCTime -> SearchExpression
changedAfter = (SearchTerm -> SearchExpression
Term (SearchTerm -> SearchExpression)
-> (UTCTime -> SearchTerm) -> UTCTime -> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((UTCTime -> SearchTerm) -> UTCTime -> SearchExpression)
-> (Field a -> UTCTime -> SearchTerm)
-> Field a
-> UTCTime
-> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Field a -> UTCTime -> SearchTerm
forall a b.
(FieldType a, FieldType b) =>
Text -> Field a -> b -> SearchTerm
BinaryOp "changedafter"

changedFrom :: FieldType a => Field a -> a -> SearchExpression
changedFrom :: Field a -> a -> SearchExpression
changedFrom = (SearchTerm -> SearchExpression
Term (SearchTerm -> SearchExpression)
-> (a -> SearchTerm) -> a -> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((a -> SearchTerm) -> a -> SearchExpression)
-> (Field a -> a -> SearchTerm) -> Field a -> a -> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Field a -> a -> SearchTerm
forall a b.
(FieldType a, FieldType b) =>
Text -> Field a -> b -> SearchTerm
BinaryOp "changedfrom"

changedTo :: FieldType a => Field a -> a -> SearchExpression
changedTo :: Field a -> a -> SearchExpression
changedTo = (SearchTerm -> SearchExpression
Term (SearchTerm -> SearchExpression)
-> (a -> SearchTerm) -> a -> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((a -> SearchTerm) -> a -> SearchExpression)
-> (Field a -> a -> SearchTerm) -> Field a -> a -> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Field a -> a -> SearchTerm
forall a b.
(FieldType a, FieldType b) =>
Text -> Field a -> b -> SearchTerm
BinaryOp "changedto"

changedBy :: FieldType a => Field a -> UserEmail -> SearchExpression
changedBy :: Field a -> Text -> SearchExpression
changedBy = (SearchTerm -> SearchExpression
Term (SearchTerm -> SearchExpression)
-> (Text -> SearchTerm) -> Text -> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((Text -> SearchTerm) -> Text -> SearchExpression)
-> (Field a -> Text -> SearchTerm)
-> Field a
-> Text
-> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Field a -> Text -> SearchTerm
forall a b.
(FieldType a, FieldType b) =>
Text -> Field a -> b -> SearchTerm
BinaryOp "changedby"

contentMatches :: T.Text -> SearchExpression
contentMatches :: Text -> SearchExpression
contentMatches = SearchTerm -> SearchExpression
Term (SearchTerm -> SearchExpression)
-> (Text -> SearchTerm) -> Text -> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Field Text -> Text -> SearchTerm
forall a b.
(FieldType a, FieldType b) =>
Text -> Field a -> b -> SearchTerm
BinaryOp "matches" Field Text
ContentField

isEmpty :: FieldType a => Field a -> SearchExpression
isEmpty :: Field a -> SearchExpression
isEmpty = SearchTerm -> SearchExpression
Term (SearchTerm -> SearchExpression)
-> (Field a -> SearchTerm) -> Field a -> SearchExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Field a -> SearchTerm
forall a. FieldType a => Text -> Field a -> SearchTerm
UnaryOp "isempty"

(.&&.) :: SearchExpression -> SearchExpression -> SearchExpression
.&&. :: SearchExpression -> SearchExpression -> SearchExpression
(.&&.) a :: SearchExpression
a (And as :: [SearchExpression]
as) = [SearchExpression] -> SearchExpression
And (SearchExpression
aSearchExpression -> [SearchExpression] -> [SearchExpression]
forall a. a -> [a] -> [a]
:[SearchExpression]
as)
(.&&.) a :: SearchExpression
a b :: SearchExpression
b        = [SearchExpression] -> SearchExpression
And [SearchExpression
a, SearchExpression
b]
infixr 3 .&&.

(.||.) :: SearchExpression -> SearchExpression -> SearchExpression
.||. :: SearchExpression -> SearchExpression -> SearchExpression
(.||.) a :: SearchExpression
a (Or as :: [SearchExpression]
as) = [SearchExpression] -> SearchExpression
Or (SearchExpression
aSearchExpression -> [SearchExpression] -> [SearchExpression]
forall a. a -> [a] -> [a]
:[SearchExpression]
as)
(.||.) a :: SearchExpression
a b :: SearchExpression
b       = [SearchExpression] -> SearchExpression
Or [SearchExpression
a, SearchExpression
b]
infixr 2 .||.

not' :: SearchExpression -> SearchExpression
not' :: SearchExpression -> SearchExpression
not' (Not a :: SearchExpression
a) = SearchExpression
a
not' a :: SearchExpression
a       = SearchExpression -> SearchExpression
Not SearchExpression
a