module Yesod.Core.Types.HandlerContents
  (
    HandlerContents (..)
  ) where

import Control.Exception (Exception)
import Data.Maybe
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import qualified Network.HTTP.Types as H
import qualified Network.Wai as W
import Yesod.Core.Types.ErrorResponse
import Yesod.Core.Types.TypedContent (ContentType, TypedContent (..), typedContentToSnippet)

data HandlerContents =
      HCContent !H.Status !TypedContent
    | HCError !ErrorResponse
    | HCSendFile !ContentType !FilePath !(Maybe W.FilePart)
    | HCRedirect !H.Status !T.Text
    | HCCreated !T.Text
    | HCWai !W.Response
    | HCWaiApp !W.Application

instance Show HandlerContents where
    show :: HandlerContents -> String
show (HCContent Status
status tc :: TypedContent
tc@(TypedContent ContentType
t Content
_))
      = [String] -> String
forall a. Monoid a => [a] -> a
mconcat [ String
"HCContent "
                , (Status, ContentType) -> String
forall a. Show a => a -> String
show (Status
status, ContentType
t)
                , String
" ("
                , String -> Maybe String -> String
forall a. a -> Maybe a -> a
fromMaybe String
"" (Maybe String -> String) -> Maybe String -> String
forall a b. (a -> b) -> a -> b
$ Text -> String
TL.unpack (Text -> String) -> Maybe Text -> Maybe String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TypedContent -> Int64 -> Maybe Text
typedContentToSnippet TypedContent
tc Int64
1000
                , String
")"
                ]
    show (HCError ErrorResponse
e) = String
"HCError " String -> ShowS
forall a. [a] -> [a] -> [a]
++ ErrorResponse -> String
forall a. Show a => a -> String
show ErrorResponse
e
    show (HCSendFile ContentType
ct String
fp Maybe FilePart
mfp) = String
"HCSendFile " String -> ShowS
forall a. [a] -> [a] -> [a]
++ (ContentType, String, Maybe FilePart) -> String
forall a. Show a => a -> String
show (ContentType
ct, String
fp, Maybe FilePart
mfp)
    show (HCRedirect Status
s Text
t) = String
"HCRedirect " String -> ShowS
forall a. [a] -> [a] -> [a]
++ (Status, Text) -> String
forall a. Show a => a -> String
show (Status
s, Text
t)
    show (HCCreated Text
t) = String
"HCCreated " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Text -> String
forall a. Show a => a -> String
show Text
t
    show (HCWai Response
_) = String
"HCWai"
    show (HCWaiApp Application
_) = String
"HCWaiApp"

instance Exception HandlerContents