git.iko.soy/typeable/http-api-data
on 9b36264 help sign in
128 lines 3.7 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 (Int8, Int16, Int32, Int64)
13import Data.Word (Word, Word8, Word16, Word32, Word64)
14import qualified Data.Text as S
15import qualified Data.Text.Lazy as L
16import qualified Data.Text.Read
17import Data.Time (Day)
19class PathPiece s where
20 fromPathPiece :: S.Text -> Maybe s
21 toPathPiece :: s -> S.Text
23instance PathPiece String where
24 fromPathPiece = Just . S.unpack
25 toPathPiece = S.pack
27instance PathPiece S.Text where
28 fromPathPiece = Just
29 toPathPiece = id
31instance PathPiece L.Text where
32 fromPathPiece = Just . L.fromChunks . return
33 toPathPiece = S.concat . L.toChunks
35parseIntegral :: (Integral a, Bounded a, Ord a) => S.Text -> Maybe a
36parseIntegral s = n
37 where
38 n = case Data.Text.Read.signed Data.Text.Read.decimal s of
39 Right (i, _) | i <= top && i >= bot -> Just (fromInteger i)
40 _ -> Nothing
41 Just witness = n
42 top = toInteger (maxBound `asTypeOf` witness)
43 bot = toInteger (minBound `asTypeOf` witness)
45instance PathPiece Integer where
46 fromPathPiece s = case Data.Text.Read.signed Data.Text.Read.decimal s of
47 Right (i, _) -> Just i
48 Left _ -> Nothing
49 toPathPiece = S.pack . show
51instance PathPiece Int where
52 fromPathPiece = parseIntegral
53 toPathPiece = S.pack . show
55instance PathPiece Int8 where
56 fromPathPiece = parseIntegral
57 toPathPiece = S.pack . show
59instance PathPiece Int16 where
60 fromPathPiece = parseIntegral
61 toPathPiece = S.pack . show
63instance PathPiece Int32 where
64 fromPathPiece = parseIntegral
65 toPathPiece = S.pack . show
67instance PathPiece Int64 where
68 fromPathPiece = parseIntegral
69 toPathPiece = S.pack . show
71instance PathPiece Word where
72 fromPathPiece = parseIntegral
73 toPathPiece = S.pack . show
75instance PathPiece Word8 where
76 fromPathPiece = parseIntegral
77 toPathPiece = S.pack . show
79instance PathPiece Word16 where
80 fromPathPiece = parseIntegral
81 toPathPiece = S.pack . show
83instance PathPiece Word32 where
84 fromPathPiece = parseIntegral
85 toPathPiece = S.pack . show
87instance PathPiece Word64 where
88 fromPathPiece = parseIntegral
89 toPathPiece = S.pack . show
91instance PathPiece Day where
92 fromPathPiece t =
93 case reads $ S.unpack t of
94 [(a,"")] -> Just a
95 _ -> Nothing
96 toPathPiece = S.pack . show
98class PathMultiPiece s where
99 fromPathMultiPiece :: [S.Text] -> Maybe s
100 toPathMultiPiece :: s -> [S.Text]
102instance PathMultiPiece [String] where
103 fromPathMultiPiece = Just . map S.unpack
104 toPathMultiPiece = map S.pack
106instance PathMultiPiece [S.Text] where
107 fromPathMultiPiece = Just
108 toPathMultiPiece = id
110instance PathMultiPiece [L.Text] where
111 fromPathMultiPiece = Just . map (L.fromChunks . return)
112 toPathMultiPiece = map $ S.concat . L.toChunks
114{-# DEPRECATED toSinglePiece "Use toPathPiece instead of toSinglePiece" #-}
115toSinglePiece :: PathPiece p => p -> S.Text
116toSinglePiece = toPathPiece
118{-# DEPRECATED fromSinglePiece "Use fromPathPiece instead of fromSinglePiece" #-}
119fromSinglePiece :: PathPiece p => S.Text -> Maybe p
120fromSinglePiece = fromPathPiece
122{-# DEPRECATED toMultiPiece "Use toPathMultiPiece instead of toMultiPiece" #-}
123toMultiPiece :: PathMultiPiece ps => ps -> [S.Text]
124toMultiPiece = toPathMultiPiece
126{-# DEPRECATED fromMultiPiece "Use fromPathMultiPiece instead of fromMultiPiece" #-}
127fromMultiPiece :: PathMultiPiece ps => [S.Text] -> Maybe ps
128fromMultiPiece = fromPathMultiPiece