Skip to main content

Routes

Struct Routes 

Source
pub struct Routes(/* private fields */);
Expand description

Which routes a hook runs for.

use web_net::Routes;

let every = Routes::all();
let blog = Routes::prefix("/blog");
let pages = Routes::named(&["home", "about"]);

Implementations§

Source§

impl Routes

Source

pub fn all() -> Self

Every route.

use web_net::{App, Hook, Routes, get};

let app = App::builder()
    .hook(Hook::Finish, Routes::all(), |cx| {
        cx.set_header("x-site", "demo");
        Ok(())
    })
    .route(get("/", |cx| cx.text(200, "home")))
    .build()?;
let r = serve_net::testing::respond(&app, b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n");
assert_eq!(r.header("x-site"), Some(&b"demo"[..]));
Source

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

Routes whose pattern starts with prefix (/blog).

use serve_net::testing::respond;
use web_net::{App, Hook, Routes, get};

let app = App::builder()
    .hook(Hook::Finish, Routes::prefix("/docs"), |cx| {
        cx.set_header("x-section", "docs");
        Ok(())
    })
    .route(get("/docs/intro", |cx| cx.text(200, "intro")))
    .route(get("/docsearch", |cx| cx.text(200, "search")))
    .build()?;
let r = respond(&app, b"GET /docs/intro HTTP/1.1\r\nHost: localhost\r\n\r\n");
assert_eq!(r.header("x-section"), Some(&b"docs"[..]));
// A prefix matches whole segments.
let r = respond(&app, b"GET /docsearch HTTP/1.1\r\nHost: localhost\r\n\r\n");
assert_eq!(r.header("x-section"), None);
Source

pub fn named(names: &[&'static str]) -> Self

Routes with one of these names.

use web_net::{App, Hook, Routes, get};

let app = App::builder()
    .hook(Hook::Finish, Routes::named(&["home", "about"]), |cx| {
        cx.set_header("x-page", "static");
        Ok(())
    })
    .route(get("/", |cx| cx.text(200, "home")).name("home"))
    .build()?;
let r = serve_net::testing::respond(&app, b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n");
assert_eq!(r.header("x-page"), Some(&b"static"[..]));

Trait Implementations§

Source§

impl Clone for Routes

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Routes

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Forget for Routes

§

impl Freeze for Routes

§

impl Move for Routes

§

impl RefUnwindSafe for Routes

§

impl Send for Routes

§

impl Sync for Routes

§

impl Unpin for Routes

§

impl UnsafeUnpin for Routes

§

impl UnwindSafe for Routes

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.