fix(derive): Improve Kind conflict errors

This makes it better scale for the future
This commit is contained in:
Ed Page 2022-09-02 10:48:36 -05:00
parent 11ffcf9393
commit edce5c2119
4 changed files with 22 additions and 12 deletions

View file

@ -855,13 +855,10 @@ impl Item {
}
fn set_kind(&mut self, kind: Sp<Kind>) {
if let Kind::Arg(_) = *self.kind {
self.kind = kind;
if let (Some(old), Some(new)) = (self.kind.name(), kind.name()) {
abort!(kind.span(), "`{}` cannot be used with `{}`", new, old);
} else {
abort!(
kind.span(),
"`subcommand`, `flatten`, `external_subcommand` and `skip` cannot be used together"
);
self.kind = kind;
}
}
@ -1094,6 +1091,19 @@ pub enum Kind {
ExternalSubcommand,
}
impl Kind {
fn name(&self) -> Option<&'static str> {
match self {
Self::Arg(_) => None,
Self::FromGlobal(_) => Some("from_global"),
Self::Subcommand(_) => Some("subcommand"),
Self::Flatten => Some("flatten"),
Self::Skip(_) => Some("skip"),
Self::ExternalSubcommand => Some("external_subcommand"),
}
}
}
#[derive(Clone)]
pub struct Method {
name: Ident,

View file

@ -1,5 +1,5 @@
error: `subcommand`, `flatten`, `external_subcommand` and `skip` cannot be used together
--> $DIR/skip_flatten.rs:17:18
error: `flatten` cannot be used with `skip`
--> tests/derive_ui/skip_flatten.rs:17:18
|
17 | #[clap(skip, flatten)]
| ^^^^^^^

View file

@ -1,5 +1,5 @@
error: `subcommand`, `flatten`, `external_subcommand` and `skip` cannot be used together
--> $DIR/skip_subcommand.rs:17:24
error: `skip` cannot be used with `subcommand`
--> tests/derive_ui/skip_subcommand.rs:17:24
|
17 | #[clap(subcommand, skip)]
| ^^^^

View file

@ -1,5 +1,5 @@
error: `subcommand`, `flatten`, `external_subcommand` and `skip` cannot be used together
--> $DIR/subcommand_and_flatten.rs:16:24
error: `flatten` cannot be used with `subcommand`
--> tests/derive_ui/subcommand_and_flatten.rs:16:24
|
16 | #[clap(subcommand, flatten)]
| ^^^^^^^