Skip to content

Commit 1c1aa3c

Browse files
authored
Merge pull request #36 from throughnothing/end-match
Add `end` combinator to support matching end of routes.
2 parents 45872c1 + e3f0908 commit 1c1aa3c

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

src/Routing/Match.purs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Data.List (List(..), reverse)
1313
import Data.Map as M
1414
import Data.Maybe (Maybe(..))
1515
import Data.Newtype (class Newtype, unwrap)
16-
import Data.Semiring.Free (Free(), free)
16+
import Data.Semiring.Free (Free, free)
1717
import Data.Tuple (Tuple(..), snd)
1818
import Data.Validation.Semiring (V, invalid, unV)
1919

@@ -93,6 +93,11 @@ instance matchMatchClass :: MatchClass Match where
9393
_ ->
9494
invalid $ free ExpectedQuery
9595

96+
end = Match \route ->
97+
case route of
98+
Nil -> pure $ Tuple Nil unit
99+
_ -> invalid $ free ExpectedEnd
100+
96101
fail msg = Match \_ ->
97102
invalid $ free $ Fail msg
98103

src/Routing/Match/Class.purs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,7 @@ class (Alternative f) <= MatchClass f where
3232
-- | `bool` matches any boolean path component.
3333
bool :: f Boolean
3434

35+
-- | `end` matches the end of a route.
36+
end :: f Unit
37+
3538
fail :: forall a. String -> f a

src/Routing/Match/Error.purs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ data MatchError
77
= UnexpectedPath String
88
-- expected "true" or "false"
99
| ExpectedBoolean
10+
-- expected end
11+
| ExpectedEnd
1012
-- expected numeric literal
1113
| ExpectedNumber
1214
-- expected integer literal
@@ -31,6 +33,7 @@ showMatchError err =
3133
ExpectedNumber -> "expected number"
3234
ExpectedInt -> "expected int"
3335
ExpectedBoolean -> "expected boolean"
36+
ExpectedEnd -> "expected end"
3437
ExpectedString -> "expected string var"
3538
ExpectedPathPart -> "expected path part, found query"
3639
Fail str -> "match error: " <> str

test/Test/Main.purs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,49 @@
11
module Test.Main where
22

3-
import Prelude (class Show, Unit, discard, show, ($), (<$>), (*>), (<*>), (<>))
3+
import Prelude (class Show, Unit, discard, show, ($), (<$>), (*>), (<*), (<*>), (<>), append)
44
import Control.Monad.Eff (Eff)
5-
import Control.Monad.Eff.Console (CONSOLE(), logShow)
5+
import Control.Monad.Eff.Console (CONSOLE(), log)
66
import Control.Alt ((<|>))
77
import Data.List (List)
88
import Data.Map as M
99

1010

1111
import Routing (match)
1212
import Routing.Match (Match, list)
13-
import Routing.Match.Class (num, int, param, bool, lit, params)
13+
import Routing.Match.Class (bool, end, int, lit, num, param, params)
1414

1515
data FooBar
1616
= Foo Number (M.Map String String)
1717
| Bar Boolean String
1818
| Baz (List Number)
1919
| Quux Int
20+
| End Int
2021

2122
instance showFooBar :: Show FooBar where
2223
show (Foo num q) = "(Foo " <> show num <> " " <> show q <> ")"
2324
show (Bar bool str) = "(Bar " <> show bool <> " " <> show str <> ")"
2425
show (Baz lst) = "(Baz " <> show lst <> ")"
2526
show (Quux i) = "(Quux " <> show i <> ")"
27+
show (End i) = "(End " <> show i <> ")"
2628

2729
routing :: Match FooBar
2830
routing =
2931
Foo <$> (lit "foo" *> num) <*> params
3032
<|> Bar <$> (lit "bar" *> bool) <*> (param "baz")
3133
<|> Quux <$> (lit "" *> lit "quux" *> int)
34+
-- Order matters here. `list` is greedy, and `end` wont match after it
35+
<|> End <$> (lit "" *> int <* end)
3236
<|> Baz <$> (list num)
3337

3438

3539
main :: Eff (console :: CONSOLE) Unit
3640
main = do
37-
logShow $ match routing "foo/12/?welp='hi'&b=false"
38-
logShow $ match routing "/quux/42"
41+
print "Foo: " $ match routing "foo/12/?welp='hi'&b=false" -- foo
42+
print "Quux: " $ match routing "/quux/42" -- quux
43+
print "Baz: " $ match routing "/123/" -- baz
44+
print "End: " $ match routing "/1" -- end
45+
46+
where print s e = log $ append s $ show e
3947

4048
-- (minimal test for browser)
4149

0 commit comments

Comments
 (0)