-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypes.hs
32 lines (26 loc) · 993 Bytes
/
Types.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
module Types where
{- Module with definitions of common types used in the application. -}
import Prelude
import Data.Char(isDigit)
import qualified Data.Text as T
import Network.HTTP.Base(urlEncode, urlDecode)
import Web.PathPieces(PathPiece(..))
newtype PageNumber = PageNumber Int
deriving (Show, Eq, Read)
instance PathPiece PageNumber where
fromPathPiece text
| T.all isDigit text =
let number = read . T.unpack $ text
in
if number > 0
then Just . PageNumber $ number
else Nothing
| otherwise = Nothing
toPathPiece (PageNumber number)
| number > 0 = T.pack . show $ number
| otherwise = "1"
newtype URLEncodedText = URLEncodedText T.Text
deriving (Show, Eq, Read)
instance PathPiece URLEncodedText where
fromPathPiece = Just . URLEncodedText . T.pack . urlDecode . T.unpack
toPathPiece (URLEncodedText text) = T.pack . urlEncode . T.unpack $ text