@@ -1,7 +1,9 @@
1−{-# LANGUAGE FlexibleInstances #-}
1+{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
2−{-# LANGUAGE TypeSynonymInstances #-}
3module Web.PathPieces
2module Web.PathPieces
4+ , RouteMultiPiece (..)
4− ( SinglePiece (..)
6+ , SinglePiece (..)
5 , MultiPiece (..)
7 , MultiPiece (..)
@@ -10,30 +12,92 @@ import qualified Data.Text as S
10import qualified Data.Text.Lazy as L
12import qualified Data.Text.Lazy as L
11import qualified Data.Text.Read
13import qualified Data.Text.Read
15+class RoutePiece s where
16+ fromRoutePiece :: S.Text -> Maybe s
17+ toRoutePiece :: s -> S.Text
19+instance RoutePiece String where
20+ fromRoutePiece s = if S.null s then Nothing else Just (S.unpack s)
21+ toRoutePiece = S.pack
23+instance RoutePiece S.Text where
24+ fromRoutePiece s = if S.null s then Nothing else Just s
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
31+instance RoutePiece Integer where
33+ case Data.Text.Read.decimal s of
34+ Right (i, _) -> Just i
36+ toRoutePiece = S.pack . show
38+instance RoutePiece Int where
40+ case Data.Text.Read.decimal s of
41+ Right (i, _) -> Just i
43+ toRoutePiece = S.pack . show
45+instance RoutePiece Int64 where
47+ case Data.Text.Read.decimal s of
48+ Right (i, _) -> Just i
50+ toRoutePiece = S.pack . show
53+class RouteMultiPiece s where
54+ fromRouteMultiPiece :: [S.Text] -> Maybe s
55+ toRouteMultiPiece :: s -> [S.Text]
57+instance RouteMultiPiece [String] where
58+ fromRouteMultiPiece = Just . map S.unpack
59+ toRouteMultiPiece = map S.pack
61+instance RouteMultiPiece [S.Text] where
62+ fromRouteMultiPiece = Just
63+ toRouteMultiPiece = id
65+instance RouteMultiPiece [L.Text] where
66+ fromRouteMultiPiece = Just . map (L.fromChunks . return)
67+ toRouteMultiPiece = map $ S.concat . L.toChunks
13class SinglePiece s where
71class SinglePiece s where
14 fromSinglePiece :: S.Text -> Maybe s
72 fromSinglePiece :: S.Text -> Maybe s
15 toSinglePiece :: s -> S.Text
73 toSinglePiece :: s -> S.Text
16instance SinglePiece String where
75instance SinglePiece String where
17 fromSinglePiece s = if S.null s then Nothing else Just (S.unpack s)
76 fromSinglePiece s = if S.null s then Nothing else Just (S.unpack s)
18 toSinglePiece = S.pack
77 toSinglePiece = S.pack
19instance SinglePiece S.Text where
79instance SinglePiece S.Text where
20 fromSinglePiece s = if S.null s then Nothing else Just s
80 fromSinglePiece s = if S.null s then Nothing else Just s
21 toSinglePiece = id
81 toSinglePiece = id
22instance SinglePiece L.Text where
83instance SinglePiece L.Text where
23 fromSinglePiece s = if S.null s then Nothing else Just (L.fromChunks [s])
84 fromSinglePiece s = if S.null s then Nothing else Just (L.fromChunks [s])
24 toSinglePiece = S.concat . L.toChunks
85 toSinglePiece = S.concat . L.toChunks
25instance SinglePiece Integer where
87instance SinglePiece Integer where
26 fromSinglePiece s =
88 fromSinglePiece s =
27 case Data.Text.Read.decimal s of
89 case Data.Text.Read.decimal s of
28 Right (i, _) -> Just i
90 Right (i, _) -> Just i
29 Left _ -> Nothing
91 Left _ -> Nothing
30 toSinglePiece = S.pack . show
92 toSinglePiece = S.pack . show
31instance SinglePiece Int where
94instance SinglePiece Int where
32 fromSinglePiece s =
95 fromSinglePiece s =
33 case Data.Text.Read.decimal s of
96 case Data.Text.Read.decimal s of
34 Right (i, _) -> Just i
97 Right (i, _) -> Just i
35 Left _ -> Nothing
98 Left _ -> Nothing
36 toSinglePiece = S.pack . show
99 toSinglePiece = S.pack . show
37instance SinglePiece Int64 where
101instance SinglePiece Int64 where
38 fromSinglePiece s =
102 fromSinglePiece s =
39 case Data.Text.Read.decimal s of
103 case Data.Text.Read.decimal s of
@@ -41,15 +105,19 @@ instance SinglePiece Int64 where
41 Left _ -> Nothing
105 Left _ -> Nothing
42 toSinglePiece = S.pack . show
106 toSinglePiece = S.pack . show
44class MultiPiece s where
109class MultiPiece s where
45 fromMultiPiece :: [S.Text] -> Maybe s
110 fromMultiPiece :: [S.Text] -> Maybe s
46 toMultiPiece :: s -> [S.Text]
111 toMultiPiece :: s -> [S.Text]
47instance MultiPiece [String] where
113instance MultiPiece [String] where
48 fromMultiPiece = Just . map S.unpack
114 fromMultiPiece = Just . map S.unpack
49 toMultiPiece = map S.pack
115 toMultiPiece = map S.pack
50instance MultiPiece [S.Text] where
117instance MultiPiece [S.Text] where
51 fromMultiPiece = Just
118 fromMultiPiece = Just
52 toMultiPiece = id
119 toMultiPiece = id
53instance MultiPiece [L.Text] where
121instance MultiPiece [L.Text] where
54 fromMultiPiece = Just . map (L.fromChunks . return)
122 fromMultiPiece = Just . map (L.fromChunks . return)
55 toMultiPiece = map $ S.concat . L.toChunks
123 toMultiPiece = map $ S.concat . L.toChunks