module TheoremQuest.Transactions
( Req (..)
, Rsp (..)
, User
, Email
, TheoremId
, formatJSON
, formatText
, formatHaskell
, maybeRead
) where
import Codec.Binary.UTF8.String (encodeString)
import Network.HTTP
import Text.JSON
import TheoremQuest.Logic
type User = String
type Email = String
type TheoremId = Int
data Req
= Ping
| NewUser User Email
| RspInJSON Req
| Inference User (Inference TheoremId)
| TheoremAssumptions TheoremId
| TheoremConclusion TheoremId
| TheoremSearch Term Int
deriving (Show, Read)
data Rsp
= DeprecatedReq Rsp
| UnknownReq
| Ack
| Nack String
| Id Int
| Ids [Int]
| Term Term
| Terms [Term]
deriving (Show, Read)
instance JSON Rsp where
readJSON = undefined
showJSON = undefined
instance JSON Term where
readJSON = undefined
showJSON = undefined
formatJSON :: JSON a => a -> ([Header], String)
formatJSON a = ([Header HdrContentLength $ show $ length msg, Header HdrContentEncoding "UTF-8", Header HdrContentType "application/json"], msg)
where
msg = encodeString $ encode a
formatText :: String -> ([Header], String)
formatText a = ([Header HdrContentLength $ show $ length msg, Header HdrContentEncoding "UTF-8", Header HdrContentType "text/plain"], msg)
where
msg = encodeString a
formatHaskell :: Show a => a -> ([Header], String)
formatHaskell = formatText . show
maybeRead :: Read a => String -> Maybe a
maybeRead s = case [x | (x,t) <- reads s, ("","") <- lex t] of
[x] -> Just x
_ -> Nothing