Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mount! macro to simplify the construction of Mount objects #79

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ extern crate sequence_trie;
pub use mount::{Mount, OriginalUrl};

mod mount;
mod macros;

35 changes: 35 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/// Create and populate a mount.
///
/// ```ignore
/// let router = router!("/" => index,
/// "/:query" => queryHandler)
/// ```
///
/// Is equivalent to:
///
/// ```ignore
/// let mut mount = Mount::new();
/// mount.mount("/", index);
/// mount.mount("/:query", queryHandler);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this example suggests that mount can deal with variables in paths when it only matches by prefix. Also your macro example says router!, not mount!.

/// ```
#[macro_export]
macro_rules! mount {
($($mountpoint:expr => $handler:expr),+ $(,)*) => ( {
let mut mount = $crate::Mount::new();
$(mount.mount($mountpoint, $handler);)*
mount
});
}


#[cfg(test)]
mod tests {
use iron::{Response, Request, IronResult};

#[test]
fn methods() {
fn handler(_: &mut Request) -> IronResult<Response> {Ok(Response::new())}
let _ = mount!("/" => handler,
"/foo" => handler);
}
}