A fast and flexible URL router implementation for the MoonBit language.
moon add ShellWen/sw-routerfn main {
let router = @sw_router.router?([
("/test", fn { _path, _query, _fragment => "main" }),
(
"/:foo/:bar",
fn {
path, query, fragment => {
let foo = path.get("foo").unwrap()
let bar = path.get("bar").unwrap()
let flat_query = query.map(fn { (k, v) => k + "=" + v }).join("&")
let fragment = fragment.or("")
"FOO_\{foo}__BAR_\{bar}__QUERY_\{flat_query}__FRAGMENT_\{fragment}"
}
},
),
]).unwrap()
let str = router.dispatch!("/test")
assert_eq!(str, "main")
let str = router.dispatch!("/hello/world?param=value&a=b#fragment")
assert_eq!(str, "FOO_hello__BAR_world__QUERY_param=value&a=b__FRAGMENT_fragment")
}pub suberror AddRouteError {
ParseLocationError(LocationParseError)
AlreadyExists(String)
PathParamAlreadyExists(String)
RouteShouldNotHaveQuery(String)
RouteShouldNotHaveFragment(String)
}impl Eq for AddRouteErrorimpl Show for AddRouteErrorpub suberror DispatchError {
InvalidLocation(LocationParseError)
InvalidQuery(QueryParseError)
RouteNotMatched(String)
}impl Eq for DispatchErrorimpl Show for DispatchErrorpub suberror LocationParseError {
ShouldStartWithSlash
MoreThanOneQuery
MoreThanOneFragment
FragmentBeforeQuery
EmptyLocation
InvalidCharacter(Int)
}impl Eq for LocationParseErrorimpl Show for LocationParseErrorpub suberror QueryParseError {
InvalidCharacter(Int)
}impl Eq for QueryParseErrorimpl Show for QueryParseErrortype Router[A]fn[A] Router::add_route(self : Router[A], path : String, handler : (Map[String, String], Array[(String, String)], String?) -> A) -> Unit raise AddRouteErrorfn[A] router(configs : Array[(String, (Map[String, String], Array[(String, String)], String?) -> A)]) -> Router[A] raise AddRouteErrorInstall
Download zipA fast and flexible URL router implementation for the MoonBit language.