1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
use std::fmt::Debug;
use std::marker::PhantomData;
use std::path::PathBuf;
use tracing::trace;

use crate::astr;
use crate::ctx::wrap_handler;
use crate::ctx::wrap_handler_action;
use crate::ctx::wrap_handler_fallback;
use crate::ctx::Ctx;
use crate::ctx::Extract;
use crate::ctx::Handler;
use crate::ctx::Store;
use crate::opt::Assoc;
use crate::opt::Opt;
use crate::opt::RawValParser;
use crate::ser::Service;
use crate::ser::Services;
use crate::set::Ctor;
use crate::set::Set;
use crate::set::SetOpt;
use crate::Error;
use crate::HashMap;
use crate::Str;
use crate::Uid;

/// Keep the variable length arguments handler in [`HashMap`] with key [`Uid`].
///
/// # Example
/// ```rust
/// # use aopt::prelude::*;
/// # use aopt::Error;
/// # use aopt::Arc;
/// # use aopt::ext::ServicesExt;
/// # use aopt::RawVal;
/// # use std::ops::Deref;
/// #
/// # fn main() -> Result<(), Error> {
///    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)?;
/// #
/// #   Ok(())
/// # }
/// ```
pub struct InvokeService<S: Set> {
    callbacks: HashMap<Uid, InvokeHandler<S, Error>>,
}

pub type InvokeHandler<S, E> = Box<dyn FnMut(&mut S, &mut Services, &Ctx) -> Result<Option<()>, E>>;

impl<S: Set> Debug for InvokeService<S> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("InvokeService")
            .field("callbacks", &"{ ... }")
            .finish()
    }
}

impl<S: Set> Default for InvokeService<S> {
    fn default() -> Self {
        Self {
            callbacks: HashMap::default(),
        }
    }
}

impl<S: Set> InvokeService<S> {
    pub fn new() -> Self {
        Self {
            callbacks: HashMap::default(),
        }
    }
}

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 {
        self.callbacks.insert(uid, Box::new(handler));
        self
    }

    /// Register a callback that will called by [`Policy`](crate::parser::Policy) when option setted.
    ///
    /// The [`InvokeService`] first call the [`invoke`](crate::ctx::Handler::invoke), then
    /// call the [`process`](crate::ctx::Store::process) with the return value.
    /// # Note
    /// ```txt
    /// |   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 set_handler<A, O, H, T>(&mut self, uid: Uid, handler: H, store: T) -> &mut Self
    where
        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,
    {
        self.set_raw(uid, wrap_handler(handler, store));
        self
    }

    pub fn has(&self, uid: Uid) -> bool {
        self.callbacks.contains_key(&uid)
    }

    /// Invoke the handler saved in [`InvokeService`], it will panic if the handler not exist.
    pub fn invoke(
        &mut self,
        set: &mut S,
        ser: &mut Services,
        ctx: &Ctx,
    ) -> Result<Option<()>, Error> {
        let uid = ctx.uid();
        if let Some(callback) = self.callbacks.get_mut(&uid) {
            return (callback)(set, ser, ctx);
        }
        unreachable!(
            "There is no callback of {}, call `invoke_default` instead",
            uid
        )
    }
}

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,
    {
        HandlerEntry::new(self, uid)
    }

    /// The default handler for all option.
    ///
    /// If there no handler for a option, then default handler will be called.
    /// It will parsing [`RawVal`](crate::RawVal)(using [`RawValParser`]) into associated type, then call the action
    /// of option save the value to [`ValService`](crate::ser::ValService).
    /// For value type, reference documents of [`Assoc`].
    pub fn fallback(set: &mut S, ser: &mut Services, ctx: &Ctx) -> Result<Option<()>, Error> {
        let uid = ctx.uid();
        let opt = set.get(uid).unwrap();
        let assoc = opt.assoc();
        let arg = ctx.arg();
        let val = arg.as_ref().map(|v| v.as_ref());
        let mut action = *opt.action();

        trace!(
            "Invoke default handler for {{{uid}}}, ctx{{{ctx:?}}} action{{{}}} & assoc{{{}}}",
            action,
            assoc
        );
        match assoc {
            Assoc::Bool => action.process(uid, set, ser, val, bool::parse(opt, val, ctx).ok()),
            Assoc::Int => action.process(uid, set, ser, val, i64::parse(opt, val, ctx).ok()),
            Assoc::Int128 => action.process(uid, set, ser, val, i128::parse(opt, val, ctx).ok()),
            Assoc::ISize => action.process(uid, set, ser, val, isize::parse(opt, val, ctx).ok()),
            Assoc::Int64 => action.process(uid, set, ser, val, i64::parse(opt, val, ctx).ok()),
            Assoc::Int32 => action.process(uid, set, ser, val, i32::parse(opt, val, ctx).ok()),
            Assoc::Int16 => action.process(uid, set, ser, val, i16::parse(opt, val, ctx).ok()),
            Assoc::Int8 => action.process(uid, set, ser, val, i8::parse(opt, val, ctx).ok()),
            Assoc::Uint => action.process(uid, set, ser, val, u64::parse(opt, val, ctx).ok()),
            Assoc::Uint128 => action.process(uid, set, ser, val, u128::parse(opt, val, ctx).ok()),
            Assoc::USize => action.process(uid, set, ser, val, usize::parse(opt, val, ctx).ok()),
            Assoc::Uint64 => action.process(uid, set, ser, val, u64::parse(opt, val, ctx).ok()),
            Assoc::Uint32 => action.process(uid, set, ser, val, u32::parse(opt, val, ctx).ok()),
            Assoc::Uint16 => action.process(uid, set, ser, val, u16::parse(opt, val, ctx).ok()),
            Assoc::Uint8 => action.process(uid, set, ser, val, u8::parse(opt, val, ctx).ok()),
            Assoc::Flt => action.process(uid, set, ser, val, f64::parse(opt, val, ctx).ok()),
            Assoc::Flt64 => action.process(uid, set, ser, val, f64::parse(opt, val, ctx).ok()),
            Assoc::Flt32 => action.process(uid, set, ser, val, f32::parse(opt, val, ctx).ok()),
            Assoc::Str => action.process(uid, set, ser, val, String::parse(opt, val, ctx).ok()),
            Assoc::Noa => action.process(uid, set, ser, val, val.map(|_| true)),
            Assoc::Path => action.process(uid, set, ser, val, PathBuf::parse(opt, val, ctx).ok()),
            Assoc::Null => Ok(Some(())),
        }
    }

    pub fn invoke_default(
        &mut self,
        set: &mut S,
        ser: &mut Services,
        ctx: &Ctx,
    ) -> Result<Option<()>, Error> {
        Self::fallback(set, ser, ctx)
    }
}

impl<S: Set> Service for InvokeService<S> {
    fn service_name() -> Str {
        astr("InvokeService")
    }
}

pub struct HandlerEntry<'a, S, H, A, O>
where
    O: 'static,
    S: Set + 'static,
    SetOpt<S>: Opt,
    H: Handler<S, A, Output = Option<O>, Error = Error> + 'static,
    A: Extract<S, Error = Error> + 'static,
{
    ser: &'a mut InvokeService<S>,

    handler: Option<H>,

    register: bool,

    uid: Uid,

    marker: PhantomData<(A, O)>,
}

impl<'a, A, S, O, H> HandlerEntry<'a, S, H, A, O>
where
    O: 'static,
    S: Set + 'static,
    SetOpt<S>: Opt,
    H: Handler<S, A, Output = Option<O>, Error = Error> + 'static,
    A: Extract<S, Error = Error> + 'static,
{
    pub fn new(inv_ser: &'a mut InvokeService<S>, uid: Uid) -> Self {
        Self {
            ser: inv_ser,
            handler: None,
            register: false,
            uid,
            marker: PhantomData::default(),
        }
    }

    /// Register the handler which will be called when option is set.
    pub fn on(mut self, handler: H) -> Self {
        self.handler = Some(handler);
        self
    }

    /// Register the handler which will be called when option is set.
    /// And the [`fallback`](crate::ser::InvokeService::fallback) will be called if
    /// the handler return None.
    pub fn fallback(mut self, handler: H) -> Self {
        if !self.register {
            self.ser.set_raw(self.uid, wrap_handler_fallback(handler));
            self.register = true;
        }
        self
    }

    /// Register the handler with given store.
    /// The store will be used save the return value of option handler.
    pub fn then(mut self, store: impl Store<S, O, Ret = (), Error = Error> + 'static) -> Self {
        if !self.register {
            if let Some(handler) = self.handler.take() {
                self.ser.set_raw(self.uid, wrap_handler(handler, store));
            }
            self.register = true;
        }
        self
    }

    pub fn submit(mut self) -> Uid {
        if !self.register {
            if let Some(handler) = self.handler.take() {
                self.ser.set_raw(self.uid, wrap_handler_action(handler));
            }
            self.register = true;
        }
        self.uid
    }
}

impl<'a, S, H, A, O> Drop for HandlerEntry<'a, S, H, A, O>
where
    O: 'static,
    S: Set + 'static,
    SetOpt<S>: Opt,
    H: Handler<S, A, Output = Option<O>, Error = Error> + 'static,
    A: Extract<S, Error = Error> + 'static,
{
    fn drop(&mut self) {
        if !self.register {
            if let Some(handler) = self.handler.take() {
                self.ser.set_raw(self.uid, wrap_handler_action(handler));
            }
            self.register = true;
        }
    }
}