Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User-friendly method names #30

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/parser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ let custom tipe (stringToSomething: string->Result<_,_>) : Parser<_,_> =
/42/ ==> Some "42"
</pre>
*)
[<System.Obsolete("UrlParser.str is deprecated, please use parseString instead.")>]
let str state =
custom "string" Ok state

Expand All @@ -86,16 +87,25 @@ let str state =
/42/ ==> Some 42
</pre>
*)
[<System.Obsolete("UrlParser.i32 is deprecated, please use parseInt instead.")>]
let i32 state =
custom "i32" (System.Int32.TryParse >> function true, value -> Ok value | _ -> Error "Can't parse int" ) state

/// Parse a segment of the path as a `String`.
let parseString state =
custom "String" Ok state

/// Parse a segment of the path as an `Int32`.
let parseInt state =
custom "Int32" (System.Int32.TryParse >> function true, value -> Ok value | _ -> Error "Can't parse Int32" ) state

(** Parse a segment of the path if it matches a given string.
```
s "blog" // can parse /blog/
// but not /glob/ or /42/ or anything else
```
*)
[<System.Obsolete("UrlParser.s is deprecated, please use matchString instead.")>]
let s str : Parser<_,_> =
let inner { visited = visited; unvisited = unvisited; args = args; value = value } =
match unvisited with
Expand All @@ -107,6 +117,18 @@ let s str : Parser<_,_> =
[]
inner

/// Parse a segment of the path if it matches a given string.
let matchString str : Parser<_,_> =
let inner { visited = visited; unvisited = unvisited; args = args; value = value } =
match unvisited with
| [] -> []
| next :: rest ->
if next = str then
[ State.mkState (next :: visited) rest args value ]
else
[]
inner

(** Parse all the remaining unvisited segments
```
remaining // can parse /blog/, /blog/article/32, / or anything else
Expand Down Expand Up @@ -222,10 +244,13 @@ let oneOf parsers state =
/blog/post/42 ==> Some (Post 42)
</pre>
*)
[<System.Obsolete("UrlParser.top is deprecated, please use matchNothing instead.")>]
let top state=
[state]


/// A parser that does not consume any path segments.
let matchNothing state =
[state]

(**
#### Query parameters
Expand Down