11module Routing.Match where
22
33import Prelude
4- import Data.Either
5- import Data.Tuple
6- import Data.Maybe
7- import Data.List
8- import Control.Alt
9- import Control.Plus
10- import Control.Apply
11- import Control.Alternative
12- import Control.Monad.Error
13- import qualified Data.StrMap as M
4+ import Data.Either (Either (..))
5+ import Data.Tuple (Tuple (..), snd )
6+ import Data.Maybe (Maybe (..))
7+ import Data.List (List (..), reverse )
8+ import Control.Alt (Alt , (<|>))
9+ import Control.Plus (Plus )
10+ import Control.Alternative (Alternative )
11+ import Control.Monad.Except
1412import Global (readFloat , isNaN )
15- import Data.Semiring.Free
13+ import Data.Semiring.Free ( Free (), free , runFree )
1614import Data.Foldable
17- import qualified Data.Array as A
1815import Data.Validation.Semiring
1916
17+
18+ import qualified Data.Map as M
19+ import qualified Data.String as S
20+
2021import Routing.Parser
2122import Routing.Types
2223import Routing.Match.Class
2324import Routing.Match.Error
2425
25- newtype Match a = Match (Route -> V (Free MatchError ) (Tuple Route a ))
26+ newtype Match a = Match (Route -> V (Free MatchError ) (Tuple Route a ))
27+ unMatch :: forall a . Match a -> (Route -> V (Free MatchError ) (Tuple Route a ))
28+ unMatch (Match a) = a
2629
2730instance matchMatchClass :: MatchClass Match where
2831 lit input = Match $ \route ->
@@ -35,21 +38,21 @@ instance matchMatchClass :: MatchClass Match where
3538 invalid $ free ExpectedPathPart
3639 num = Match $ \route ->
3740 case route of
38- Cons (Path input) rs ->
41+ Cons (Path input) rs ->
3942 let res = readFloat input in
4043 if isNaN res then
4144 invalid $ free ExpectedNumber
4245 else
43- pure $ Tuple rs res
46+ pure $ Tuple rs res
4447 _ ->
4548 invalid $ free ExpectedNumber
4649
4750 bool = Match $ \route ->
4851 case route of
4952 Cons (Path input) rs | input == " true" ->
50- pure $ Tuple rs true
53+ pure $ Tuple rs true
5154 Cons (Path input) rs | input == " false" ->
52- pure $ Tuple rs false
55+ pure $ Tuple rs false
5356 _ ->
5457 invalid $ free ExpectedBoolean
5558
@@ -80,24 +83,23 @@ instance matchFunctor :: Functor Match where
8083instance matchAlt :: Alt Match where
8184 alt (Match r2e1) (Match r2e2) = Match $ \r -> do
8285 (r2e1 r) <|> (r2e2 r)
83-
86+
8487instance matchPlus :: Plus Match where
8588 empty = Match $ const $ invalid one
8689
8790instance matchAlternative :: Alternative Match
8891
8992instance matchApply :: Apply Match where
90- apply (Match r2a2b) (Match r2a) =
93+ apply (Match r2a2b) (Match r2a) =
9194 Match $ (\r -> runV (processFnErr r) processFnRes (r2a2b r))
92- where processFnErr r err =
95+ where processFnErr r err =
9396 invalid $ err * runV id (const one) (r2a r)
9497 processFnRes (Tuple rs a2b) =
9598 runV invalid (\(Tuple rss a) -> pure $ Tuple rss (a2b a)) (r2a rs)
9699
97100instance matchApplicative :: Applicative Match where
98101 pure a = Match \r -> pure $ Tuple r a
99102
100-
101103-- | Matches list of matchers. Useful when argument can easy fail (not `str`)
102104-- | returns `Match Nil` if no matches
103105list :: forall a . Match a -> Match (List a )
@@ -109,22 +111,23 @@ list (Match r2a) =
109111 (const $ pure (Tuple r (reverse accum)))
110112 (\(Tuple rs a) -> go (Cons a accum) rs)
111113 (r2a r)
112-
114+
113115
114116
115117
116118-- It groups `Free MatchError` -> [[MatchError]] -map with showMatchError ->
117- -- [[String]] -fold with semicolon-> [String] -fold with newline-> String
119+ -- [[String]] -fold with semicolon-> [String] -fold with newline-> String
118120runMatch :: forall a . Match a -> Route -> Either String a
119121runMatch (Match fn) route =
120122 runV foldErrors (Right <<< snd) $ fn route
121- where foldErrors errs = Left $
122- foldl (\b a -> a <> " \n " <> b) " " do
123- es <- reverse <$> runFree errs
124- pure $ foldl (\b a -> a <> " ;" <> b) " " $ showMatchError <$> es
123+ where
124+ foldErrors errs =
125+ Left $ foldl (\b a -> a <> " \n " <> b) " " do
126+ es <- reverse <$> runFree errs
127+ pure $ foldl (\b a -> a <> " ;" <> b) " " $ showMatchError <$> es
125128
126129
127- -- | if we match something that can fail then we have to
130+ -- | if we match something that can fail then we have to
128131-- | match `Either a b`. This function converts matching on such
129132-- | sum to matching on right subpart. Matching on left branch fails.
130133-- | i.e.
@@ -134,16 +137,17 @@ runMatch (Match fn) route =
134137-- | sortOfString "asc" = Right Asc
135138-- | sortOfString "desc" = Right Desc
136139-- | sortOfString _ = Left "incorrect sort"
137- -- |
140+ -- |
138141-- | newtype Routing = Routing Sort
139142-- | routes :: Match Routing
140143-- | routes = (pure Routing) <*> (eitherMatch (sortOfString <$> var))
141- -- |
144+ -- |
142145-- | ```
143146eitherMatch :: forall a b . Match (Either a b ) -> Match b
144147eitherMatch (Match r2eab) = Match $ \r ->
145148 runV invalid runEither $ (r2eab r)
146- where runEither (Tuple rs eit) =
147- case eit of
148- Left _ -> invalid $ free $ Fail " Nested check failed"
149- Right res -> pure $ Tuple rs res
149+ where
150+ runEither (Tuple rs eit) =
151+ case eit of
152+ Left _ -> invalid $ free $ Fail " Nested check failed"
153+ Right res -> pure $ Tuple rs res
0 commit comments