pub struct Dispatcher<'table, K, C, R, O, E, M, B = NoBefore, A = NoAfter, S = IdentityResponse> { /* private fields */ }Expand description
A borrowed command table paired with a matching strategy.
Table order defines priority: the first matching entry is executed.
Implementations§
Source§impl<'table, K, C, R, O, E, M> Dispatcher<'table, K, C, R, O, E, M, NoBefore, NoAfter, IdentityResponse>
impl<'table, K, C, R, O, E, M> Dispatcher<'table, K, C, R, O, E, M, NoBefore, NoAfter, IdentityResponse>
Sourcepub const fn new(
table: &'table [CommandEntry<K, C, R, O, E>],
matcher: M,
) -> Self
pub const fn new( table: &'table [CommandEntry<K, C, R, O, E>], matcher: M, ) -> Self
Creates a dispatcher over table.
Examples found in repository?
124fn main() {
125 let mut context = Context {
126 opcode: Opcode::SignAndSend,
127 payload: 40,
128 };
129 let mut resources = Resources {
130 io: Io::default(),
131 crypto: Crypto { key: 2 },
132 audit: Audit::default(),
133 };
134
135 let dispatcher = Dispatcher::new(COMMANDS, KeyMatcher)
136 .with_before(before)
137 .with_after(after)
138 .with_response(response);
139 let response = dispatcher.dispatch(&mut context, &mut resources).unwrap();
140
141 assert_eq!(
142 response,
143 WireResponse {
144 status: 0,
145 value: 42,
146 }
147 );
148 assert_eq!(resources.io.last_value, 42);
149 assert_eq!(resources.io.writes, 1);
150 assert_eq!(resources.audit.before_calls, 1);
151 assert_eq!(resources.audit.after_calls, 1);
152 assert_eq!(resources.audit.response_calls, 1);
153 println!(
154 "status={}, value={}, writes={}",
155 response.status, response.value, resources.io.writes
156 );
157}Source§impl<'table, K, C, R, O, E, M, B, A, S> Dispatcher<'table, K, C, R, O, E, M, B, A, S>
impl<'table, K, C, R, O, E, M, B, A, S> Dispatcher<'table, K, C, R, O, E, M, B, A, S>
Sourcepub fn with_before<F, Params>(
self,
function: F,
) -> Dispatcher<'table, K, C, R, O, E, M, BeforeFn<F, Params>, A, S>
pub fn with_before<F, Params>( self, function: F, ) -> Dispatcher<'table, K, C, R, O, E, M, BeforeFn<F, Params>, A, S>
Installs a resource-injected before function.
This does not install an after function. Calling with_before again
replaces only the previous before stage.
Examples found in repository?
124fn main() {
125 let mut context = Context {
126 opcode: Opcode::SignAndSend,
127 payload: 40,
128 };
129 let mut resources = Resources {
130 io: Io::default(),
131 crypto: Crypto { key: 2 },
132 audit: Audit::default(),
133 };
134
135 let dispatcher = Dispatcher::new(COMMANDS, KeyMatcher)
136 .with_before(before)
137 .with_after(after)
138 .with_response(response);
139 let response = dispatcher.dispatch(&mut context, &mut resources).unwrap();
140
141 assert_eq!(
142 response,
143 WireResponse {
144 status: 0,
145 value: 42,
146 }
147 );
148 assert_eq!(resources.io.last_value, 42);
149 assert_eq!(resources.io.writes, 1);
150 assert_eq!(resources.audit.before_calls, 1);
151 assert_eq!(resources.audit.after_calls, 1);
152 assert_eq!(resources.audit.response_calls, 1);
153 println!(
154 "status={}, value={}, writes={}",
155 response.status, response.value, resources.io.writes
156 );
157}Sourcepub fn with_after<F, Params>(
self,
function: F,
) -> Dispatcher<'table, K, C, R, O, E, M, B, AfterFn<F, Params>, S>where
F: AfterHandler<C, R, O, E, Params>,
pub fn with_after<F, Params>(
self,
function: F,
) -> Dispatcher<'table, K, C, R, O, E, M, B, AfterFn<F, Params>, S>where
F: AfterHandler<C, R, O, E, Params>,
Installs a resource-injected after function.
The function receives mutable access to a successful command output. This does not install a before function.
Examples found in repository?
124fn main() {
125 let mut context = Context {
126 opcode: Opcode::SignAndSend,
127 payload: 40,
128 };
129 let mut resources = Resources {
130 io: Io::default(),
131 crypto: Crypto { key: 2 },
132 audit: Audit::default(),
133 };
134
135 let dispatcher = Dispatcher::new(COMMANDS, KeyMatcher)
136 .with_before(before)
137 .with_after(after)
138 .with_response(response);
139 let response = dispatcher.dispatch(&mut context, &mut resources).unwrap();
140
141 assert_eq!(
142 response,
143 WireResponse {
144 status: 0,
145 value: 42,
146 }
147 );
148 assert_eq!(resources.io.last_value, 42);
149 assert_eq!(resources.io.writes, 1);
150 assert_eq!(resources.audit.before_calls, 1);
151 assert_eq!(resources.audit.after_calls, 1);
152 assert_eq!(resources.audit.response_calls, 1);
153 println!(
154 "status={}, value={}, writes={}",
155 response.status, response.value, resources.io.writes
156 );
157}Sourcepub fn with_response<F, Params, T>(
self,
function: F,
) -> ResponseDispatcher<'table, K, C, R, O, E, M, B, A, F, Params, T>where
F: ResponseHandler<C, R, O, T, E, Params>,
pub fn with_response<F, Params, T>(
self,
function: F,
) -> ResponseDispatcher<'table, K, C, R, O, E, M, B, A, F, Params, T>where
F: ResponseHandler<C, R, O, T, E, Params>,
Installs a resource-injected successful-response function.
The function consumes the command output after the after stage and may convert it to a different final type.
Examples found in repository?
124fn main() {
125 let mut context = Context {
126 opcode: Opcode::SignAndSend,
127 payload: 40,
128 };
129 let mut resources = Resources {
130 io: Io::default(),
131 crypto: Crypto { key: 2 },
132 audit: Audit::default(),
133 };
134
135 let dispatcher = Dispatcher::new(COMMANDS, KeyMatcher)
136 .with_before(before)
137 .with_after(after)
138 .with_response(response);
139 let response = dispatcher.dispatch(&mut context, &mut resources).unwrap();
140
141 assert_eq!(
142 response,
143 WireResponse {
144 status: 0,
145 value: 42,
146 }
147 );
148 assert_eq!(resources.io.last_value, 42);
149 assert_eq!(resources.io.writes, 1);
150 assert_eq!(resources.audit.before_calls, 1);
151 assert_eq!(resources.audit.after_calls, 1);
152 assert_eq!(resources.audit.response_calls, 1);
153 println!(
154 "status={}, value={}, writes={}",
155 response.status, response.value, resources.io.writes
156 );
157}Sourcepub fn select(&self, context: &C) -> Option<&'table CommandEntry<K, C, R, O, E>>where
M: Matcher<K, C>,
pub fn select(&self, context: &C) -> Option<&'table CommandEntry<K, C, R, O, E>>where
M: Matcher<K, C>,
Returns the first command accepted by the matcher.
Sourcepub fn dispatch(
&self,
context: &mut C,
resources: &mut R,
) -> Result<S::Output, DispatchError<E>>
pub fn dispatch( &self, context: &mut C, resources: &mut R, ) -> Result<S::Output, DispatchError<E>>
Selects a command and runs the configured execution pipeline.
Execution stops at the first failure. The after stage runs only after a successful command, and response handling runs only after a successful after stage.
Examples found in repository?
124fn main() {
125 let mut context = Context {
126 opcode: Opcode::SignAndSend,
127 payload: 40,
128 };
129 let mut resources = Resources {
130 io: Io::default(),
131 crypto: Crypto { key: 2 },
132 audit: Audit::default(),
133 };
134
135 let dispatcher = Dispatcher::new(COMMANDS, KeyMatcher)
136 .with_before(before)
137 .with_after(after)
138 .with_response(response);
139 let response = dispatcher.dispatch(&mut context, &mut resources).unwrap();
140
141 assert_eq!(
142 response,
143 WireResponse {
144 status: 0,
145 value: 42,
146 }
147 );
148 assert_eq!(resources.io.last_value, 42);
149 assert_eq!(resources.io.writes, 1);
150 assert_eq!(resources.audit.before_calls, 1);
151 assert_eq!(resources.audit.after_calls, 1);
152 assert_eq!(resources.audit.response_calls, 1);
153 println!(
154 "status={}, value={}, writes={}",
155 response.status, response.value, resources.io.writes
156 );
157}Sourcepub const fn table(&self) -> &'table [CommandEntry<K, C, R, O, E>]
pub const fn table(&self) -> &'table [CommandEntry<K, C, R, O, E>]
Returns the borrowed command table.
Sourcepub fn matcher_mut(&mut self) -> &mut M
pub fn matcher_mut(&mut self) -> &mut M
Returns a mutable matcher reference for between-dispatch configuration.
Sourcepub fn before_mut(&mut self) -> &mut B
pub fn before_mut(&mut self) -> &mut B
Returns a mutable reference to the configured before stage.
Sourcepub fn after_mut(&mut self) -> &mut A
pub fn after_mut(&mut self) -> &mut A
Returns a mutable reference to the configured after stage.
Sourcepub fn response_mut(&mut self) -> &mut S
pub fn response_mut(&mut self) -> &mut S
Returns a mutable reference to the configured response stage.