module HsKu.JSON where

import HsKu
import Data.Aeson
import Data.Text
import Data.ByteString.Lazy as LBS
import GHC.Generics

newtype HaikuResult = HaikuResult
  { HaikuResult -> Maybe Haiku
result :: Maybe Haiku
  } deriving (forall x. HaikuResult -> Rep HaikuResult x)
-> (forall x. Rep HaikuResult x -> HaikuResult)
-> Generic HaikuResult
forall x. Rep HaikuResult x -> HaikuResult
forall x. HaikuResult -> Rep HaikuResult x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. HaikuResult -> Rep HaikuResult x
from :: forall x. HaikuResult -> Rep HaikuResult x
$cto :: forall x. Rep HaikuResult x -> HaikuResult
to :: forall x. Rep HaikuResult x -> HaikuResult
Generic

instance ToJSON HaikuResult

haikuToJSON :: Languages -> Text -> LBS.ByteString
haikuToJSON :: Languages -> Text -> ByteString
haikuToJSON Languages
langs Text
input =
  let res :: Maybe (Language, Haiku)
res = Languages -> Text -> Maybe (Language, Haiku)
parseHaiku Languages
langs Text
input
  in  HaikuResult -> ByteString
forall a. ToJSON a => a -> ByteString
encode (HaikuResult -> ByteString) -> HaikuResult -> ByteString
forall a b. (a -> b) -> a -> b
$ Maybe Haiku -> HaikuResult
HaikuResult ((Language, Haiku) -> Haiku
forall a b. (a, b) -> b
snd ((Language, Haiku) -> Haiku)
-> Maybe (Language, Haiku) -> Maybe Haiku
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Language, Haiku)
res)