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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
use std::any::TypeId;
use std::fmt::Debug;
use std::marker::PhantomData;

use crate::opt::Any;
use crate::opt::Cmd;
use crate::opt::ConfigValue;
use crate::opt::Main;
use crate::opt::Pos;
use crate::prelude::ErasedTy;
use crate::set::Ctor;
use crate::set::Set;
use crate::set::SetCfg;
use crate::set::SetExt;
use crate::trace_log;
use crate::value::Infer;
use crate::value::Placeholder;
use crate::value::RawValParser;
use crate::value::ValInitializer;
use crate::value::ValStorer;
use crate::value::ValValidator;
use crate::Error;
use crate::Uid;

use super::Commit;

/// Create option using given configurations.
pub struct SetCommit<'a, S, U>
where
    S: Set,
    U: Infer + 'static,
    U::Val: RawValParser,
    SetCfg<S>: ConfigValue + Default,
{
    info: Option<SetCfg<S>>,
    set: Option<&'a mut S>,
    uid: Option<Uid>,
    pub(crate) drop: bool,
    marker: PhantomData<U>,
}

impl<'a, S, U> Debug for SetCommit<'a, S, U>
where
    S: Set + Debug,
    U: Infer + 'static,
    U::Val: RawValParser,
    SetCfg<S>: ConfigValue + Default + Debug,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SetCommitW")
            .field("info", &self.info)
            .field("set", &self.set)
            .field("uid", &self.uid)
            .field("drop", &self.drop)
            .finish()
    }
}

macro_rules! add_interface {
    ($ty:ty, $name1:ident, $name2:ident) => {
        #[doc = concat!("Set the infer type to [`", stringify!($ty), "`]\\<T\\>.")]
        pub fn $name1<T>(
            self,
        ) -> SetCommit<'a, S, $ty> where T::Val: RawValParser, T: ErasedTy + Infer {
            let type_id = self.cfg().r#type();

            debug_assert!(
                type_id.is_none() || type_id == Some(&TypeId::of::<$ty>()),
                "Can not set value type of {} if it already has one", stringify!($ty),
            );
            self.set_infer::<$ty>()
        }

        #[doc = concat!("Set the infer type to [`", stringify!($ty) ,"`]\\<T\\>, add default initializer and default storer.")]
        ///
        /// The function will call [`add_default_initializer`](SetCommit::add_default_initializer) add
        /// [`add_default_storer`](SetCommit::add_default_storer).
        pub fn $name2<T>(
            self,
        ) -> SetCommit<'a, S, $ty> where T::Val: RawValParser + Clone, T: ErasedTy + Infer {
            let type_id = self.cfg().r#type();

            debug_assert!(
                type_id.is_none() || type_id == Some(&TypeId::of::<$ty>()),
                "Can not set value type of {} if it already has one", stringify!($ty),
            );
            self.set_infer::<$ty>()
                .add_default_initializer()
                .add_default_storer()
        }
    }
}

impl<'a, S> SetCommit<'a, S, Placeholder>
where
    S: Set,
    SetCfg<S>: ConfigValue + Default,
{
    pub fn new_placeholder(set: &'a mut S, info: SetCfg<S>) -> Self {
        Self {
            set: Some(set),
            info: Some(info),
            uid: None,
            drop: true,
            marker: PhantomData,
        }
    }

    add_interface!(Pos<T>, set_pos_type_only, set_pos_type);

    add_interface!(Main<T>, set_main_type_only, set_main_type);

    add_interface!(Any<T>, set_any_type_only, set_any_type);
}

impl<'a, S, U> SetCommit<'a, S, U>
where
    S: Set,
    U: Infer + 'static,
    U::Val: RawValParser,
    SetCfg<S>: ConfigValue + Default,
{
    pub fn new(set: &'a mut S, info: SetCfg<S>) -> Self {
        Self {
            set: Some(set),
            info: Some(info),
            uid: None,
            drop: true,
            marker: PhantomData,
        }
    }

    /// Set the infer type of option.
    pub fn set_infer<O>(mut self) -> SetCommit<'a, S, O>
    where
        O: Infer + 'static,
        O::Val: RawValParser,
    {
        self.drop = false;

        let set = self.set.take();
        let info = self.info.take();
        let info = info.unwrap();

        SetCommit::new(set.unwrap(), info)
    }

    pub(crate) fn commit_change(&mut self) -> Result<Uid, Error> {
        if let Some(uid) = self.uid {
            Ok(uid)
        } else {
            self.drop = false;

            let info = std::mem::take(&mut self.info);
            let mut info = info.unwrap();

            <U as Infer>::infer_fill_info(&mut info)?;

            let set = self.set.as_mut().unwrap();
            let ctor = info
                .ctor()
                .ok_or_else(|| crate::raise_error!("Invalid configuration: missing creator name!"))?
                .clone();

            trace_log!("Register a opt {:?} with creator({})", info.name(), ctor);

            let opt = set.ctor_mut(&ctor)?.new_with(info).map_err(|e| e.into())?;
            let uid = set.insert(opt);

            trace_log!("--> register okay: {uid}");
            self.uid = Some(uid);
            Ok(uid)
        }
    }

    /// Run the commit.
    ///
    /// It create an option using given type [`Ctor`].
    /// And add it to referenced [`Set`](Set), return the new option [`Uid`].
    pub fn run(mut self) -> Result<Uid, Error> {
        self.commit_change()
    }
}

impl<'a, S, U> SetCommit<'a, S, U>
where
    S: Set,
    U: Infer + 'static,
    U::Val: RawValParser,
    SetCfg<S>: ConfigValue + Default,
{
    /// Set the value type of option(except for [`Cmd`]).
    pub fn set_value_type_only<T: ErasedTy>(self) -> SetCommitWithValue<'a, S, U, T> {
        debug_assert!(
            TypeId::of::<U>() != TypeId::of::<Cmd>() || TypeId::of::<T>() == TypeId::of::<bool>(),
            "For Cmd, you can't have other value type!"
        );
        SetCommitWithValue::new(self)
    }

    /// Set the value type of option, add default initializer and default storer.
    ///
    /// The function will call [`add_default_initializer_t`](SetCommitWithValue::add_default_initializer_t) add
    /// [`add_default_storer_t`](SetCommitWithValue::add_default_storer_t).
    pub fn set_value_type<T: ErasedTy + RawValParser + Clone>(
        self,
    ) -> SetCommitWithValue<'a, S, U, T> {
        self.set_value_type_only::<T>()
            .add_default_initializer_t()
            .add_default_storer_t()
    }

    /// Set the option value validator.
    pub fn set_validator_t<T: ErasedTy + RawValParser>(
        self,
        validator: ValValidator<T>,
    ) -> SetCommitWithValue<'a, S, U, T> {
        self.set_value_type_only::<T>().set_validator_t(validator)
    }

    /// Set the option default value.
    pub fn set_value_t<T: ErasedTy + Clone>(self, value: T) -> SetCommitWithValue<'a, S, U, T> {
        self.set_value_type_only::<T>().set_value_t(value)
    }

    /// Set the option default value.
    pub fn set_values_t<T: ErasedTy + Clone>(
        self,
        value: Vec<T>,
    ) -> SetCommitWithValue<'a, S, U, T> {
        self.set_value_type_only::<T>().set_values_t(value)
    }
}

impl<'a, S, U> SetCommit<'a, S, U>
where
    S: Set,
    U: Infer + 'static,
    U::Val: RawValParser,
    SetCfg<S>: ConfigValue + Default,
{
    /// Set the option value validator.
    pub fn set_validator(self, validator: ValValidator<U::Val>) -> Self {
        self.set_storer(ValStorer::from(validator))
    }

    /// Add default [`storer`](ValStorer::fallback) of type [`U::Val`](Infer::Val).
    pub fn add_default_storer(self) -> Self {
        self.set_storer(ValStorer::fallback::<U::Val>())
    }
}

impl<'a, S, U> SetCommit<'a, S, U>
where
    S: Set,
    U: Infer + 'static,
    U::Val: Clone + RawValParser,
    SetCfg<S>: ConfigValue + Default,
{
    /// Set the option default value.
    pub fn set_value(self, value: U::Val) -> Self {
        self.set_initializer(ValInitializer::new_value(value))
    }

    /// Set the option default value.
    pub fn set_values(self, value: Vec<U::Val>) -> Self {
        self.set_initializer(ValInitializer::new_values(value))
    }

    /// Add a default [`initializer`](ValInitializer::fallback).
    pub fn add_default_initializer(self) -> Self {
        self.set_initializer(ValInitializer::fallback())
    }
}

impl<'a, S, U> Commit<S> for SetCommit<'a, S, U>
where
    S: Set,
    U: Infer + 'static,
    U::Val: RawValParser,
    SetCfg<S>: ConfigValue + Default,
{
    fn cfg(&self) -> &SetCfg<S> {
        self.info.as_ref().unwrap()
    }

    fn cfg_mut(&mut self) -> &mut SetCfg<S> {
        self.info.as_mut().unwrap()
    }
}

impl<'a, S, U> Drop for SetCommit<'a, S, U>
where
    S: Set,
    U: Infer + 'static,
    U::Val: RawValParser,
    SetCfg<S>: ConfigValue + Default,
{
    fn drop(&mut self) {
        if self.drop {
            self.commit_change()
                .unwrap_or_else(|e| panic!("catch error in SetCommit::drop: {:?}", e));
        }
    }
}

/// Create option using given configurations.
pub struct SetCommitWithValue<'a, S, U, T>
where
    S: Set,
    U: Infer + 'static,
    T: ErasedTy,
    U::Val: RawValParser,
    SetCfg<S>: ConfigValue + Default,
{
    inner: Option<SetCommit<'a, S, U>>,

    marker: PhantomData<T>,
}

impl<'a, S, U, T> Debug for SetCommitWithValue<'a, S, U, T>
where
    U: Infer + 'static,
    T: ErasedTy,
    S: Set + Debug,
    U::Val: RawValParser,
    SetCfg<S>: ConfigValue + Default + Debug,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SetCommitWithValue")
            .field("inner", &self.inner)
            .field("marker", &self.marker)
            .finish()
    }
}

impl<'a, S, U, T> SetCommitWithValue<'a, S, U, T>
where
    S: Set,
    U: Infer + 'static,
    T: ErasedTy,
    U::Val: RawValParser,
    SetCfg<S>: ConfigValue + Default,
{
    pub fn new(inner: SetCommit<'a, S, U>) -> Self {
        Self {
            inner: Some(inner),
            marker: PhantomData,
        }
    }

    pub fn inner(&self) -> Result<&SetCommit<'a, S, U>, Error> {
        self.inner
            .as_ref()
            .ok_or_else(|| crate::raise_error!("Must set inner data of SetCommitWithValue(ref)"))
    }

    pub fn inner_mut(&mut self) -> Result<&mut SetCommit<'a, S, U>, Error> {
        self.inner
            .as_mut()
            .ok_or_else(|| crate::raise_error!("Must set inner data of SetCommitWithValue(mut)"))
    }

    /// Set the infer type of option.
    pub fn set_infer<O: Infer>(mut self) -> SetCommitWithValue<'a, S, O, T>
    where
        O::Val: RawValParser,
    {
        SetCommitWithValue::new(self.inner.take().unwrap().set_infer::<O>())
    }

    pub(crate) fn commit_inner_change(&mut self) -> Result<Uid, Error> {
        self.inner_mut()?.commit_change()
    }

    /// Run the commit.
    ///
    /// It create an option using given type [`Ctor`].
    /// And add it to referenced [`Set`](Set), return the new option [`Uid`].
    pub fn run(mut self) -> Result<Uid, Error> {
        self.commit_inner_change()
    }
}

impl<'a, S, U, T> SetCommitWithValue<'a, S, U, T>
where
    S: Set,
    U: Infer + 'static,
    U::Val: RawValParser,
    T: ErasedTy + RawValParser,
    SetCfg<S>: ConfigValue + Default,
{
    /// Set the option value validator.
    pub fn set_validator_t(self, validator: ValValidator<T>) -> Self {
        self.set_storer(ValStorer::new_validator(validator))
    }

    /// Add default [`storer`](ValStorer::fallback) of type `T`.
    pub fn add_default_storer_t(self) -> Self {
        self.set_storer(ValStorer::fallback::<T>())
    }
}

impl<'a, S, U, T> SetCommitWithValue<'a, S, U, T>
where
    S: Set,
    T: ErasedTy + Clone,
    U: Infer + 'static,
    U::Val: RawValParser,
    SetCfg<S>: ConfigValue + Default,
{
    /// Set the option default value.
    pub fn set_value_t(self, value: T) -> Self {
        self.set_initializer(ValInitializer::new_value(value))
    }

    /// Set the option default value.
    pub fn set_values_t(self, value: Vec<T>) -> Self {
        self.set_initializer(ValInitializer::new_values(value))
    }

    /// Add a default [`initializer`](ValInitializer::fallback).
    pub fn add_default_initializer_t(self) -> Self {
        self.set_initializer(ValInitializer::fallback())
    }
}

impl<'a, S, U, T> SetCommitWithValue<'a, S, U, T>
where
    S: Set,
    T: ErasedTy,
    U: Infer + 'static,
    U::Val: RawValParser,
    SetCfg<S>: ConfigValue + Default,
{
    /// Set the option value validator.
    pub fn set_validator(self, validator: ValValidator<U::Val>) -> Self {
        self.set_storer(ValStorer::from(validator))
    }
}

impl<'a, S, U, T> SetCommitWithValue<'a, S, U, T>
where
    S: Set,
    T: ErasedTy,
    U: Infer + 'static,
    U::Val: Clone + RawValParser,
    SetCfg<S>: ConfigValue + Default,
{
    /// Set the option default value.
    pub fn set_value(self, value: U::Val) -> Self {
        self.set_initializer(ValInitializer::new_value(value))
    }

    /// Set the option default value.
    pub fn set_values(self, value: Vec<U::Val>) -> Self {
        self.set_initializer(ValInitializer::new_values(value))
    }
}

impl<'a, S, U, T> Commit<S> for SetCommitWithValue<'a, S, U, T>
where
    S: Set,
    T: ErasedTy,
    U: Infer + 'static,
    U::Val: RawValParser,
    SetCfg<S>: ConfigValue + Default,
{
    fn cfg(&self) -> &SetCfg<S> {
        self.inner().unwrap().cfg()
    }

    fn cfg_mut(&mut self) -> &mut SetCfg<S> {
        self.inner_mut().unwrap().cfg_mut()
    }
}