module TD.Data.FoundPosition
  (FoundPosition(..)) where

import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I

data FoundPosition
  = FoundPosition -- ^ Contains 0-based match position
    { FoundPosition -> Maybe Int
position :: Maybe Int -- ^ The position of the match
    }
  deriving (FoundPosition -> FoundPosition -> Bool
(FoundPosition -> FoundPosition -> Bool)
-> (FoundPosition -> FoundPosition -> Bool) -> Eq FoundPosition
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: FoundPosition -> FoundPosition -> Bool
== :: FoundPosition -> FoundPosition -> Bool
$c/= :: FoundPosition -> FoundPosition -> Bool
/= :: FoundPosition -> FoundPosition -> Bool
Eq, Int -> FoundPosition -> ShowS
[FoundPosition] -> ShowS
FoundPosition -> String
(Int -> FoundPosition -> ShowS)
-> (FoundPosition -> String)
-> ([FoundPosition] -> ShowS)
-> Show FoundPosition
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> FoundPosition -> ShowS
showsPrec :: Int -> FoundPosition -> ShowS
$cshow :: FoundPosition -> String
show :: FoundPosition -> String
$cshowList :: [FoundPosition] -> ShowS
showList :: [FoundPosition] -> ShowS
Show)

instance I.ShortShow FoundPosition where
  shortShow :: FoundPosition -> String
shortShow FoundPosition
    { position :: FoundPosition -> Maybe Int
position = Maybe Int
position_
    }
      = String
"FoundPosition"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"position" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
position_
        ]

instance AT.FromJSON FoundPosition where
  parseJSON :: Value -> Parser FoundPosition
parseJSON v :: Value
v@(AT.Object Object
obj) = do
    String
t <- Object
obj Object -> Key -> Parser String
forall a. FromJSON a => Object -> Key -> Parser a
A..: Key
"@type" :: AT.Parser String

    case String
t of
      String
"foundPosition" -> Value -> Parser FoundPosition
parseFoundPosition Value
v
      String
_               -> Parser FoundPosition
forall a. Monoid a => a
mempty
    
    where
      parseFoundPosition :: A.Value -> AT.Parser FoundPosition
      parseFoundPosition :: Value -> Parser FoundPosition
parseFoundPosition = String
-> (Object -> Parser FoundPosition)
-> Value
-> Parser FoundPosition
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"FoundPosition" ((Object -> Parser FoundPosition) -> Value -> Parser FoundPosition)
-> (Object -> Parser FoundPosition)
-> Value
-> Parser FoundPosition
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
position_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"position"
        FoundPosition -> Parser FoundPosition
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (FoundPosition -> Parser FoundPosition)
-> FoundPosition -> Parser FoundPosition
forall a b. (a -> b) -> a -> b
$ FoundPosition
          { position :: Maybe Int
position = Maybe Int
position_
          }
  parseJSON Value
_ = Parser FoundPosition
forall a. Monoid a => a
mempty