|
1 | 1 | #![doc = include_str!("../README.md")] |
| 2 | +use clap::{Args, FromArgMatches, Subcommand}; |
2 | 3 | pub use config::ConfigError; |
3 | 4 | use config::{Config, Environment, File, Value}; |
4 | | -use serde::{Serialize, de::DeserializeOwned}; |
| 5 | +use serde::{Deserialize, Serialize, de::DeserializeOwned}; |
5 | 6 | use std::path::Path; |
6 | 7 | use toml::Value as TomlValue; |
7 | 8 |
|
@@ -100,3 +101,70 @@ fn toml_to_config(toml: TomlValue) -> Value { |
100 | 101 | ), |
101 | 102 | } |
102 | 103 | } |
| 104 | + |
| 105 | +#[derive( |
| 106 | + Eq, |
| 107 | + PartialEq, |
| 108 | + Ord, |
| 109 | + PartialOrd, |
| 110 | + Debug, |
| 111 | + Clone, |
| 112 | + Serialize, |
| 113 | + Deserialize, |
| 114 | +)] |
| 115 | +pub struct ReClap<T, U> |
| 116 | +where |
| 117 | + T: Args, |
| 118 | + U: Subcommand, |
| 119 | +{ |
| 120 | + pub prev: T, |
| 121 | + pub next: Option<Box<U>>, |
| 122 | +} |
| 123 | + |
| 124 | +impl<T, U> Args for ReClap<T, U> |
| 125 | +where |
| 126 | + T: Args, |
| 127 | + U: Subcommand, |
| 128 | +{ |
| 129 | + fn augment_args(cmd: clap::Command) -> clap::Command { |
| 130 | + T::augment_args(cmd).defer(|cmd| { |
| 131 | + U::augment_subcommands( |
| 132 | + cmd.disable_help_subcommand(true), |
| 133 | + ) |
| 134 | + }) |
| 135 | + } |
| 136 | + fn augment_args_for_update( |
| 137 | + _: clap::Command, |
| 138 | + ) -> clap::Command { |
| 139 | + unimplemented!() |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +impl<T, U> FromArgMatches for ReClap<T, U> |
| 144 | +where |
| 145 | + T: Args, |
| 146 | + U: Subcommand, |
| 147 | +{ |
| 148 | + fn from_arg_matches( |
| 149 | + matches: &clap::ArgMatches, |
| 150 | + ) -> Result<Self, clap::Error> { |
| 151 | + let prev = T::from_arg_matches(matches)?; |
| 152 | + let next = if let Some((_name, _sub)) = |
| 153 | + matches.subcommand() |
| 154 | + { |
| 155 | + Some(U::from_arg_matches(matches)?) |
| 156 | + } else { |
| 157 | + None |
| 158 | + }; |
| 159 | + Ok(Self { |
| 160 | + prev, |
| 161 | + next: next.map(Box::new), |
| 162 | + }) |
| 163 | + } |
| 164 | + fn update_from_arg_matches( |
| 165 | + &mut self, |
| 166 | + _: &clap::ArgMatches, |
| 167 | + ) -> Result<(), clap::Error> { |
| 168 | + unimplemented!() |
| 169 | + } |
| 170 | +} |
0 commit comments