Skip to content

Commit

Permalink
extract common logic to a function
Browse files Browse the repository at this point in the history
  • Loading branch information
alpaylan committed Jan 2, 2025
1 parent 51f5eff commit cbb0ee8
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions debugger/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ impl Cli {
println!("Unrecognized command: {}; use h for help", command);
}

fn extract_arg(cmd: &str) -> Option<&str> {
cmd.find(' ').map(|pos| &cmd[pos + 1..])
}


fn execute_command(&mut self, command: &str) -> Result<(), DebuggerError> {
match command {
"" => (),
Expand All @@ -134,15 +139,15 @@ impl Cli {
"ba" => self.context.add_all_rules_breakpoints()?,
"da" => self.context.delete_all_breakpoints(),
grammar if "grammar".starts_with(grammar) => {
let grammar_file = grammar.find(" ").and_then(|pos| Some(&grammar[pos + 1..]));
let grammar_file = Self::extract_arg(grammar);
if let Some(grammar_file) = grammar_file {
self.grammar(PathBuf::from(grammar_file))?;
} else {
println!("expected filename, usage: g(grammar) <filename>");
}
}
input if "input".starts_with(input) => {
let input_file = input.find(" ").and_then(|pos| Some(&input[pos + 1..]));
let input_file = Self::extract_arg(input);
if let Some(input_file) = input_file {
self.input(PathBuf::from(input_file))?;
} else {
Expand All @@ -154,25 +159,23 @@ impl Cli {
self.context.load_input_direct(input.to_owned());
}
breakpoint if "breakpoint".starts_with(breakpoint) => {
let rule = breakpoint
.find(" ")
.and_then(|pos| Some(&breakpoint[pos + 1..]));
let rule = Self::extract_arg(breakpoint);
if let Some(rule) = rule {
self.breakpoint(rule);
} else {
println!("expected rule, usage: b(breakpoint) <rule>");
}
}
delete if "delete".starts_with(delete) => {
let rule = delete.find(" ").and_then(|pos| Some(&delete[pos + 1..]));
let rule = Self::extract_arg(delete);
if let Some(rule) = rule {
self.context.delete_breakpoint(rule);
} else {
println!("expected rule, usage: d(delete) <rule>");
}
}
run if "run".starts_with(run) => {
let rule = run.find(" ").and_then(|pos| Some(&run[pos + 1..]));
let rule = Self::extract_arg(run);
if let Some(rule) = rule {
self.run(rule)?;
} else {
Expand Down

0 comments on commit cbb0ee8

Please sign in to comment.