module Ribosome.Host.Data.Response where

import Data.MessagePack (Object)
import Exon (exon)

import Ribosome.Host.Data.Request (RequestId (RequestId))

data Response =
  Success Object
  |
  Error Text
  deriving stock (Response -> Response -> Bool
(Response -> Response -> Bool)
-> (Response -> Response -> Bool) -> Eq Response
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Response -> Response -> Bool
$c/= :: Response -> Response -> Bool
== :: Response -> Response -> Bool
$c== :: Response -> Response -> Bool
Eq, Int -> Response -> ShowS
[Response] -> ShowS
Response -> String
(Int -> Response -> ShowS)
-> (Response -> String) -> ([Response] -> ShowS) -> Show Response
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Response] -> ShowS
$cshowList :: [Response] -> ShowS
show :: Response -> String
$cshow :: Response -> String
showsPrec :: Int -> Response -> ShowS
$cshowsPrec :: Int -> Response -> ShowS
Show)

formatResponse :: Response -> Text
formatResponse :: Response -> Text
formatResponse = \case
  Success Object
o -> Object -> Text
forall b a. (Show a, IsString b) => a -> b
show Object
o
  Error Text
e -> [exon|error: #{e}|]

data TrackedResponse =
  TrackedResponse {
    TrackedResponse -> RequestId
id :: RequestId,
    TrackedResponse -> Response
payload :: Response
  }
  deriving stock (TrackedResponse -> TrackedResponse -> Bool
(TrackedResponse -> TrackedResponse -> Bool)
-> (TrackedResponse -> TrackedResponse -> Bool)
-> Eq TrackedResponse
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TrackedResponse -> TrackedResponse -> Bool
$c/= :: TrackedResponse -> TrackedResponse -> Bool
== :: TrackedResponse -> TrackedResponse -> Bool
$c== :: TrackedResponse -> TrackedResponse -> Bool
Eq, Int -> TrackedResponse -> ShowS
[TrackedResponse] -> ShowS
TrackedResponse -> String
(Int -> TrackedResponse -> ShowS)
-> (TrackedResponse -> String)
-> ([TrackedResponse] -> ShowS)
-> Show TrackedResponse
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TrackedResponse] -> ShowS
$cshowList :: [TrackedResponse] -> ShowS
show :: TrackedResponse -> String
$cshow :: TrackedResponse -> String
showsPrec :: Int -> TrackedResponse -> ShowS
$cshowsPrec :: Int -> TrackedResponse -> ShowS
Show)

formatTrackedResponse :: TrackedResponse -> Text
formatTrackedResponse :: TrackedResponse -> Text
formatTrackedResponse (TrackedResponse (RequestId Int64
i) Response
payload) =
  [exon|<#{show i}> #{formatResponse payload}|]