1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//! Trout is a tree-based routing library.
//! It is fairly generic, but designed to be used for HTTP servers.

#![warn(missing_docs)]

#[cfg(feature = "http02")]
pub mod http02;
mod internal;
pub mod node;

pub use node::Node;

#[derive(Debug, Clone, Copy)]
/// Ways that routing can fail
pub enum RoutingFailure {
    /// No node was found for the request path
    NotFound,
    /// No handler was found for the request method
    MethodNotAllowed,
}

/// Trait to represent a Request which can be handled
pub trait Request {
    /// Request method, as in HTTP
    type Method: Eq + std::hash::Hash + Clone + Send + Sync;

    /// Path of request. Should *not* have a leading slash.
    fn path<'a>(&'a self) -> &'a str;
    /// Request method, as in HTTP
    fn method<'a>(&'a self) -> &Self::Method;
}

impl Request for String {
    type Method = ();

    fn path(&self) -> &str {
        &self
    }

    fn method(&self) -> &Self::Method {
        &()
    }
}