Repository navigation
|
According to the tutorial and the practical experience, Instead of: #[derive(Debug, Clone, bpaf::Bpaf)]
pub struct A1Options {
a1: String,
a1x: bool,
}
#[derive(Debug, Clone, bpaf::Bpaf)]
pub struct A2Options {
a2: i64,
a2x: bool,
}
#[derive(Debug, Clone, bpaf::Bpaf)]
pub enum AOptions {
A1(
#[bpaf(external(a1_options))]
A1Options,
),
A2(
#[bpaf(external(a2_options))]
A2Options,
),
}
#[derive(Debug, Clone, bpaf::Bpaf)]
#[bpaf(options)]
pub struct Options {
#[bpaf(external)]
a_options: AOptions,
}
fn main() {
println!("{:?}", options().run())
}I would really like to be able to write: #[derive(Debug, Clone, bpaf::Bpaf)]
pub struct A1Options {
a1: String,
a1x: bool,
}
#[derive(Debug, Clone, bpaf::Bpaf)]
pub struct A2Options {
a2: i64,
a2x: bool,
}
#[derive(Debug, Clone, bpaf::Bpaf)]
pub enum AOptions {
A1(A1Options),
A2(A2Options),
}
#[derive(Debug, Clone, bpaf::Bpaf)]
pub struct Options {
a: AOptions,
}
fn main() {
println!("{:?}", bpaf::run::<Options>())
}P.S. not related to code but to the end-user experience with this example. Trying to to supply incompatible sets of options leads to confusing error messages, e.g.: |
Replies: 1 comment 1 reply
You tend to have much more primitive parsers (those that parse using flag or It's a decision I made, but mostly coming from a technical limitation.
This again optimizes for more common scenario if you work with existing types: This way you can have several parsers for the same type and the name carries some meaning. This also makes it impossible to use the implicit form in case of enum variants containing structs (see example below).
For a custom type a trait would be more ergonomic, but the trait won't work if you are working with existing types. Supporting both a trait and a function would require a second variant of
Unfortunately proc macros are limited like that.
I think I have this fixed in the next major version. |
You tend to have much more primitive parsers (those that parse using flag or
FromStrlogic) than nested parsers so I tried to optimize towards more common case scenario. In a proc macro you can't tell if a function targeted byexternalexists or not. When there's no annotation derive macro makes a few attempts to guess (is it abool?, anOption<T>, aVec<T>) before deciding to go with eitherpositionalorargumentparser withFromStr.It'…