子のページ
in PARENT を付けて宣言したルートは親のパスとパラメータを引き継ぎ、コンパイラがそれを検査します。
ページの下にページを宣言する
パスのあとに in 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>"#);この規則はトレイト Extends<P> で表します。パラメータが P のルートの子には Extends<P> を実装したパラメータが必要で、タプルは P で始まるときに Extends<P> を実装します。
コンパイラが検査すること
親のパラメータで始まらないパラメータは、コンパイルできません。
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;
}"/orgs/{org}" の下の "/members/{id}" のように親のパスの続きになっていないパスは、エラー E0080 になります。メッセージは "a child route's path is its parent's path followed by / and more segments" です。
検査されないこと
inのあとに書く親はそれ自体がページなので、自分のページを持たないパスの接頭辞は親にできません。inが変えるのは、何がコンパイルできるかだけです。ルーターは子を独立したページとして配信し、子へのリクエストでは子のハンドラだけが動きます。そのため、組織があるかどうかのように親のハンドラがする検査は、子のハンドラでもう一度します。
完全な例
組織のページはメンバーを一覧にし、各メンバーのページは組織の下に、メンバーのタブはメンバーの下にあります。タブは列挙型のセグメントです。組織のページは組織をシグナルから、各 id をリストの項目から読み、メンバーのページはタブの URL をサーバーで書きます。
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")),
)
}