@@ -1,7 +1,9 @@
1−{-# LANGUAGE FlexibleInstances #-}
2−{-# LANGUAGE TypeSynonymInstances #-}
1+{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
32module Web.PathPieces
4− ( SinglePiece (..)
3+ ( RoutePiece (..)
4+ , RouteMultiPiece (..)
5+ -- * Deprecated
6+ , SinglePiece (..)
57 , MultiPiece (..)
68 ) where
79
@@ -10,30 +12,92 @@ import qualified Data.Text as S
1012import qualified Data.Text.Lazy as L
1113import qualified Data.Text.Read
1214
15+class RoutePiece s where
16+ fromRoutePiece :: S.Text -> Maybe s
17+ toRoutePiece :: s -> S.Text
18+
19+instance RoutePiece String where
20+ fromRoutePiece s = if S.null s then Nothing else Just (S.unpack s)
21+ toRoutePiece = S.pack
22+
23+instance RoutePiece S.Text where
24+ fromRoutePiece s = if S.null s then Nothing else Just s
25+ toRoutePiece = id
26+
27+instance RoutePiece L.Text where
28+ fromRoutePiece s = if S.null s then Nothing else Just (L.fromChunks [s])
29+ toRoutePiece = S.concat . L.toChunks
30+
31+instance RoutePiece Integer where
32+ fromRoutePiece s =
33+ case Data.Text.Read.decimal s of
34+ Right (i, _) -> Just i
35+ Left _ -> Nothing
36+ toRoutePiece = S.pack . show
37+
38+instance RoutePiece Int where
39+ fromRoutePiece s =
40+ case Data.Text.Read.decimal s of
41+ Right (i, _) -> Just i
42+ Left _ -> Nothing
43+ toRoutePiece = S.pack . show
44+
45+instance RoutePiece Int64 where
46+ fromRoutePiece s =
47+ case Data.Text.Read.decimal s of
48+ Right (i, _) -> Just i
49+ Left _ -> Nothing
50+ toRoutePiece = S.pack . show
51+
52+
53+class RouteMultiPiece s where
54+ fromRouteMultiPiece :: [S.Text] -> Maybe s
55+ toRouteMultiPiece :: s -> [S.Text]
56+
57+instance RouteMultiPiece [String] where
58+ fromRouteMultiPiece = Just . map S.unpack
59+ toRouteMultiPiece = map S.pack
60+
61+instance RouteMultiPiece [S.Text] where
62+ fromRouteMultiPiece = Just
63+ toRouteMultiPiece = id
64+
65+instance RouteMultiPiece [L.Text] where
66+ fromRouteMultiPiece = Just . map (L.fromChunks . return)
67+ toRouteMultiPiece = map $ S.concat . L.toChunks
68+
69+
70+-- | deprecated api
1371class SinglePiece s where
1472 fromSinglePiece :: S.Text -> Maybe s
1573 toSinglePiece :: s -> S.Text
74+
1675instance SinglePiece String where
1776 fromSinglePiece s = if S.null s then Nothing else Just (S.unpack s)
1877 toSinglePiece = S.pack
78+
1979instance SinglePiece S.Text where
2080 fromSinglePiece s = if S.null s then Nothing else Just s
2181 toSinglePiece = id
82+
2283instance SinglePiece L.Text where
2384 fromSinglePiece s = if S.null s then Nothing else Just (L.fromChunks [s])
2485 toSinglePiece = S.concat . L.toChunks
86+
2587instance SinglePiece Integer where
2688 fromSinglePiece s =
2789 case Data.Text.Read.decimal s of
2890 Right (i, _) -> Just i
2991 Left _ -> Nothing
3092 toSinglePiece = S.pack . show
93+
3194instance SinglePiece Int where
3295 fromSinglePiece s =
3396 case Data.Text.Read.decimal s of
3497 Right (i, _) -> Just i
3598 Left _ -> Nothing
3699 toSinglePiece = S.pack . show
100+
37101instance SinglePiece Int64 where
38102 fromSinglePiece s =
39103 case Data.Text.Read.decimal s of
@@ -41,15 +105,19 @@ instance SinglePiece Int64 where
41105 Left _ -> Nothing
42106 toSinglePiece = S.pack . show
43107
108+
44109class MultiPiece s where
45110 fromMultiPiece :: [S.Text] -> Maybe s
46111 toMultiPiece :: s -> [S.Text]
112+
47113instance MultiPiece [String] where
48114 fromMultiPiece = Just . map S.unpack
49115 toMultiPiece = map S.pack
116+
50117instance MultiPiece [S.Text] where
51118 fromMultiPiece = Just
52119 toMultiPiece = id
120+
53121instance MultiPiece [L.Text] where
54122 fromMultiPiece = Just . map (L.fromChunks . return)
55123 toMultiPiece = map $ S.concat . L.toChunks