Skip to main content

Group

Struct Group 

Source
pub struct Group<I, R, E: Render = NoRender> { /* private fields */ }
Expand description

Routes sharing a path prefix and an access boundary.

A group’s rule holds for every route in it: a route’s own rule adds to it (both are checked, the group’s first), and a route declared public inside a group with a rule stops the build. A route that declares nothing takes the group’s access.

use web_net::{App, Group, get};

let app = App::builder()
    .group(
        Group::at("/docs")
            .route(get("/", |cx| cx.text(200, "index")))
            .route(get("/:page", |cx| cx.text(200, "page"))),
    )
    .build()?;

Implementations§

Source§

impl<I: 'static, R, E: Render> Group<I, R, E>

Source

pub fn at(prefix: impl Into<String>) -> Self

A group under prefix (/admin).

use web_net::{App, Group, get};

let app = App::builder()
    .group(Group::at("/admin").route(get("/", |cx| cx.text(200, "dashboard"))))
    .build()?;
let r = serve_net::testing::respond(&app, b"GET /admin HTTP/1.1\r\nHost: localhost\r\n\r\n");
assert_eq!(r.text(), "dashboard");
Source

pub fn cors(self, cors: Cors) -> Self

The CORS policy of routes that declare none.

use web_net::{App, Cors, Group, get};

let api = Group::at("/api")
    .cors(Cors::new().origin("https://app.example.com"))
    .route(get("/items", |cx| cx.bytes(200, "application/json", b"[]")));
let app = App::builder().group(api).build()?;
let r = serve_net::testing::respond(
    &app,
    b"GET /api/items HTTP/1.1\r\nHost: localhost\r\nOrigin: https://app.example.com\r\n\r\n",
);
assert_eq!(r.header("access-control-allow-origin"), Some(&b"https://app.example.com"[..]));
Source

pub fn cache(self, policy: CachePolicy) -> Self

The cache policy of routes that declare none.

use web_net::{App, CachePolicy, Group, get};

let docs = Group::at("/docs")
    .cache(CachePolicy::Guests { max_age: 60 })
    .route(get("/intro", |cx| cx.text(200, "intro")));
let app = App::builder().group(docs).build()?;
let r = serve_net::testing::respond(&app, b"GET /docs/intro HTTP/1.1\r\nHost: localhost\r\n\r\n");
assert_eq!(r.header("cache-control"), Some(&b"public, max-age=60"[..]));
Source

pub fn public(self) -> Self

Routes that declare nothing are public.

use web_net::{App, Group, get};
// `NoStaff` is a gate that denies `Rule::Staff` to everyone.
let app = App::with_gate(NoStaff)
    .group(Group::at("/docs").public().route(get("/intro", |cx| cx.text(200, "intro"))))
    .build()?;
let r = serve_net::testing::respond(&app, b"GET /docs/intro HTTP/1.1\r\nHost: localhost\r\n\r\n");
assert_eq!(r.status, 200);
Source

pub fn allow(self, rule: R) -> Self

Every route in the group needs rule, as well as its own.

use web_net::{App, Group, get};
// `NoStaff` is a gate that denies `Rule::Staff` to everyone.
let app = App::with_gate(NoStaff)
    .group(Group::at("/admin").allow(Rule::Staff).route(get("/users", |cx| cx.text(200, "users"))))
    .build()?;
let r = serve_net::testing::respond(&app, b"GET /admin/users HTTP/1.1\r\nHost: localhost\r\n\r\n");
assert_eq!(r.status, 403);
Source

pub fn route(self, route: Route<I, R, E>) -> Self

Adds a route; its pattern is joined to the prefix.

use web_net::{App, Group, get};

let blog = Group::at("/blog").route(get("/:slug", |cx| {
    let slug: String = cx.param_as("slug")?;
    cx.text(200, &slug)
}));
let app = App::builder().group(blog).build()?;
let r = serve_net::testing::respond(&app, b"GET /blog/hello HTTP/1.1\r\nHost: localhost\r\n\r\n");
assert_eq!(r.text(), "hello");
Source

pub fn group(self, group: Group<I, R, E>) -> Self

Adds a nested group; its prefix is joined to this one, and its access holds inside this group’s (both rules apply).

use web_net::{App, Group, get};

let v1 = Group::at("/v1").route(get("/items", |cx| cx.text(200, "items")));
let app = App::builder().group(Group::at("/api").group(v1)).build()?;
let r = serve_net::testing::respond(&app, b"GET /api/v1/items HTTP/1.1\r\nHost: localhost\r\n\r\n");
assert_eq!(r.text(), "items");

Trait Implementations§

Source§

impl<I, R, E: Render> Debug for Group<I, R, E>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<I, R, E = NoRender> !RefUnwindSafe for Group<I, R, E>

§

impl<I, R, E = NoRender> !UnwindSafe for Group<I, R, E>

§

impl<I, R, E> Forget for Group<I, R, E>

§

impl<I, R, E> Freeze for Group<I, R, E>
where Access<R>: Freeze, Vec<Route<I, R, E>>: Freeze,

§

impl<I, R, E> Move for Group<I, R, E>

§

impl<I, R, E> Send for Group<I, R, E>
where Access<R>: Send, Vec<Route<I, R, E>>: Send,

§

impl<I, R, E> Sync for Group<I, R, E>
where Access<R>: Sync, Vec<Route<I, R, E>>: Sync,

§

impl<I, R, E> Unpin for Group<I, R, E>
where Access<R>: Unpin, Vec<Route<I, R, E>>: Unpin,

§

impl<I, R, E> UnsafeUnpin for Group<I, R, E>
where Access<R>: UnsafeUnpin, Vec<Route<I, R, E>>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.