derive_smart_pointer/derive-smart-pointer.rs
1//! With minimal changes you can parse directly into smart pointers.
2//!
3//! While Bpaf derive macro knows nothing about Box or Arc it lets you
4//! to parse into them by overriding the parsing sequence.
5//!
6//! Here `name` first parsed into a regular `String` then converted
7//! into `Box<str>` using `map`.
8//!
9//! Similarly a set of coins is parsed into a regular vector and later converted into Arc still
10//! inside the parser
11
12#![allow(dead_code)]
13
14use bpaf::*;
15use std::sync::Arc;
16
17#[derive(Debug, Clone, Bpaf)]
18#[bpaf(options)]
19pub struct Options {
20 #[bpaf(argument::<String>("NAME"), map(Box::from))]
21 /// Adventurer's name
22 name: Box<str>,
23
24 #[bpaf(positional::<usize>("COIN"), many, map(Arc::from))]
25 /// A set of coins
26 coins: Arc<[usize]>,
27}
28
29fn main() {
30 let xs = options().fallback_to_usage().run();
31 println!("{xs:?}");
32}