Browse Source

refactoring: rename variable 'command' to 'keyword'

main
fdai7381 2 years ago
committed by fdai7451
parent
commit
e826d48ea3
  1. 4
      src/execute.rs
  2. 12
      src/parse.rs

4
src/execute.rs

@ -20,8 +20,8 @@ pub fn execute<S: Into<Stdio>>(mut command: Command, stdin: S) -> io::Result<(Ch
} }
pub fn interpret(line: &str) -> io::Result<()> { pub fn interpret(line: &str) -> io::Result<()> {
let (command, args) = parse_line(line);
let mut command = Command::new(command);
let (keyword, args) = parse_line(line);
let mut command = Command::new(keyword);
command.args(args); command.args(args);
let (mut stdout, _) = execute(command, Stdio::null())?; let (mut stdout, _) = execute(command, Stdio::null())?;
io::copy(&mut stdout, &mut io::stdout())?; io::copy(&mut stdout, &mut io::stdout())?;

12
src/parse.rs

@ -1,14 +1,14 @@
pub fn parse_line(line: &str) -> (&str, Vec<&str>) { pub fn parse_line(line: &str) -> (&str, Vec<&str>) {
let tokens = tokenize(line); let tokens = tokenize(line);
let command = tokens[0];
let keyword = tokens[0];
let mut args: Vec<&str> = Vec::new(); let mut args: Vec<&str> = Vec::new();
if tokens.len() > 1 { if tokens.len() > 1 {
args = tokens[1..].to_vec(); args = tokens[1..].to_vec();
} }
(command, args)
(keyword, args)
} }
fn tokenize(line: &str) -> Vec<&str> { fn tokenize(line: &str) -> Vec<&str> {
@ -22,16 +22,16 @@ mod tests {
#[test] #[test]
fn test_tokenize() { fn test_tokenize() {
assert_eq!( assert_eq!(
tokenize("test arg1 arg2 arg3"),
vec!["test", "arg1", "arg2", "arg3"]
tokenize("keyword arg1 arg2 arg3"),
vec!["keyword", "arg1", "arg2", "arg3"]
); );
} }
#[test] #[test]
fn test_parse_line() { fn test_parse_line() {
assert_eq!( assert_eq!(
parse_line("test arg1 arg2 arg3"),
("test", vec!["arg1", "arg2", "arg3"])
parse_line("keyword arg1 arg2 arg3"),
("keyword", vec!["arg1", "arg2", "arg3"])
); );
} }
} }
Loading…
Cancel
Save