Struct aopt::ser::InvokeService
source · pub struct InvokeService<S: Set> { /* private fields */ }Expand description
Keep the variable length arguments handler in HashMap with key Uid.
Example
pub struct Count(usize);
// implement Extract for your type
impl Extract<ASet> for Count {
type Error = Error;
fn extract(_set: &ASet, _ser: &Services, ctx: &Ctx) -> Result<Self, Self::Error> {
Ok(Self(ctx.args().len()))
}
}
let mut ser = Services::default().with(UsrValService::default());
let mut is = InvokeService::new();
let mut set = ASet::default();
let args = Arc::new(Args::new(["--foo", "bar", "doo"].into_iter()));
let ctx = Ctx::default().with_args(args);
ser.ser_usrval_mut()?.insert(ser::Value::new(42i64));
// you can register callback into InvokeService
is.entry(0)
.on(|_set: &mut ASet, _: &mut ASer| -> Result<Option<()>, Error> {
println!("Calling the handler of {{0}}");
Ok(None)
},
).then(Action::Null);
is.entry(1)
.on(|_set: &mut ASet, _: &mut ASer, cnt: Count| -> Result<Option<()>, Error> {
println!("Calling the handler of {{1}}");
assert_eq!(cnt.0, 3);
Ok(None)
},
).then(Action::Null);
is.entry(2)
.on(|_set: &mut ASet, _: &mut ASer, data: ser::Value<i64>| -> Result<Option<()>, Error> {
println!("Calling the handler of {{2}}");
assert_eq!(data.as_ref(), &42);
Ok(None)
},
).then(Action::Null);
is.invoke(&mut set, &mut ser, &ctx)?;
is.invoke(&mut set, &mut ser, &ctx)?;
is.invoke(&mut set, &mut ser, &ctx)?;Implementations§
source§impl<S: Set + 'static> InvokeService<S>
impl<S: Set + 'static> InvokeService<S>
pub fn set_raw<H: FnMut(&mut S, &mut Services, &Ctx) -> Result<Option<()>, Error> + 'static>(
&mut self,
uid: Uid,
handler: H
) -> &mut Self
sourcepub fn set_handler<A, O, H, T>(
&mut self,
uid: Uid,
handler: H,
store: T
) -> &mut Selfwhere
O: 'static,
A: Extract<S, Error = Error> + 'static,
T: Store<S, O, Ret = (), Error = Error> + 'static,
H: Handler<S, A, Output = Option<O>, Error = Error> + 'static,
pub fn set_handler<A, O, H, T>(
&mut self,
uid: Uid,
handler: H,
store: T
) -> &mut Selfwhere
O: 'static,
A: Extract<S, Error = Error> + 'static,
T: Store<S, O, Ret = (), Error = Error> + 'static,
H: Handler<S, A, Output = Option<O>, Error = Error> + 'static,
Register a callback that will called by Policy when option setted.
The InvokeService first call the invoke, then
call the process with the return value.
Note
| handler: |&mut Set, &mut Services, { Other Args }| -> Result<Option<Value>, Error>
| storer: |&mut Set, &mut Services, Option<&RawVal>, Option<Value>| -> Result<Option<()>, Error>
|
wrapped
|
v
| |&mut Set, &mut Services, &Ctx| -> Option<Value>
|
invoked
|
v
| call Callbacks::invoke(&mut self, &mut Set, &mut Services, &Ctx)
| call Handler::invoke(&mut self, &mut Set, &mut Services, Args)
| call Args::extract(&Set, &Services, &Ctx) -> Args
| -> Result<Option<Value>, Error>
| -> call Store::process(&Set, Option<&RawVal>, Option<Value>)
| -> Result<Option<()>, Error>
pub fn has(&self, uid: Uid) -> bool
source§impl<S> InvokeService<S>where
S: Set,
<S::Ctor as Ctor>::Opt: Opt,
impl<S> InvokeService<S>where
S: Set,
<S::Ctor as Ctor>::Opt: Opt,
pub fn entry<A, O, H>(&mut self, uid: Uid) -> HandlerEntry<'_, S, H, A, O>where
O: 'static,
H: Handler<S, A, Output = Option<O>, Error = Error> + 'static,
A: Extract<S, Error = Error> + 'static,
sourcepub fn fallback(
set: &mut S,
ser: &mut Services,
ctx: &Ctx
) -> Result<Option<()>, Error>
pub fn fallback(
set: &mut S,
ser: &mut Services,
ctx: &Ctx
) -> Result<Option<()>, Error>
The default handler for all option.
If there no handler for a option, then default handler will be called.
It will parsing RawVal(using RawValParser) into associated type, then call the action
of option save the value to ValService.
For value type, reference documents of Assoc.