1use std::any::TypeId;
2use std::fmt::Debug;
3use std::marker::PhantomData;
4
5use crate::err::Error;
6use crate::opt::Action;
7use crate::opt::Index;
8use crate::opt::Information;
9use crate::opt::OptParser;
10use crate::typeid;
11use crate::value::Placeholder;
12use crate::value::ValInitializer;
13use crate::value::ValStorer;
14
15use super::Cid;
16use super::Style;
17
18pub trait ConfigBuild<C> {
19 type Val;
20
21 fn build<P>(self, parser: &P) -> Result<C, Error>
22 where
23 P: OptParser,
24 P::Output: Information;
25}
26
27impl<C: ConfigValue + Default> ConfigBuild<C> for &'_ str {
28 type Val = Placeholder;
29
30 fn build<P>(self, parser: &P) -> Result<C, Error>
31 where
32 P: OptParser,
33 P::Output: Information,
34 {
35 let mut output = parser.parse_opt(self).map_err(Into::into)?;
36 let mut ret = C::default();
37
38 if let Some(v) = output.take_name() {
39 ret.set_name(v);
40 }
41 if let Some(v) = output.take_index() {
42 ret.set_index(v);
43 }
44 if let Some(v) = output.take_force() {
45 ret.set_force(v);
46 }
47 if let Some(v) = output.take_help() {
48 ret.set_help(v);
49 }
50 if let Some(v) = output.take_ctor() {
51 ret.set_ctor(v);
52 }
53 if let Some(v) = output.take_alias() {
54 ret.set_alias(v);
55 }
56 Ok(ret)
57 }
58}
59
60impl<C: ConfigValue + Default> ConfigBuild<C> for String {
61 type Val = Placeholder;
62
63 fn build<P>(self, parser: &P) -> Result<C, Error>
64 where
65 P: OptParser,
66 P::Output: Information,
67 {
68 <&str as ConfigBuild<C>>::build(self.as_str(), parser)
69 }
70}
71
72impl<C: ConfigValue + Default> ConfigBuild<C> for &'_ String {
73 type Val = Placeholder;
74
75 fn build<P>(self, parser: &P) -> Result<C, Error>
76 where
77 P: OptParser,
78 P::Output: Information,
79 {
80 <&str as ConfigBuild<C>>::build(self.as_str(), parser)
81 }
82}
83
84impl<C: ConfigValue, I: 'static> ConfigBuild<C> for ConfigBuilder<C, I> {
85 type Val = I;
86
87 fn build<P>(self, _: &P) -> Result<C, Error>
88 where
89 P: OptParser,
90 P::Output: Information,
91 {
92 Ok(self.config.with_type::<I>())
93 }
94}
95
96impl<C, T, I> ConfigBuild<C> for ConfigBuilderWith<C, T, I>
97where
98 I: 'static,
99 T: ConfigBuild<C>,
100 C: ConfigValue + Default,
101{
102 type Val = I;
103
104 fn build<P>(self, parser: &P) -> Result<C, Error>
105 where
106 P: OptParser,
107 P::Output: Information,
108 {
109 let mut init = self.init.build(parser)?;
110 let mut config = self.inner.build(parser)?;
111
112 macro_rules! merge {
113 ($has:ident, $set:ident, $take:ident) => {
114 if !config.$has() {
115 if let Some(value) = init.$take() {
116 config.$set(value);
117 }
118 }
119 };
120 }
121 merge!(has_ctor, set_ctor, take_ctor);
122 merge!(has_type, set_type_id, take_type);
123 merge!(has_name, set_name, take_name);
124 merge!(has_force, set_force, take_force);
125 merge!(has_index, set_index, take_index);
126 merge!(has_alias, set_alias, take_alias);
127 merge!(has_hint, set_hint, take_hint);
128 merge!(has_help, set_help, take_help);
129 merge!(has_action, set_action, take_action);
130 merge!(has_storer, set_storer, take_storer);
131 merge!(has_style, set_style, take_style);
132 merge!(has_initializer, set_initializer, take_initializer);
133 config.set_ignore_name(config.ignore_name() || init.ignore_name());
134 config.set_ignore_alias(config.ignore_alias() || init.ignore_alias());
135 config.set_ignore_index(config.ignore_index() || init.ignore_index());
136 Ok(config)
137 }
138}
139
140pub trait ConfigValue {
141 fn ctor(&self) -> Option<&str>;
143
144 fn r#type(&self) -> Option<&TypeId>;
146
147 fn name(&self) -> Option<&str>;
149
150 fn force(&self) -> Option<bool>;
152
153 fn index(&self) -> Option<&Index>;
155
156 fn alias(&self) -> Option<&Vec<String>>;
158
159 fn hint(&self) -> Option<&str>;
161
162 fn help(&self) -> Option<&str>;
164
165 fn action(&self) -> Option<&Action>;
167
168 fn storer(&self) -> Option<&ValStorer>;
170
171 fn style(&self) -> Option<&Vec<Style>>;
173
174 fn initializer(&self) -> Option<&ValInitializer>;
176
177 fn ctor_mut(&mut self) -> Option<&mut String>;
179
180 fn type_mut(&mut self) -> Option<&mut TypeId>;
182
183 fn name_mut(&mut self) -> Option<&mut String>;
185
186 fn force_mut(&mut self) -> Option<&mut bool>;
188
189 fn index_mut(&mut self) -> Option<&mut Index>;
191
192 fn alias_mut(&mut self) -> Option<&mut Vec<String>>;
194
195 fn hint_mut(&mut self) -> Option<&mut String>;
197
198 fn help_mut(&mut self) -> Option<&mut String>;
200
201 fn action_mut(&mut self) -> Option<&mut Action>;
203
204 fn storer_mut(&mut self) -> Option<&mut ValStorer>;
206
207 fn style_mut(&mut self) -> Option<&mut Vec<Style>>;
209
210 fn initializer_mut(&mut self) -> Option<&mut ValInitializer>;
212
213 fn ignore_name(&self) -> bool;
214
215 fn ignore_alias(&self) -> bool;
216
217 fn ignore_index(&self) -> bool;
218
219 fn has_ctor(&self) -> bool;
220
221 fn has_type(&self) -> bool;
222
223 fn has_name(&self) -> bool;
224
225 fn has_force(&self) -> bool;
226
227 fn has_index(&self) -> bool;
228
229 fn has_hint(&self) -> bool;
230
231 fn has_help(&self) -> bool;
232
233 fn has_alias(&self) -> bool;
234
235 fn has_action(&self) -> bool;
236
237 fn has_storer(&self) -> bool;
238
239 fn has_style(&self) -> bool;
240
241 fn has_initializer(&self) -> bool;
242
243 fn set_ctor(&mut self, ctor: impl Into<String>) -> &mut Self;
244
245 fn set_type<T: 'static>(&mut self) -> &mut Self;
246
247 fn set_type_id(&mut self, type_id: TypeId) -> &mut Self;
248
249 fn set_name(&mut self, name: impl Into<String>) -> &mut Self;
250
251 fn set_force(&mut self, force: bool) -> &mut Self;
252
253 fn set_index(&mut self, index: Index) -> &mut Self;
254
255 fn set_alias(&mut self, alias: Vec<impl Into<String>>) -> &mut Self;
256
257 fn clr_alias(&mut self) -> &mut Self;
258
259 fn add_alias(&mut self, alias: impl Into<String>) -> &mut Self;
260
261 fn rem_alias(&mut self, alias: impl AsRef<str>) -> &mut Self;
262
263 fn set_hint(&mut self, hint: impl Into<String>) -> &mut Self;
264
265 fn set_help(&mut self, help: impl Into<String>) -> &mut Self;
266
267 fn set_action(&mut self, action: Action) -> &mut Self;
268
269 fn set_storer(&mut self, storer: ValStorer) -> &mut Self;
270
271 fn set_style(&mut self, styles: Vec<Style>) -> &mut Self;
272
273 fn set_initializer(&mut self, initializer: ValInitializer) -> &mut Self;
274
275 fn set_ignore_name(&mut self, ignore_name: bool) -> &mut Self;
276
277 fn set_ignore_alias(&mut self, ignore_alias: bool) -> &mut Self;
278
279 fn set_ignore_index(&mut self, ignore_index: bool) -> &mut Self;
280
281 fn take_ctor(&mut self) -> Option<String>;
282
283 fn take_type(&mut self) -> Option<TypeId>;
284
285 fn take_name(&mut self) -> Option<String>;
286
287 fn take_force(&mut self) -> Option<bool>;
288
289 fn take_index(&mut self) -> Option<Index>;
290
291 fn take_alias(&mut self) -> Option<Vec<String>>;
292
293 fn take_hint(&mut self) -> Option<String>;
294
295 fn take_help(&mut self) -> Option<String>;
296
297 fn take_action(&mut self) -> Option<Action>;
298
299 fn take_storer(&mut self) -> Option<ValStorer>;
300
301 fn take_style(&mut self) -> Option<Vec<Style>>;
302
303 fn take_initializer(&mut self) -> Option<ValInitializer>;
304
305 fn infer_builtin_ty(&mut self);
306
307 fn with_index(self, index: Index) -> Self;
308
309 fn with_force(self, force: bool) -> Self;
310
311 fn with_ctor(self, ctor: impl Into<String>) -> Self;
312
313 fn with_name(self, name: impl Into<String>) -> Self;
314
315 fn with_type<T: 'static>(self) -> Self;
316
317 fn with_hint(self, hint: impl Into<String>) -> Self;
318
319 fn with_help(self, help: impl Into<String>) -> Self;
320
321 fn with_alias(self, alias: Vec<impl Into<String>>) -> Self;
322
323 fn with_style(self, styles: Vec<Style>) -> Self;
324
325 fn with_action(self, action: Action) -> Self;
326
327 fn with_storer(self, storer: ValStorer) -> Self;
328
329 fn with_ignore_alias(self, ignore_alias: bool) -> Self;
330
331 fn with_ignore_index(self, ignore_index: bool) -> Self;
332
333 fn with_ignore_name(self, ignore_name: bool) -> Self;
334
335 fn with_initializer(self, initializer: ValInitializer) -> Self;
336}
337
338#[derive(Debug, Default)]
340pub struct OptConfig {
341 ctor: Option<String>,
342
343 r#type: Option<TypeId>,
344
345 name: Option<String>,
346
347 force: Option<bool>,
348
349 index: Option<Index>,
350
351 alias: Option<Vec<String>>,
352
353 hint: Option<String>,
354
355 help: Option<String>,
356
357 action: Option<Action>,
358
359 storer: Option<ValStorer>,
360
361 initializer: Option<ValInitializer>,
362
363 ignore_name: bool,
364
365 ignore_alias: bool,
366
367 ignore_index: bool,
368
369 styles: Option<Vec<Style>>,
370}
371
372impl ConfigValue for OptConfig {
373 fn ctor(&self) -> Option<&str> {
374 self.ctor.as_deref()
375 }
376
377 fn r#type(&self) -> Option<&TypeId> {
378 self.r#type.as_ref()
379 }
380
381 fn name(&self) -> Option<&str> {
382 self.name.as_deref()
383 }
384
385 fn force(&self) -> Option<bool> {
386 self.force
387 }
388
389 fn index(&self) -> Option<&Index> {
390 self.index.as_ref()
391 }
392
393 fn alias(&self) -> Option<&Vec<String>> {
394 self.alias.as_ref()
395 }
396
397 fn hint(&self) -> Option<&str> {
398 self.help.as_deref()
399 }
400
401 fn help(&self) -> Option<&str> {
402 self.help.as_deref()
403 }
404
405 fn action(&self) -> Option<&Action> {
406 self.action.as_ref()
407 }
408
409 fn storer(&self) -> Option<&ValStorer> {
410 self.storer.as_ref()
411 }
412
413 fn style(&self) -> Option<&Vec<Style>> {
414 self.styles.as_ref()
415 }
416
417 fn initializer(&self) -> Option<&ValInitializer> {
418 self.initializer.as_ref()
419 }
420
421 fn ctor_mut(&mut self) -> Option<&mut String> {
422 self.ctor.as_mut()
423 }
424
425 fn type_mut(&mut self) -> Option<&mut TypeId> {
426 self.r#type.as_mut()
427 }
428
429 fn name_mut(&mut self) -> Option<&mut String> {
430 self.name.as_mut()
431 }
432
433 fn force_mut(&mut self) -> Option<&mut bool> {
434 self.force.as_mut()
435 }
436
437 fn index_mut(&mut self) -> Option<&mut Index> {
438 self.index.as_mut()
439 }
440
441 fn alias_mut(&mut self) -> Option<&mut Vec<String>> {
442 self.alias.as_mut()
443 }
444
445 fn hint_mut(&mut self) -> Option<&mut String> {
446 self.hint.as_mut()
447 }
448
449 fn help_mut(&mut self) -> Option<&mut String> {
450 self.help.as_mut()
451 }
452
453 fn action_mut(&mut self) -> Option<&mut Action> {
454 self.action.as_mut()
455 }
456
457 fn storer_mut(&mut self) -> Option<&mut ValStorer> {
458 self.storer.as_mut()
459 }
460
461 fn style_mut(&mut self) -> Option<&mut Vec<Style>> {
462 self.styles.as_mut()
463 }
464
465 fn initializer_mut(&mut self) -> Option<&mut ValInitializer> {
466 self.initializer.as_mut()
467 }
468
469 fn ignore_name(&self) -> bool {
470 self.ignore_name
471 }
472
473 fn ignore_alias(&self) -> bool {
474 self.ignore_alias
475 }
476
477 fn ignore_index(&self) -> bool {
478 self.ignore_index
479 }
480
481 fn has_ctor(&self) -> bool {
482 self.ctor.is_some()
483 }
484
485 fn has_type(&self) -> bool {
486 self.r#type.is_some()
487 }
488
489 fn has_name(&self) -> bool {
490 self.name.is_some()
491 }
492
493 fn has_force(&self) -> bool {
494 self.force.is_some()
495 }
496
497 fn has_index(&self) -> bool {
498 self.index.is_some()
499 }
500
501 fn has_hint(&self) -> bool {
502 self.hint.is_some()
503 }
504
505 fn has_help(&self) -> bool {
506 self.help.is_some()
507 }
508
509 fn has_alias(&self) -> bool {
510 self.alias.is_some()
511 }
512
513 fn has_action(&self) -> bool {
514 self.action.is_some()
515 }
516
517 fn has_storer(&self) -> bool {
518 self.storer.is_some()
519 }
520
521 fn has_style(&self) -> bool {
522 self.styles.is_some()
523 }
524
525 fn has_initializer(&self) -> bool {
526 self.initializer.is_some()
527 }
528
529 fn set_ctor(&mut self, ctor: impl Into<String>) -> &mut Self {
530 self.ctor = Some(ctor.into());
531 self
532 }
533
534 fn set_type<T: 'static>(&mut self) -> &mut Self {
535 self.r#type = Some(typeid::<T>());
536 self
537 }
538
539 fn set_type_id(&mut self, type_id: TypeId) -> &mut Self {
540 self.r#type = Some(type_id);
541 self
542 }
543
544 fn set_name(&mut self, name: impl Into<String>) -> &mut Self {
545 self.name = Some(name.into());
546 self
547 }
548
549 fn set_force(&mut self, force: bool) -> &mut Self {
550 self.force = Some(force);
551 self
552 }
553
554 fn set_index(&mut self, index: Index) -> &mut Self {
555 self.index = Some(index);
556 self
557 }
558
559 fn set_alias(&mut self, alias: Vec<impl Into<String>>) -> &mut Self {
560 self.alias = Some(alias.into_iter().map(Into::into).collect::<Vec<_>>());
561 self
562 }
563
564 fn clr_alias(&mut self) -> &mut Self {
565 if let Some(alias) = self.alias.as_mut() {
566 alias.clear();
567 }
568 self
569 }
570
571 fn add_alias(&mut self, alias: impl Into<String>) -> &mut Self {
572 self.alias.get_or_insert(vec![]).push(alias.into());
573 self
574 }
575
576 fn rem_alias(&mut self, alias: impl AsRef<str>) -> &mut Self {
577 let alias = alias.as_ref();
578
579 if let Some(v) = self.alias.as_mut() {
580 for (index, value) in v.iter().enumerate() {
581 if value == alias {
582 v.remove(index);
583 break;
584 }
585 }
586 }
587 self
588 }
589
590 fn set_hint(&mut self, hint: impl Into<String>) -> &mut Self {
591 self.hint = Some(hint.into());
592 self
593 }
594
595 fn set_help(&mut self, help: impl Into<String>) -> &mut Self {
596 self.help = Some(help.into());
597 self
598 }
599
600 fn set_action(&mut self, action: Action) -> &mut Self {
601 self.action = Some(action);
602 self
603 }
604
605 fn set_storer(&mut self, storer: ValStorer) -> &mut Self {
606 self.storer = Some(storer);
607 self
608 }
609
610 fn set_style(&mut self, styles: Vec<Style>) -> &mut Self {
611 self.styles = Some(styles);
612 self
613 }
614
615 fn set_initializer(&mut self, initializer: ValInitializer) -> &mut Self {
616 self.initializer = Some(initializer);
617 self
618 }
619
620 fn set_ignore_name(&mut self, ignore_name: bool) -> &mut Self {
621 self.ignore_name = ignore_name;
622 self
623 }
624
625 fn set_ignore_alias(&mut self, ignore_alias: bool) -> &mut Self {
626 self.ignore_alias = ignore_alias;
627 self
628 }
629
630 fn set_ignore_index(&mut self, ignore_index: bool) -> &mut Self {
631 self.ignore_index = ignore_index;
632 self
633 }
634
635 fn take_ctor(&mut self) -> Option<String> {
636 self.ctor.take()
637 }
638
639 fn take_type(&mut self) -> Option<TypeId> {
640 self.r#type.take()
641 }
642
643 fn take_name(&mut self) -> Option<String> {
644 self.name.take()
645 }
646
647 fn take_force(&mut self) -> Option<bool> {
648 self.force.take()
649 }
650
651 fn take_index(&mut self) -> Option<Index> {
652 self.index.take()
653 }
654
655 fn take_alias(&mut self) -> Option<Vec<String>> {
656 self.alias.take()
657 }
658
659 fn take_hint(&mut self) -> Option<String> {
660 self.hint.take()
661 }
662
663 fn take_help(&mut self) -> Option<String> {
664 self.help.take()
665 }
666
667 fn take_action(&mut self) -> Option<Action> {
668 self.action.take()
669 }
670
671 fn take_storer(&mut self) -> Option<ValStorer> {
672 self.storer.take()
673 }
674
675 fn take_style(&mut self) -> Option<Vec<Style>> {
676 self.styles.take()
677 }
678
679 fn take_initializer(&mut self) -> Option<ValInitializer> {
680 self.initializer.take()
681 }
682
683 fn infer_builtin_ty(&mut self) {
684 if let Some(ctor) = self.ctor() {
685 let cid = Cid::from(ctor);
686
687 self.set_type_id(match cid {
688 Cid::Int => typeid::<i64>(),
689 Cid::Str => typeid::<String>(),
690 Cid::Flt => typeid::<f64>(),
691 Cid::Uint => typeid::<u64>(),
692 Cid::Bool => typeid::<bool>(),
693 Cid::Cmd => typeid::<crate::opt::Cmd>(),
694 Cid::Pos => typeid::<crate::opt::Pos<bool>>(),
695 Cid::Main => typeid::<crate::opt::Main>(),
696 Cid::Any => typeid::<crate::opt::AnyOpt>(),
697 Cid::Raw => typeid::<std::ffi::OsString>(),
698 _ => {
699 unreachable!("creator `{ctor}` can't infer any type")
700 }
701 });
702 }
703 }
704
705 fn with_index(mut self, index: Index) -> Self {
706 self.index = Some(index);
707 self
708 }
709
710 fn with_force(mut self, force: bool) -> Self {
711 self.force = Some(force);
712 self
713 }
714
715 fn with_ctor(mut self, ctor: impl Into<String>) -> Self {
716 self.ctor = Some(ctor.into());
717 self
718 }
719
720 fn with_name(mut self, name: impl Into<String>) -> Self {
721 self.name = Some(name.into());
722 self
723 }
724
725 fn with_type<T: 'static>(mut self) -> Self {
726 self.r#type = Some(typeid::<T>());
727 self
728 }
729
730 fn with_hint(mut self, hint: impl Into<String>) -> Self {
731 self.help = Some(hint.into());
732 self
733 }
734
735 fn with_help(mut self, help: impl Into<String>) -> Self {
736 self.help = Some(help.into());
737 self
738 }
739
740 fn with_alias(mut self, alias: Vec<impl Into<String>>) -> Self {
741 self.alias = Some(alias.into_iter().map(|v| v.into()).collect());
742 self
743 }
744
745 fn with_style(mut self, styles: Vec<Style>) -> Self {
746 self.styles = Some(styles);
747 self
748 }
749
750 fn with_action(mut self, action: Action) -> Self {
751 self.action = Some(action);
752 self
753 }
754
755 fn with_storer(mut self, storer: ValStorer) -> Self {
756 self.storer = Some(storer);
757 self
758 }
759
760 fn with_ignore_alias(mut self, ignore_alias: bool) -> Self {
761 self.ignore_alias = ignore_alias;
762 self
763 }
764
765 fn with_ignore_index(mut self, ignore_index: bool) -> Self {
766 self.ignore_index = ignore_index;
767 self
768 }
769
770 fn with_ignore_name(mut self, ignore_name: bool) -> Self {
771 self.ignore_name = ignore_name;
772 self
773 }
774
775 fn with_initializer(mut self, initializer: ValInitializer) -> Self {
776 self.initializer = Some(initializer);
777 self
778 }
779}
780
781pub trait ConfigBuildMutable {
782 type Cfg;
783
784 fn config_mut(&mut self) -> &mut Self::Cfg;
785}
786
787impl<C, I> ConfigBuildMutable for ConfigBuilder<C, I> {
788 type Cfg = C;
789
790 fn config_mut(&mut self) -> &mut C {
791 &mut self.config
792 }
793}
794
795impl<C, T, I> ConfigBuildMutable for ConfigBuilderWith<C, T, I>
796where
797 C: Default,
798{
799 type Cfg = C;
800
801 fn config_mut(&mut self) -> &mut C {
802 self.inner_mut().config_mut()
803 }
804}
805
806pub trait ConfigBuildInfer<C> {
807 type Output<T>;
808
809 fn infer<T: 'static>(self) -> Self::Output<T>;
810}
811
812impl<C> ConfigBuildInfer<C> for &'_ str
813where
814 C: ConfigValue + Default,
815{
816 type Output<T> = ConfigBuilderWith<C, Self, T>;
817
818 fn infer<T: 'static>(self) -> Self::Output<T> {
819 ConfigBuilderWith::new(self, ConfigBuilder::new(C::default().with_type::<T>()))
820 }
821}
822
823impl<C> ConfigBuildInfer<C> for String
824where
825 C: ConfigValue + Default,
826{
827 type Output<T> = ConfigBuilderWith<C, Self, T>;
828
829 fn infer<T: 'static>(self) -> Self::Output<T> {
830 ConfigBuilderWith::new(self, ConfigBuilder::new(C::default().with_type::<T>()))
831 }
832}
833
834impl<C> ConfigBuildInfer<C> for &'_ String
835where
836 C: ConfigValue + Default,
837{
838 type Output<T> = ConfigBuilderWith<C, Self, T>;
839
840 fn infer<T: 'static>(self) -> Self::Output<T> {
841 ConfigBuilderWith::new(self, ConfigBuilder::new(C::default().with_type::<T>()))
842 }
843}
844
845impl<C, I> ConfigBuildInfer<C> for ConfigBuilder<C, I> {
846 type Output<T> = ConfigBuilder<C, T>;
847
848 fn infer<T: 'static>(self) -> Self::Output<T> {
849 ConfigBuilder {
850 config: self.config,
851 marker: PhantomData,
852 }
853 }
854}
855
856pub trait ConfigBuildWith {
857 type Output;
858
859 fn with_ctor(self, ctor: impl Into<String>) -> Self::Output;
860
861 fn with_name(self, name: impl Into<String>) -> Self::Output;
862
863 fn with_force(self, force: bool) -> Self::Output;
864
865 fn with_index(self, index: Index) -> Self::Output;
866
867 fn with_alias(self, alias: Vec<impl Into<String>>) -> Self::Output;
868
869 fn with_hint(self, hint: impl Into<String>) -> Self::Output;
870
871 fn with_help(self, help: impl Into<String>) -> Self::Output;
872
873 fn with_action(self, action: Action) -> Self::Output;
874
875 fn with_storer(self, storer: ValStorer) -> Self::Output;
876
877 fn with_initializer(self, initializer: ValInitializer) -> Self::Output;
878
879 fn with_ignore_alias(self, ignore_alias: bool) -> Self::Output;
880
881 fn with_ignore_index(self, ignore_index: bool) -> Self::Output;
882
883 fn with_ignore_name(self, ignore_name: bool) -> Self::Output;
884
885 fn with_style(self, styles: Vec<Style>) -> Self::Output;
886}
887
888impl<T> ConfigBuildWith for T
889where
890 T: ConfigBuildMutable,
891 T::Cfg: ConfigValue,
892{
893 type Output = Self;
894
895 fn with_ctor(mut self, ctor: impl Into<String>) -> Self::Output {
896 self.config_mut().set_ctor(ctor);
897 self
898 }
899
900 fn with_name(mut self, name: impl Into<String>) -> Self::Output {
901 self.config_mut().set_name(name);
902 self
903 }
904
905 fn with_force(mut self, force: bool) -> Self::Output {
906 self.config_mut().set_force(force);
907 self
908 }
909
910 fn with_index(mut self, index: Index) -> Self::Output {
911 self.config_mut().set_index(index);
912 self
913 }
914
915 fn with_alias(mut self, alias: Vec<impl Into<String>>) -> Self::Output {
916 self.config_mut().set_alias(alias);
917 self
918 }
919
920 fn with_hint(mut self, hint: impl Into<String>) -> Self::Output {
921 self.config_mut().set_hint(hint);
922 self
923 }
924
925 fn with_help(mut self, help: impl Into<String>) -> Self::Output {
926 self.config_mut().set_help(help);
927 self
928 }
929
930 fn with_action(mut self, action: Action) -> Self::Output {
931 self.config_mut().set_action(action);
932 self
933 }
934
935 fn with_storer(mut self, storer: ValStorer) -> Self::Output {
936 self.config_mut().set_storer(storer);
937 self
938 }
939
940 fn with_initializer(mut self, initializer: ValInitializer) -> Self::Output {
941 self.config_mut().set_initializer(initializer);
942 self
943 }
944
945 fn with_ignore_alias(mut self, ignore_alias: bool) -> Self::Output {
946 self.config_mut().set_ignore_alias(ignore_alias);
947 self
948 }
949
950 fn with_ignore_index(mut self, ignore_index: bool) -> Self::Output {
951 self.config_mut().set_ignore_index(ignore_index);
952 self
953 }
954
955 fn with_ignore_name(mut self, ignore_name: bool) -> Self::Output {
956 self.config_mut().set_ignore_name(ignore_name);
957 self
958 }
959
960 fn with_style(mut self, styles: Vec<Style>) -> Self::Output {
961 self.config_mut().set_style(styles);
962 self
963 }
964}
965
966pub struct ConfigBuilder<C, I> {
967 config: C,
968
969 marker: PhantomData<I>,
970}
971
972impl<C, I> Default for ConfigBuilder<C, I>
973where
974 C: Default,
975{
976 fn default() -> Self {
977 Self {
978 config: C::default(),
979 marker: Default::default(),
980 }
981 }
982}
983
984impl<C, I> Debug for ConfigBuilder<C, I>
985where
986 C: Debug,
987{
988 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
989 f.debug_struct("ConfigBuilder")
990 .field("config", &self.config)
991 .finish()
992 }
993}
994
995impl<C, I> ConfigBuilder<C, I> {
996 pub fn new(config: C) -> Self {
997 Self {
998 config,
999 marker: PhantomData,
1000 }
1001 }
1002
1003 pub fn config(&self) -> &C {
1004 &self.config
1005 }
1006
1007 pub fn config_mut(&mut self) -> &mut C {
1008 &mut self.config
1009 }
1010}
1011
1012pub struct ConfigBuilderWith<C, T, I> {
1013 init: T,
1014 inner: ConfigBuilder<C, I>,
1015}
1016
1017impl<C, T, I> Default for ConfigBuilderWith<C, T, I>
1018where
1019 T: Default,
1020 C: Default,
1021{
1022 fn default() -> Self {
1023 Self {
1024 init: Default::default(),
1025 inner: Default::default(),
1026 }
1027 }
1028}
1029
1030impl<C, T, I> Debug for ConfigBuilderWith<C, T, I>
1031where
1032 T: Debug,
1033 C: Debug,
1034{
1035 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1036 f.debug_struct("ConfigBuilderWith")
1037 .field("init", &self.init)
1038 .field("inner", &self.inner)
1039 .finish()
1040 }
1041}
1042
1043impl<C, T, I> ConfigBuilderWith<C, T, I> {
1044 pub fn new(init: T, inner: ConfigBuilder<C, I>) -> Self {
1045 Self { init, inner }
1046 }
1047
1048 pub fn inner(&self) -> &ConfigBuilder<C, I> {
1049 &self.inner
1050 }
1051
1052 pub fn inner_mut(&mut self) -> &mut ConfigBuilder<C, I> {
1053 &mut self.inner
1054 }
1055
1056 pub fn config(&self) -> &C {
1057 self.inner.config()
1058 }
1059
1060 pub fn config_mut(&mut self) -> &mut C {
1061 self.inner.config_mut()
1062 }
1063}
1064
1065impl<C, T, I> From<T> for ConfigBuilderWith<C, T, I>
1066where
1067 C: Default,
1068{
1069 fn from(value: T) -> Self {
1070 ConfigBuilderWith::new(value, Default::default())
1071 }
1072}
1073
1074macro_rules! def_help_for {
1075 ($type:ty) => {
1076 impl ConfigBuildWith for $type {
1077 type Output = ConfigBuilderWith<OptConfig, Self, Placeholder>;
1078
1079 fn with_ctor(self, ctor: impl Into<String>) -> Self::Output {
1080 ConfigBuilderWith::new(
1081 self,
1082 ConfigBuilder::new(OptConfig::default().with_ctor(ctor)),
1083 )
1084 }
1085
1086 fn with_name(self, name: impl Into<String>) -> Self::Output {
1087 ConfigBuilderWith::new(
1088 self,
1089 ConfigBuilder::new(OptConfig::default().with_name(name)),
1090 )
1091 }
1092
1093 fn with_force(self, force: bool) -> Self::Output {
1094 ConfigBuilderWith::new(
1095 self,
1096 ConfigBuilder::new(OptConfig::default().with_force(force)),
1097 )
1098 }
1099
1100 fn with_index(self, index: Index) -> Self::Output {
1101 ConfigBuilderWith::new(
1102 self,
1103 ConfigBuilder::new(OptConfig::default().with_index(index)),
1104 )
1105 }
1106
1107 fn with_alias(self, alias: Vec<impl Into<String>>) -> Self::Output {
1108 ConfigBuilderWith::new(
1109 self,
1110 ConfigBuilder::new(OptConfig::default().with_alias(alias)),
1111 )
1112 }
1113
1114 fn with_hint(self, hint: impl Into<String>) -> Self::Output {
1115 ConfigBuilderWith::new(
1116 self,
1117 ConfigBuilder::new(OptConfig::default().with_hint(hint)),
1118 )
1119 }
1120
1121 fn with_help(self, help: impl Into<String>) -> Self::Output {
1122 ConfigBuilderWith::new(
1123 self,
1124 ConfigBuilder::new(OptConfig::default().with_help(help)),
1125 )
1126 }
1127
1128 fn with_action(self, action: Action) -> Self::Output {
1129 ConfigBuilderWith::new(
1130 self,
1131 ConfigBuilder::new(OptConfig::default().with_action(action)),
1132 )
1133 }
1134
1135 fn with_storer(self, storer: ValStorer) -> Self::Output {
1136 ConfigBuilderWith::new(
1137 self,
1138 ConfigBuilder::new(OptConfig::default().with_storer(storer)),
1139 )
1140 }
1141
1142 fn with_initializer(self, initializer: ValInitializer) -> Self::Output {
1143 ConfigBuilderWith::new(
1144 self,
1145 ConfigBuilder::new(OptConfig::default().with_initializer(initializer)),
1146 )
1147 }
1148
1149 fn with_ignore_alias(self, ignore_alias: bool) -> Self::Output {
1150 ConfigBuilderWith::new(
1151 self,
1152 ConfigBuilder::new(OptConfig::default().with_ignore_alias(ignore_alias)),
1153 )
1154 }
1155
1156 fn with_ignore_index(self, ignore_index: bool) -> Self::Output {
1157 ConfigBuilderWith::new(
1158 self,
1159 ConfigBuilder::new(OptConfig::default().with_ignore_index(ignore_index)),
1160 )
1161 }
1162
1163 fn with_ignore_name(self, ignore_name: bool) -> Self::Output {
1164 ConfigBuilderWith::new(
1165 self,
1166 ConfigBuilder::new(OptConfig::default().with_ignore_name(ignore_name)),
1167 )
1168 }
1169
1170 fn with_style(self, styles: Vec<Style>) -> Self::Output {
1171 ConfigBuilderWith::new(
1172 self,
1173 ConfigBuilder::new(OptConfig::default().with_style(styles)),
1174 )
1175 }
1176 }
1177 };
1178}
1179
1180def_help_for!(&'_ str);
1181def_help_for!(&'_ String);
1182def_help_for!(String);