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§

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>

Invoke the handler saved in InvokeService, it will panic if the handler not exist.

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.

Trait Implementations§

Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more