use dispatch_core::{Dispatcher, Handler, Matcher, Middleware};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Command {
Read,
Write,
}
struct Request {
command: Command,
authenticated: bool,
value: u32,
}
#[derive(Debug, PartialEq, Eq)]
struct Response {
value: u32,
audited: bool,
}
#[derive(Debug, PartialEq, Eq)]
enum Error {
Unauthorized,
}
struct Route {
command: Command,
operation: Operation,
}
enum Operation {
Read,
Double,
}
impl Handler for Route {
type Input = Request;
type Output = Response;
type Error = Error;
fn handle(&self, request: &Request) -> Result<Response, Error> {
let value = match self.operation {
Operation::Read => request.value,
Operation::Double => request.value * 2,
};
Ok(Response {
value,
audited: false,
})
}
}
struct CommandMatcher;
impl Matcher<Route, Request> for CommandMatcher {
fn matches(&self, route: &Route, request: &Request) -> bool {
route.command == request.command
}
}
struct SecurityAndAudit {
before_count: u32,
after_count: u32,
}
impl Middleware<Route, Request, Response, Error> for SecurityAndAudit {
fn before(&mut self, _route: &Route, request: &Request) -> Result<(), Error> {
self.before_count += 1;
if !request.authenticated {
return Err(Error::Unauthorized);
}
Ok(())
}
fn after(
&mut self,
_route: &Route,
_request: &Request,
response: &mut Response,
) -> Result<(), Error> {
self.after_count += 1;
response.audited = true;
Ok(())
}
}
fn main() {
let routes = [
Route {
command: Command::Read,
operation: Operation::Read,
},
Route {
command: Command::Write,
operation: Operation::Double,
},
];
let middleware = SecurityAndAudit {
before_count: 0,
after_count: 0,
};
let mut dispatcher = Dispatcher::new(CommandMatcher, middleware);
let request = Request {
command: Command::Write,
authenticated: true,
value: 21,
};
let response = dispatcher.dispatch(&routes, &request).unwrap();
assert_eq!(
response,
Response {
value: 42,
audited: true,
}
);
assert_eq!(dispatcher.middleware().before_count, 1);
assert_eq!(dispatcher.middleware().after_count, 1);
println!("value={}, audited={}", response.value, response.audited);
}