Struct aopt::ctx::Invoker

source ·
pub struct Invoker<'a, Set, Ser> { /* 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, ASer> for Count {
     type Error = Error;

     fn extract(_set: &ASet, _ser: &ASer, ctx: &Ctx) -> Result<Self, Self::Error> {
         Ok(Self(ctx.args().len()))
     }
 }
 let mut ser = ASer::default();
 let mut is = Invoker::new();
 let mut set = ASet::default();
 let args = ARef::new(Args::from(["--foo", "bar", "doo"]));
 let mut ctx = Ctx::default().with_args(args);

 ser.sve_insert(ser::Value::new(42i64));
 // you can register callback into Invoker
 is.entry(0)
     .on(
         |_set: &mut ASet, _: &mut ASer| -> Result<Option<()>, Error> {
             println!("Calling the handler of {{0}}");
             Ok(None)
         },
     )
     .then(NullStore);
 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(NullStore);
 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(NullStore);

 ctx.set_inner_ctx(Some(InnerCtx::default().with_uid(0)));
 is.invoke(&0, &mut set, &mut ser, &mut ctx)?;

 ctx.set_inner_ctx(Some(InnerCtx::default().with_uid(1)));
 is.invoke(&1, &mut set, &mut ser, &mut ctx)?;

 ctx.set_inner_ctx(Some(InnerCtx::default().with_uid(2)));
 is.invoke(&2, &mut set, &mut ser, &mut ctx)?;

Implementations§

source§

impl<'a, Set, Ser> Invoker<'a, Set, Ser>

source

pub fn new() -> Self

source§

impl<'a, Set, Ser> Invoker<'a, Set, Ser>
where Set: Set,

source

pub fn set_raw<H: FnMut(&mut Set, &mut Ser, &mut Ctx) -> Result<bool, Error> + 'a>( &mut self, uid: Uid, handler: H ) -> &mut Self

source

pub fn set_handler<A, O, H, T>( &mut self, uid: Uid, handler: H, store: T ) -> &mut Self
where O: ErasedTy, A: Extract<Set, Ser, Error = Error> + 'a, T: Store<Set, Ser, O, Ret = bool, Error = Error> + 'a, H: Handler<Set, Ser, A, Output = Option<O>, Error = Error> + 'a,

Register a callback that will called by Policy when option set.

The Invoker first call the invoke, then call the process with the return value.

§Note
|   handler: |&mut Set, &mut Ser, { Other Args }| -> Result<Option<Value>, Error>
|   storer: |&mut Set, &mut Ser, Option<&RawVal>, Option<Value>| -> Result<bool, Error>
        |
     wrapped
        |
        v
|   |&mut Set, &mut Ser, &Ctx| -> Option<Value>
        |
     invoked
        |
        v
|   call Callbacks::invoke(&mut self, &mut Set, &mut Ser, &mut Ctx)
|       call Handler::invoke(&mut self, &mut Set, &mut Ser, Args)
|           call Args::extract(&Set, &Ser, &Ctx) -> Args
|           -> Result<Option<Value>, Error>
|       -> call Store::process(&Set, Option<&RawVal>, Option<Value>)
|           -> Result<bool, Error>
source

pub fn has(&self, uid: Uid) -> bool

source§

impl<'a, Set, Ser> Invoker<'a, Set, Ser>
where SetOpt<Set>: Opt, Set: Set,

source

pub fn entry<A, O, H>( &mut self, uid: Uid ) -> HandlerEntry<'a, '_, Self, Set, Ser, H, A, O>
where O: ErasedTy, H: Handler<Set, Ser, A, Output = Option<O>, Error = Error> + 'a, A: Extract<Set, Ser, Error = Error> + 'a,

source

pub fn fallback( set: &mut Set, _: &mut Ser, ctx: &mut Ctx ) -> Result<bool, 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 save the value to ValStorer.

Trait Implementations§

source§

impl<'a, Set, Ser> Debug for Invoker<'a, Set, Ser>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a, Set, Ser> Default for Invoker<'a, Set, Ser>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<'a, Set, Ser> HandlerCollection<'a, Set, Ser> for Invoker<'a, Set, Ser>
where Set: Set,

source§

fn register<H: FnMut(&mut Set, &mut Ser, &mut Ctx) -> Result<bool, Error> + 'a>( &mut self, uid: Uid, handler: H )

source§

fn get_handler( &mut self, uid: &Uid ) -> Option<&mut InvokeHandler<'a, Set, Ser, Error>>

source§

fn invoke( &mut self, uid: &Uid, set: &mut Set, ser: &mut Ser, ctx: &mut Ctx ) -> Result<bool, Error>

Invoke the handler of given uid, will panic if handler not exist.
source§

fn invoke_fb( &mut self, uid: &Uid, set: &mut Set, ser: &mut Ser, ctx: &mut Ctx ) -> Result<bool, Error>

Invoke the handler of given uid if it exist, otherwise call the fallback.

Auto Trait Implementations§

§

impl<'a, Set, Ser> Freeze for Invoker<'a, Set, Ser>

§

impl<'a, Set, Ser> !RefUnwindSafe for Invoker<'a, Set, Ser>

§

impl<'a, Set, Ser> !Send for Invoker<'a, Set, Ser>

§

impl<'a, Set, Ser> !Sync for Invoker<'a, Set, Ser>

§

impl<'a, Set, Ser> Unpin for Invoker<'a, Set, Ser>

§

impl<'a, Set, Ser> !UnwindSafe for Invoker<'a, Set, Ser>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> ErasedTy for T
where T: Any + 'static,