From c9e88488aeccb232eaabde28861c76c98be83114 Mon Sep 17 00:00:00 2001 From: Tao Guo Date: Fri, 26 Mar 2021 13:47:18 -0700 Subject: [PATCH] parser clean up use match for parsing fd redirections --- macros/src/parser.rs | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/macros/src/parser.rs b/macros/src/parser.rs index efc34ea..7cc74c5 100644 --- a/macros/src/parser.rs +++ b/macros/src/parser.rs @@ -97,30 +97,23 @@ impl Parser { while *i < self.args.len() { match self.args[*i].clone() { ParseRedirectFd(fd1, fd2) => { - if fd1 == fd2 { - *i += 1; - continue; - } - let mut redirect = quote!(::cmd_lib::Redirect); - if fd1 == 1 && fd2 == 2 { - redirect.extend(quote!(::StdoutToStderr)); - } else if fd1 == 2 && fd2 == 1 { - redirect.extend(quote!(::StderrToStdout)); - } else { - panic!("unsupported fd numbers: {} {}", fd1, fd2); + if fd1 != fd2 { + let mut redirect = quote!(::cmd_lib::Redirect); + match (fd1, fd2) { + (1, 2) => redirect.extend(quote!(::StdoutToStderr)), + (2, 1) => redirect.extend(quote!(::StderrToStdout)), + _ => panic!("unsupported fd numbers: {} {}", fd1, fd2), + } + ret.extend(quote!(.add_redirect(#redirect))); } - ret.extend(quote!(.add_redirect(#redirect))); } ParseRedirectFile(fd1, file, append) => { let mut redirect = quote!(::cmd_lib::Redirect); - if fd1 == 0 { - redirect.extend(quote!(::FileToStdin(#file))); - } else if fd1 == 1 { - redirect.extend(quote!(::StdoutToFile(#file, #append))); - } else if fd1 == 2 { - redirect.extend(quote!(::StderrToFile(#file, #append))); - } else { - panic!("unsupported fd ({}) redirect to file {}", fd1, file); + match fd1 { + 0 => redirect.extend(quote!(::FileToStdin(#file))), + 1 => redirect.extend(quote!(::StdoutToFile(#file, #append))), + 2 => redirect.extend(quote!(::StderrToFile(#file, #append))), + _ => panic!("unsupported fd ({}) redirect to file {}", fd1, file), } ret.extend(quote!(.add_redirect(#redirect))); }