Nameclash with derive (crates pest and clap)

I want to use the Rust crates clap and pest in the same code. Both have a Parser, which can be used in a derive like this:

  • clap
use clap::Parser;

#[derive(Parser)]
struct Args {
...
}
  • pest
use pest::Parser;

#[derive(Parser)]
#[grammar = "ident.pest"]
struct IdentParser;

How can I avoid a name clash?

use the full path:

#[derive(clap::Parser)]
struct Cli {
  ...
}

or make sure to not use both in the same module.

3 Likes

or

use clap::Parser as ClapParser;
use pest::Parser as PestParser;

should also work...

2 Likes

Thanks to both of you, @jer and @arlecchino, I ended up using both of your suggestions. The full details can be found in the commit log. I had to update three executables, two of them were using both crates at the same time, one of them just one crate. Basically I wanted to switch from structopt to clap, but ran into the conflict with pest. So the solution looked like this:

use clap::Parser as ClapParser;
use pest::Parser as PestParser;
...
#[derive(clap::Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
...
}
...
#[derive(pest_derive::Parser)]
#[grammar = "ident.pest"]
struct IdentParser;