use clap::Parser;
#[derive(Clone, Debug, Parser)]
pub struct Args {
/// DNS name for DNS-SD discovery of beacon servers
#[clap(
long,
value_parser(addr::parse_dns_name),
)]
pub beacon_discovery_dns_name: addr::dns::Name<'static>,
}
I'd get an error such as:
error[E0308]: mismatched types
...
3 | #[derive(Clone, Debug, Parser)]
| ^^^^^^ one type is more general than the other
...
= note: expected trait `for<'r> <for<'r> fn(&'r str) -> std::result::Result<addr::dns::Name<'r>, addr::error::Error<'r>> {addr::parse_dns_name} as std::ops::FnOnce<(&'r str,)>>`
found trait `for<'r> <for<'r> fn(&'r str) -> std::result::Result<addr::dns::Name<'r>, addr::error::Error<'r>> {addr::parse_dns_name} as std::ops::FnOnce<(&'r str,)>>`
Obviously I cannot change either clap's code or addr's code, so how can I adapt my code to have this working?
If I have to make another call myself, that defeats the purpose. (It's not sustainable for the caller to have to know all these methods they have to call in addition to just Args::parse.)
show that if you get addr::dns::Name<'static>, you must provide it with &'static str.
But AFAIK a cli struct like Args usually holds owned (non-reference) types like String.
You can test and get it from the error:
pub beacon_discovery_dns_name: &'static str,
error[E0277]: the trait bound `&str: FromStr` is not satisfied
--> src/main.rs:13:9
|
13 | pub beacon_discovery_dns_name: &'static str,
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `FromStr` is not implemented for `&str`
|
= help: the trait `FromStr` is implemented for `std::string::String`