derive_show_asm/
derive_show_asm.rs1use bpaf::{construct, long, Bpaf, Parser, ShellComp};
5use std::{convert::Infallible, path::PathBuf};
6
7#[derive(Clone, Debug, Bpaf)]
8#[bpaf(options("asm"))] #[allow(clippy::struct_excessive_bools)]
10pub struct Options {
11 #[bpaf(external(parse_manifest_path))]
12 pub manifest_path: PathBuf,
13 #[bpaf(argument("DIR"))]
15 pub target_dir: Option<PathBuf>,
16 #[bpaf(long, short, argument("SPEC"))]
18 pub package: Option<String>,
19 #[bpaf(external, optional)]
20 pub focus: Option<Focus>,
21 pub dry: bool,
23 pub frozen: bool,
25 pub locked: bool,
27 pub offline: bool,
29 #[bpaf(external)]
30 pub format: Format,
31 #[bpaf(external, fallback(Syntax::Intel))]
32 pub syntax: Syntax,
33 #[bpaf(external)]
34 pub selected_function: SelectedFunction,
35}
36
37#[derive(Debug, Clone, Bpaf)]
38pub struct SelectedFunction {
40 #[bpaf(positional("FUNCTION"))]
42 pub function: Option<String>,
43 #[bpaf(positional("INDEX"), fallback(0))]
45 pub nth: usize,
46}
47
48fn parse_manifest_path() -> impl Parser<PathBuf> {
49 long("manifest-path")
50 .help("Path to Cargo.toml")
51 .argument::<PathBuf>("PATH")
52 .complete_shell(ShellComp::File {
53 mask: Some("*.toml"),
54 })
55 .parse(|p| {
56 if p.is_absolute() {
58 Ok(p)
59 } else {
60 std::env::current_dir()
61 .map(|d| d.join(p))
62 .and_then(|full_path| full_path.canonicalize())
63 }
64 })
65 .fallback_with(|| std::env::current_dir().map(|x| x.join("Cargo.toml")))
66}
67
68#[derive(Debug, Clone, Bpaf)]
69pub struct Format {
71 pub rust: bool,
73
74 #[bpaf(external(color_detection))]
75 pub color: bool,
76
77 pub full_name: bool,
79}
80
81#[derive(Debug, Clone, Bpaf)]
82pub enum Syntax {
89 Intel,
91 Att,
93}
94
95fn color_detection() -> impl Parser<bool> {
96 let yes = long("color")
97 .help("Enable color highlighting")
98 .req_flag(true);
99 let no = long("no-color")
100 .help("Disable color highlighting")
101 .req_flag(false);
102 construct!([yes, no]).fallback_with::<_, Infallible>(|| {
103 Ok(true)
105 })
106}
107
108fn comp_examples(prefix: &String) -> Vec<(String, Option<String>)> {
109 let examples = ["derive_show_asm", "coreutils", "comonad"];
111 examples
112 .iter()
113 .filter_map(|e| {
114 if e.starts_with(prefix) {
115 Some((e.to_string(), None))
116 } else {
117 None
118 }
119 })
120 .collect()
121}
122
123#[derive(Debug, Clone, Bpaf)]
124pub enum Focus {
128 Lib,
130
131 Test(
132 #[bpaf(long("test"), argument("TEST"))]
134 String,
135 ),
136
137 Bench(
138 #[bpaf(long("bench"), argument("BENCH"))]
140 String,
141 ),
142
143 Example(
144 #[bpaf(long("example"), argument("EXAMPLE"), complete(comp_examples))]
146 String,
147 ),
148
149 Bin(
150 #[bpaf(long("bin"), argument("BIN"))]
152 String,
153 ),
154}
155
156fn main() {
157 println!("{:#?}", options().run());
158}