git.iko.soy/typeable/http-api-data
on 9de36e9 help sign in
99 lines 2.9 kB history raw
1{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
2module Web.PathPieces
3 ( PathPiece (..)
4 , PathMultiPiece (..)
5 -- * Deprecated
6 , toSinglePiece
7 , toMultiPiece
8 , fromSinglePiece
9 , fromMultiPiece
10 ) where
12import Data.Int (Int64)
13import qualified Data.Text as S
14import qualified Data.Text.Lazy as L
15import qualified Data.Text.Read
16import Data.Time (Day)
18class PathPiece s where
19 fromPathPiece :: S.Text -> Maybe s
20 toPathPiece :: s -> S.Text
22instance PathPiece String where
23 fromPathPiece = Just . S.unpack
24 toPathPiece = S.pack
26instance PathPiece S.Text where
27 fromPathPiece = Just
28 toPathPiece = id
30instance PathPiece L.Text where
31 fromPathPiece = Just . L.fromChunks . return
32 toPathPiece = S.concat . L.toChunks
34instance PathPiece Integer where
35 fromPathPiece s =
36 case Data.Text.Read.signed Data.Text.Read.decimal s of
37 Right (i, _) -> Just i
38 Left _ -> Nothing
39 toPathPiece = S.pack . show
41instance PathPiece Int where
42 fromPathPiece s =
43 case Data.Text.Read.signed Data.Text.Read.decimal s of
44 Right (i, _) -> Just i
45 Left _ -> Nothing
46 toPathPiece = S.pack . show
48instance PathPiece Bool where
49 fromPathPiece t =
50 case reads $ S.unpack t of
51 [(a,"")] -> Just a
52 _ -> Nothing
53 toPathPiece = S.pack . show
55instance PathPiece Int64 where
56 fromPathPiece s =
57 case Data.Text.Read.signed Data.Text.Read.decimal s of
58 Right (i, _) -> Just i
59 Left _ -> Nothing
60 toPathPiece = S.pack . show
62instance PathPiece Day where
63 fromPathPiece t =
64 case reads $ S.unpack t of
65 [(a,"")] -> Just a
66 _ -> Nothing
67 toPathPiece = S.pack . show
69class PathMultiPiece s where
70 fromPathMultiPiece :: [S.Text] -> Maybe s
71 toPathMultiPiece :: s -> [S.Text]
73instance PathMultiPiece [String] where
74 fromPathMultiPiece = Just . map S.unpack
75 toPathMultiPiece = map S.pack
77instance PathMultiPiece [S.Text] where
78 fromPathMultiPiece = Just
79 toPathMultiPiece = id
81instance PathMultiPiece [L.Text] where
82 fromPathMultiPiece = Just . map (L.fromChunks . return)
83 toPathMultiPiece = map $ S.concat . L.toChunks
85{-# DEPRECATED toSinglePiece "Use toPathPiece instead of toSinglePiece" #-}
86toSinglePiece :: PathPiece p => p -> S.Text
87toSinglePiece = toPathPiece
89{-# DEPRECATED fromSinglePiece "Use fromPathPiece instead of fromSinglePiece" #-}
90fromSinglePiece :: PathPiece p => S.Text -> Maybe p
91fromSinglePiece = fromPathPiece
93{-# DEPRECATED toMultiPiece "Use toPathMultiPiece instead of toMultiPiece" #-}
94toMultiPiece :: PathMultiPiece ps => ps -> [S.Text]
95toMultiPiece = toPathMultiPiece
97{-# DEPRECATED fromMultiPiece "Use fromPathMultiPiece instead of fromMultiPiece" #-}
98fromMultiPiece :: PathMultiPiece ps => [S.Text] -> Maybe ps
99fromMultiPiece = fromPathMultiPiece