1- module Routing.Match
2- ( module Routing.Match
3- , module Routing.Match.Class
4- ) where
1+ module Routing.Match where
52
63import Prelude
74
@@ -21,7 +18,6 @@ import Data.String.NonEmpty as NES
2118import Data.Tuple (Tuple (..), snd )
2219import Data.Validation.Semiring (V , invalid , unV )
2320import Global (readFloat , isNaN )
24- import Routing.Match.Class (class MatchClass , bool , end , fail , int , lit , num , param , params , root , str )
2521import Routing.Match.Error (MatchError (..), showMatchError )
2622import Routing.Types (Route , RoutePart (..))
2723
@@ -32,77 +28,6 @@ instance newtypeMatch :: Newtype (Match a) (List RoutePart -> V (Free MatchError
3228 wrap = Match
3329 unwrap (Match m) = m
3430
35- instance matchMatchClass :: MatchClass Match where
36- lit input = Match \route ->
37- case route of
38- Cons (Path i) rs | i == input ->
39- pure $ Tuple rs unit
40- Cons (Path _) rs ->
41- invalid $ free $ UnexpectedPath input
42- _ ->
43- invalid $ free ExpectedPathPart
44-
45- num = Match \route ->
46- case route of
47- Cons (Path input) rs ->
48- let res = readFloat input in
49- if isNaN res then
50- invalid $ free ExpectedNumber
51- else
52- pure $ Tuple rs res
53- _ ->
54- invalid $ free ExpectedNumber
55-
56- int = Match \route ->
57- case route of
58- Cons (Path input) rs -> case fromString input of
59- Nothing -> invalid $ free ExpectedInt
60- Just res -> pure $ Tuple rs res
61- _ ->
62- invalid $ free ExpectedInt
63-
64- bool = Match \route ->
65- case route of
66- Cons (Path input) rs | input == " true" ->
67- pure $ Tuple rs true
68- Cons (Path input) rs | input == " false" ->
69- pure $ Tuple rs false
70- _ ->
71- invalid $ free ExpectedBoolean
72-
73- str = Match \route ->
74- case route of
75- Cons (Path input) rs ->
76- pure $ Tuple rs input
77- _ ->
78- invalid $ free ExpectedString
79-
80- param key = Match \route ->
81- case route of
82- Cons (Query map) rs ->
83- case M .lookup key map of
84- Nothing ->
85- invalid $ free $ KeyNotFound key
86- Just el ->
87- pure $ Tuple (Cons (Query <<< M .delete key $ map) rs) el
88- _ ->
89- invalid $ free ExpectedQuery
90-
91- params = Match \route ->
92- case route of
93- Cons (Query map) rs ->
94- pure $ Tuple rs map
95- _ ->
96- invalid $ free ExpectedQuery
97-
98- end = Match \route ->
99- case route of
100- Nil -> pure $ Tuple Nil unit
101- _ -> invalid $ free ExpectedEnd
102-
103- fail msg = Match \_ ->
104- invalid $ free $ Fail msg
105-
10631instance matchFunctor :: Functor Match where
10732 map fn (Match r2e) = Match $ \r ->
10833 unV invalid (\(Tuple rs a) -> pure $ Tuple rs (fn a)) $ r2e r
@@ -127,6 +52,89 @@ instance matchApply :: Apply Match where
12752instance matchApplicative :: Applicative Match where
12853 pure a = Match \r -> pure $ Tuple r a
12954
55+ -- | Matches a leading slash.
56+ root :: Match Unit
57+ root = lit " "
58+
59+ lit :: String -> Match Unit
60+ lit input = Match \route ->
61+ case route of
62+ Cons (Path i) rs | i == input ->
63+ pure $ Tuple rs unit
64+ Cons (Path _) rs ->
65+ invalid $ free $ UnexpectedPath input
66+ _ ->
67+ invalid $ free ExpectedPathPart
68+
69+ num :: Match Number
70+ num = Match \route ->
71+ case route of
72+ Cons (Path input) rs ->
73+ let res = readFloat input in
74+ if isNaN res then
75+ invalid $ free ExpectedNumber
76+ else
77+ pure $ Tuple rs res
78+ _ ->
79+ invalid $ free ExpectedNumber
80+
81+ int :: Match Int
82+ int = Match \route ->
83+ case route of
84+ Cons (Path input) rs -> case fromString input of
85+ Nothing -> invalid $ free ExpectedInt
86+ Just res -> pure $ Tuple rs res
87+ _ ->
88+ invalid $ free ExpectedInt
89+
90+ bool :: Match Boolean
91+ bool = Match \route ->
92+ case route of
93+ Cons (Path input) rs | input == " true" ->
94+ pure $ Tuple rs true
95+ Cons (Path input) rs | input == " false" ->
96+ pure $ Tuple rs false
97+ _ ->
98+ invalid $ free ExpectedBoolean
99+
100+ str :: Match String
101+ str = Match \route ->
102+ case route of
103+ Cons (Path input) rs ->
104+ pure $ Tuple rs input
105+ _ ->
106+ invalid $ free ExpectedString
107+
108+ param :: String -> Match String
109+ param key = Match \route ->
110+ case route of
111+ Cons (Query map) rs ->
112+ case M .lookup key map of
113+ Nothing ->
114+ invalid $ free $ KeyNotFound key
115+ Just el ->
116+ pure $ Tuple (Cons (Query <<< M .delete key $ map) rs) el
117+ _ ->
118+ invalid $ free ExpectedQuery
119+
120+ params :: Match (M.Map String String )
121+ params = Match \route ->
122+ case route of
123+ Cons (Query map) rs ->
124+ pure $ Tuple rs map
125+ _ ->
126+ invalid $ free ExpectedQuery
127+
128+ end :: Match Unit
129+ end = Match \route ->
130+ case route of
131+ Nil -> pure $ Tuple Nil unit
132+ _ -> invalid $ free ExpectedEnd
133+
134+ fail :: forall a . String -> Match a
135+ fail msg = Match \_ ->
136+ invalid $ free $ Fail msg
137+
130138-- | Matches a non-empty string.
131139nonempty :: Match NonEmptyString
132140nonempty =
0 commit comments