日本語

Pages under a page

A route declared with in PARENT continues its parent's path and parameters, and the compiler checks that it does.

Declaring a page under another

in PARENT after the path declares a route under the page of an earlier line. Its path is the parent's path followed by / and more segments. Its parameters are the parent's, in the same order, followed by its own, so a link to the child needs the parent's values too. Its search parameters are its own and do not come from the parent.

use hypersig::html::a;
use hypersig::{PathSegment, Route, Signals};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Org {}

hypersig::routes! {
    fn pages(Router<()>);
    const ORG: Route<PathSegment> = "/orgs/{org}" => org;
    // The child repeats the parent's path and parameters, then adds its own.
    const MEMBER: Route<(PathSegment, i32)> = "/orgs/{org}/members/{id}" in ORG => member;
}

let link = a::<Org>().href(MEMBER.link_with((PathSegment::new("acme"), 9)));
assert_eq!(link.render(), r#"<a href="/orgs/acme/members/9"></a>"#);

The trait Extends<P> holds that rule: a child of a route with parameters P needs parameters that implement Extends<P>, which a tuple does when it starts with P.

What the compiler checks

Parameters that do not start with the parent's fail to compile:

use hypersig::{PathSegment, Route};

hypersig::routes! {
    fn pages(Router<()>);
    const ORG: Route<PathSegment> = "/orgs/{org}" => org;
    // error[E0277]: `(i32, PathSegment)` does not start with `PathSegment`,
    // the parameters of the parent route
    const MEMBER: Route<(i32, PathSegment)> = "/orgs/{org}/members/{id}" in ORG => member;
}

A path that does not continue the parent's, such as "/members/{id}" under "/orgs/{org}", is error E0080 with the message "a child route's path is its parent's path followed by / and more segments".

What stays unchecked

  • A parent after in is a page itself, so a path prefix with no page of its own cannot be one.
  • in changes what compiles and nothing else. The router serves the child as a page of its own, and a request for the child runs only the child's handler, so a check the parent's handler makes, such as whether the org exists, is made again in the child's.

A complete example

An org lists its members, each member's page lies under the org, and the member's tabs lie under the member, with the tab as an enum segment. The org page reads the org from its signals and each id from the list item, and the member page writes the tab's URL on the server.

use hypersig::html::{a, each, h1, li, ul};
use hypersig::{Page, PathSegment, Route, Segment, Signals};

#[derive(Signals, Serialize, Deserialize, Clone)]
struct Member {
    id: i32,
    name: String,
}

#[derive(Signals, Serialize, Deserialize)]
struct Org {
    org: PathSegment,
    members: Vec<Member>,
}

#[derive(Segment, Serialize, Deserialize, Clone, Copy)]
#[serde(rename_all = "lowercase")]
enum Tab {
    Profile,
    Activity,
}

hypersig::routes! {
    fn pages(Router<()>);
    const ORG: Route<PathSegment> = "/orgs/{org}" => org;
    const MEMBER: Route<(PathSegment, i32)> = "/orgs/{org}/members/{id}" in ORG => member;
    // A child of a child: the org, the member, then the tab.
    const MEMBER_TAB: Route<(PathSegment, i32, Tab)> =
        "/orgs/{org}/members/{id}/{tab}" in MEMBER => member_tab;
}

async fn org((): (), org: PathSegment) -> Page<Org> {
    let members = load(&org);
    Page::new(org.to_string(), Org { org, members })
        .child(h1().bind_text(Org::org().to_text()))
        .child(ul().child(each(Org::members(), |member| {
            // The link reads the org from the signals and the id from the item.
            li().child(
                a().href(MEMBER.link_with((Org::org(), member.at(Member::id()))))
                    .bind_text(member.at(Member::name())),
            )
        })))
}

async fn member((): (), (org, id): (PathSegment, i32)) -> Option<Page<Org>> {
    let members: Vec<Member> = load(&org).into_iter().filter(|m| m.id == id).collect();
    let name = members.first()?.name.clone();
    // A URL written on the server is an `href` too.
    let activity = MEMBER_TAB.url((org.clone(), id, Tab::Activity));
    Some(
        Page::new(name, Org { org: org.clone(), members })
            .child(a().href(ORG.link_with(org)).text("Back to the org"))
            .child(a().href(activity).text("Activity")),
    )
}

Next

  • Search parameters adds a typed query string to a route.
  • Error pages shows a page for a path under /{lang} that no route serves, in the language of the path.