Skip to content

Commit

Permalink
Finish Level02/Types.hs
Browse files Browse the repository at this point in the history
  • Loading branch information
FaneBastinBellroy committed Mar 23, 2022
1 parent 6fa7c8f commit c012a80
Showing 1 changed file with 54 additions and 46 deletions.
100 changes: 54 additions & 46 deletions src/Level02/Types.hs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -fno-warn-dodgy-exports #-}

module Level02.Types
( Topic
, CommentText
, ContentType (..)
, RqType (..)
, Error (..)
, mkTopic
, getTopic
, mkCommentText
, getCommentText
, renderContentType
) where

import Data.ByteString (ByteString)
import Data.Text (Text)
( Topic,
CommentText,
ContentType (..),
RqType (..),
Error (..),
mkTopic,
getTopic,
mkCommentText,
getCommentText,
renderContentType,
)
where

import Data.ByteString (ByteString)
import Data.Text (Text)

-- Working through the specification for our application, what are the
-- types of requests we're going to handle?
Expand Down Expand Up @@ -44,11 +46,11 @@ import Data.Text (Text)

-- Topic
newtype Topic = Topic Text
deriving Show
deriving (Show)

-- CommentText
newtype CommentText = CommentText Text
deriving Show
deriving (Show)

-- Using these convenient definitions, we can create the following constructors
-- for our RqType:
Expand All @@ -57,14 +59,18 @@ newtype CommentText = CommentText Text
-- ViewRq : Which needs the topic being requested.
-- ListRq : Which doesn't need anything and lists all of the current topics.
data RqType
= AddRq Topic CommentText
| ViewRq Topic
| ListRq

-- Not everything goes according to plan, but it's important that our types
-- reflect when errors can be introduced into our program. Additionally it's
-- useful to be able to be descriptive about what went wrong.

-- Fill in the error constructors as you need them.
data Error

= EmptyTopicError
| EmptyCommentTextError

-- Provide the constructors for a sum type to specify the `ContentType` Header,
-- to be used when we build our Response type. Our application will be simple,
Expand All @@ -73,6 +79,8 @@ data Error
-- - plain text
-- - json
data ContentType
= PlainText
| JSON

-- The ``ContentType`` constructors don't match what is required for the header
-- information. Because ``wai`` uses a stringly type. So write a function that
Expand All @@ -85,11 +93,11 @@ data ContentType
-- - plain text = "text/plain"
-- - json = "application/json"
--
renderContentType
:: ContentType
-> ByteString
renderContentType =
error "renderContentType not implemented"
renderContentType ::
ContentType ->
ByteString
renderContentType PlainText = "text/plain"
renderContentType JSON = "application/json"

-- We can choose to *not* export the constructor for a data type and instead
-- provide a function of our own. In our case, we're not interested in empty
Expand All @@ -99,28 +107,28 @@ renderContentType =
-- The export list at the top of this file demonstrates how to export a type,
-- but not export the constructor.

mkTopic
:: Text
-> Either Error Topic
mkTopic =
error "mkTopic not implemented"

getTopic
:: Topic
-> Text
getTopic =
error "getTopic not implemented"

mkCommentText
:: Text
-> Either Error CommentText
mkCommentText =
error "mkCommentText not implemented"

getCommentText
:: CommentText
-> Text
getCommentText =
error "getCommentText not implemented"
mkTopic ::
Text ->
Either Error Topic
mkTopic t
| t == "" = Left EmptyTopicError
| otherwise = Right (Topic t)

getTopic ::
Topic ->
Text
getTopic (Topic t) = t

mkCommentText ::
Text ->
Either Error CommentText
mkCommentText ct
| ct == "" = Left EmptyCommentTextError
| otherwise = Right (CommentText ct)

getCommentText ::
CommentText ->
Text
getCommentText (CommentText ct) = ct

---- Go to `src/Level02/Core.hs` next

0 comments on commit c012a80

Please sign in to comment.