Skip to content

Commit ed63041

Browse files
committed
Simplify
1 parent bb50614 commit ed63041

File tree

1 file changed

+69
-106
lines changed
  • crates/rust-analyzer/src/bin

1 file changed

+69
-106
lines changed

crates/rust-analyzer/src/bin/args.rs

Lines changed: 69 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
use std::{env, fmt::Write, path::PathBuf};
77

8-
use anyhow::{bail, Result};
8+
use anyhow::{bail, format_err, Result};
99
use pico_args::Arguments;
1010
use rust_analyzer::cli::{AnalysisStatsCmd, BenchCmd, BenchWhat, Position, Verbosity};
1111
use ssr::{SsrPattern, SsrRule};
@@ -96,8 +96,6 @@ diagnostics <PATH>
9696
9797
ssr [RULE...]
9898
<RULE> A structured search replace rule (`$a.foo($b) ==> bar($a, $b)`)
99-
--debug <snippet> Prints debug information for any nodes with source exactly
100-
equal to <snippet>
10199
102100
search [PATTERN..]
103101
<PATTERN> A structured search replace pattern (`$a.foo($b)`)
@@ -145,116 +143,81 @@ impl Args {
145143
}
146144
};
147145
let command = match subcommand.as_str() {
148-
"parse" => {
149-
let no_dump = matches.contains("--no-dump");
150-
matches.finish().or_else(handle_extra_flags)?;
151-
Command::Parse { no_dump }
152-
}
153-
"symbols" => {
154-
matches.finish().or_else(handle_extra_flags)?;
155-
Command::Symbols
156-
}
157-
"highlight" => {
158-
let rainbow = matches.contains("--rainbow");
159-
matches.finish().or_else(handle_extra_flags)?;
160-
Command::Highlight { rainbow }
161-
}
162-
"analysis-stats" => {
163-
let randomize = matches.contains("--randomize");
164-
let parallel = matches.contains("--parallel");
165-
let memory_usage = matches.contains("--memory-usage");
166-
let only: Option<String> = matches.opt_value_from_str(["-o", "--only"])?;
167-
let with_deps: bool = matches.contains("--with-deps");
168-
let load_output_dirs = matches.contains("--load-output-dirs");
169-
let with_proc_macro = matches.contains("--with-proc-macro");
170-
let path = {
171-
let mut trailing = matches.free()?;
172-
if trailing.len() != 1 {
173-
bail!("Invalid flags");
174-
}
175-
trailing.pop().unwrap().into()
176-
};
177-
178-
Command::AnalysisStats(AnalysisStatsCmd {
179-
randomize,
180-
parallel,
181-
memory_usage,
182-
only,
183-
with_deps,
184-
path,
185-
load_output_dirs,
186-
with_proc_macro,
187-
})
188-
}
189-
"analysis-bench" => {
190-
let highlight_path: Option<String> = matches.opt_value_from_str("--highlight")?;
191-
let complete_path: Option<Position> = matches.opt_value_from_str("--complete")?;
192-
let goto_def_path: Option<Position> = matches.opt_value_from_str("--goto-def")?;
193-
let what = match (highlight_path, complete_path, goto_def_path) {
194-
(Some(path), None, None) => {
195-
let path = env::current_dir().unwrap().join(path);
196-
BenchWhat::Highlight { path: AbsPathBuf::assert(path) }
197-
}
198-
(None, Some(position), None) => BenchWhat::Complete(position),
199-
(None, None, Some(position)) => BenchWhat::GotoDef(position),
200-
_ => panic!(
201-
"exactly one of `--highlight`, `--complete` or `--goto-def` must be set"
202-
),
203-
};
204-
let memory_usage = matches.contains("--memory-usage");
205-
let load_output_dirs = matches.contains("--load-output-dirs");
206-
let with_proc_macro = matches.contains("--with-proc-macro");
207-
208-
let path = {
209-
let mut trailing = matches.free()?;
210-
if trailing.len() != 1 {
211-
bail!("Invalid flags");
146+
"parse" => Command::Parse { no_dump: matches.contains("--no-dump") },
147+
"symbols" => Command::Symbols,
148+
"highlight" => Command::Highlight { rainbow: matches.contains("--rainbow") },
149+
"analysis-stats" => Command::AnalysisStats(AnalysisStatsCmd {
150+
randomize: matches.contains("--randomize"),
151+
parallel: matches.contains("--parallel"),
152+
memory_usage: matches.contains("--memory-usage"),
153+
only: matches.opt_value_from_str(["-o", "--only"])?,
154+
with_deps: matches.contains("--with-deps"),
155+
load_output_dirs: matches.contains("--load-output-dirs"),
156+
with_proc_macro: matches.contains("--with-proc-macro"),
157+
path: matches
158+
.free_from_str()?
159+
.ok_or_else(|| format_err!("expected positional argument"))?,
160+
}),
161+
"analysis-bench" => Command::Bench(BenchCmd {
162+
what: {
163+
let highlight_path: Option<String> =
164+
matches.opt_value_from_str("--highlight")?;
165+
let complete_path: Option<Position> =
166+
matches.opt_value_from_str("--complete")?;
167+
let goto_def_path: Option<Position> =
168+
matches.opt_value_from_str("--goto-def")?;
169+
match (highlight_path, complete_path, goto_def_path) {
170+
(Some(path), None, None) => {
171+
let path = env::current_dir().unwrap().join(path);
172+
BenchWhat::Highlight { path: AbsPathBuf::assert(path) }
173+
}
174+
(None, Some(position), None) => BenchWhat::Complete(position),
175+
(None, None, Some(position)) => BenchWhat::GotoDef(position),
176+
_ => panic!(
177+
"exactly one of `--highlight`, `--complete` or `--goto-def` must be set"
178+
),
179+
}
180+
},
181+
memory_usage: matches.contains("--memory-usage"),
182+
load_output_dirs: matches.contains("--load-output-dirs"),
183+
with_proc_macro: matches.contains("--with-proc-macro"),
184+
path: matches
185+
.free_from_str()?
186+
.ok_or_else(|| format_err!("expected positional argument"))?,
187+
}),
188+
"diagnostics" => Command::Diagnostics {
189+
load_output_dirs: matches.contains("--load-output-dirs"),
190+
with_proc_macro: matches.contains("--with-proc-macro"),
191+
path: matches
192+
.free_from_str()?
193+
.ok_or_else(|| format_err!("expected positional argument"))?,
194+
},
195+
"proc-macro" => Command::ProcMacro,
196+
"ssr" => Command::Ssr {
197+
rules: {
198+
let mut acc = Vec::new();
199+
while let Some(rule) = matches.free_from_str()? {
200+
acc.push(rule);
212201
}
213-
trailing.pop().unwrap().into()
214-
};
215-
216-
Command::Bench(BenchCmd {
217-
memory_usage,
218-
path,
219-
what,
220-
load_output_dirs,
221-
with_proc_macro,
222-
})
223-
}
224-
"diagnostics" => {
225-
let load_output_dirs = matches.contains("--load-output-dirs");
226-
let with_proc_macro = matches.contains("--with-proc-macro");
227-
let path = {
228-
let mut trailing = matches.free()?;
229-
if trailing.len() != 1 {
230-
bail!("Invalid flags");
202+
acc
203+
},
204+
},
205+
"search" => Command::StructuredSearch {
206+
debug_snippet: matches.opt_value_from_str("--debug")?,
207+
patterns: {
208+
let mut acc = Vec::new();
209+
while let Some(rule) = matches.free_from_str()? {
210+
acc.push(rule);
231211
}
232-
trailing.pop().unwrap().into()
233-
};
234-
235-
Command::Diagnostics { path, load_output_dirs, with_proc_macro }
236-
}
237-
"proc-macro" => Command::ProcMacro,
238-
"ssr" => {
239-
let mut rules = Vec::new();
240-
while let Some(rule) = matches.free_from_str()? {
241-
rules.push(rule);
242-
}
243-
Command::Ssr { rules }
244-
}
245-
"search" => {
246-
let debug_snippet = matches.opt_value_from_str("--debug")?;
247-
let mut patterns = Vec::new();
248-
while let Some(rule) = matches.free_from_str()? {
249-
patterns.push(rule);
250-
}
251-
Command::StructuredSearch { patterns, debug_snippet }
252-
}
212+
acc
213+
},
214+
},
253215
_ => {
254216
eprintln!("{}", HELP);
255217
return Ok(Args { verbosity, log_file: None, command: Command::Help });
256218
}
257219
};
220+
matches.finish().or_else(handle_extra_flags)?;
258221
Ok(Args { verbosity, log_file, command })
259222
}
260223
}

0 commit comments

Comments
 (0)