-- |
--
-- Copyright:
--   This file is part of the package themoviedb.  It is subject to
--   the license terms in the LICENSE file found in the top-level
--   directory of this distribution and at:
--
--     https://github.com/pjones/themoviedb
--
--   No part of this package, including this file, may be copied,
--   modified, propagated, or distributed except according to the terms
--   contained in the LICENSE file.
--
-- License: MIT
--
-- Utility type for processing movie search results.
module Network.API.TheMovieDB.Internal.SearchResults
  ( SearchResults (..),
  )
where

import Data.Aeson

-- | Internal wrapper to parse a list of results from JSON.
newtype SearchResults a = SearchResults {SearchResults a -> [a]
searchResults :: [a]}
  deriving (SearchResults a -> SearchResults a -> Bool
(SearchResults a -> SearchResults a -> Bool)
-> (SearchResults a -> SearchResults a -> Bool)
-> Eq (SearchResults a)
forall a. Eq a => SearchResults a -> SearchResults a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SearchResults a -> SearchResults a -> Bool
$c/= :: forall a. Eq a => SearchResults a -> SearchResults a -> Bool
== :: SearchResults a -> SearchResults a -> Bool
$c== :: forall a. Eq a => SearchResults a -> SearchResults a -> Bool
Eq, Int -> SearchResults a -> ShowS
[SearchResults a] -> ShowS
SearchResults a -> String
(Int -> SearchResults a -> ShowS)
-> (SearchResults a -> String)
-> ([SearchResults a] -> ShowS)
-> Show (SearchResults a)
forall a. Show a => Int -> SearchResults a -> ShowS
forall a. Show a => [SearchResults a] -> ShowS
forall a. Show a => SearchResults a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SearchResults a] -> ShowS
$cshowList :: forall a. Show a => [SearchResults a] -> ShowS
show :: SearchResults a -> String
$cshow :: forall a. Show a => SearchResults a -> String
showsPrec :: Int -> SearchResults a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> SearchResults a -> ShowS
Show)

instance (FromJSON a) => FromJSON (SearchResults a) where
  parseJSON :: Value -> Parser (SearchResults a)
parseJSON = String
-> (Object -> Parser (SearchResults a))
-> Value
-> Parser (SearchResults a)
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"Search Results" ((Object -> Parser (SearchResults a))
 -> Value -> Parser (SearchResults a))
-> (Object -> Parser (SearchResults a))
-> Value
-> Parser (SearchResults a)
forall a b. (a -> b) -> a -> b
$ \Object
v ->
    [a] -> SearchResults a
forall a. [a] -> SearchResults a
SearchResults
      ([a] -> SearchResults a) -> Parser [a] -> Parser (SearchResults a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v Object -> Text -> Parser [a]
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"results"