fn prepare_command(
        &mut self, s: &str, chunk: &Chunk, i: usize,
    ) -> Option<String> {
        let captures = CAPTURE_NUM.captures_iter(s);
        let mut final_s = s.to_string();
        for capture in captures {
            let capture_str = capture.get(1).unwrap().as_str();
            let capture_num_res = capture_str.parse::<usize>();
            let capture_num =
                match capture_num_res {
                    Ok(n) => { n }
                    Err(_) => {
                        print_error(chunk, i, "invalid stack element");
                        return None;
                    }
                };

            let capture_el_rr_opt =
                self.stack.get(self.stack.len() - 1 - capture_num);
            match capture_el_rr_opt {
                Some(capture_el_rr) => {
                    let capture_el_rrb = capture_el_rr.borrow();
                    let capture_el_str_pre = capture_el_rrb.to_string();
                    let capture_el_str_opt = to_string_2(&capture_el_str_pre);
                    match capture_el_str_opt {
                        Some(capture_el_str) => {
                            let capture_str_with_brackets = format!("\\{{{}\\}}", capture_str);
                            let cswb_regex = Regex::new(&capture_str_with_brackets).unwrap();
                            final_s = cswb_regex.replace_all(&final_s, capture_el_str).to_string();
                        }
                        _ => {
                            print_error(chunk, i, "unable to parse command");
                            return None;
                        }
                    }
                }
                None => {
                    let err_str = format!("stack element {} not present", capture_num);
                    print_error(chunk, i, &err_str);
                    return None;
                }
            }
        }

        while CAPTURE_WITHOUT_NUM.is_match(&final_s) {
            if self.stack.len() < 1 {
                print_error(chunk, i, "no more elements to pop from stack");
                return None;
            }

	    let value_rr = self.stack.pop().unwrap();
	    let value_rrb = value_rr.borrow();
	    let value_pre = value_rrb.to_string();
	    let value_opt = to_string_2(&value_pre);

            match value_opt {
                Some(s) => {
                    final_s = CAPTURE_WITHOUT_NUM.replace(&final_s, s).to_string();
                }
                _ => {
                    print_error(chunk, i, "unable to parse command");
                    return None;
                }
            }
        }

        let homedir_res = std::env::var("HOME");
        match homedir_res {
            Ok(homedir) => {
                let s = " ".to_owned() + &homedir;
                final_s = HOME_DIR_TILDE.replace_all(&final_s, &*s).to_string();
            }
            _ => {}
        }

        return Some(final_s);
    }
/// Takes a command string, substitutes for the {num} and {}
/// stack element placeholders as well as the ~ home directory
/// placeholder, and returns the resulting string.
Takes a command string, substitutes for the {num} and {}
stack element placeholders as well as the ~ home directory
placeholder, and returns the resulting string.
Takes a command string substitutes for the { num } and {} stack element placeholders as well as the ~ home directory placeholder and returns the resulting string .
fn prepare_command(
        &mut self, s: &str, chunk: &Chunk, i: usize,
    ) -> Option<String> {
        let captures = CAPTURE_NUM.captures_iter(s);
        let mut final_s = s.to_string();
        for capture in captures {
            let capture_str = capture.get(1).unwrap().as_str();
            let capture_num_res = capture_str.parse::<usize>();
            let capture_num =
                match capture_num_res {
                    Ok(n) => { n }
                    Err(_) => {
                        print_error(chunk, i, "invalid stack element");
                        return None;
                    }
                };
            let capture_el_rr_opt =
                self.stack.get(self.stack.len() - 1 - capture_num);
            match capture_el_rr_opt {
                Some(capture_el_rr) => {
                    let capture_el_rrb = capture_el_rr.borrow();
                    let capture_el_str_pre = capture_el_rrb.to_string();
                    let capture_el_str_opt = to_string_2(&capture_el_str_pre);
                    match capture_el_str_opt {
                        Some(capture_el_str) => {
                            let capture_str_with_brackets = format!("\\{{{}\\}}", capture_str);
                            let cswb_regex = Regex::new(&capture_str_with_brackets).unwrap();
                            final_s = cswb_regex.replace_all(&final_s, capture_el_str).to_string();
                        }
                        _ => {
                            print_error(chunk, i, "unable to parse command");
                            return None;
                        }
                    }
                }
                None => {
                    let err_str = format!("stack element {} not present", capture_num);
                    print_error(chunk, i, &err_str);
                    return None;
                }
            }
        }
        while CAPTURE_WITHOUT_NUM.is_match(&final_s) {
            if self.stack.len() < 1 {
                print_error(chunk, i, "no more elements to pop from stack");
                return None;
            }
	    let value_rr = self.stack.pop().unwrap();
	    let value_rrb = value_rr.borrow();
	    let value_pre = value_rrb.to_string();
	    let value_opt = to_string_2(&value_pre);
            match value_opt {
                Some(s) => {
                    final_s = CAPTURE_WITHOUT_NUM.replace(&final_s, s).to_string();
                }
                _ => {
                    print_error(chunk, i, "unable to parse command");
                    return None;
                }
            }
        }
        let homedir_res = std::env::var("HOME");
        match homedir_res {
            Ok(homedir) => {
                let s = " ".to_owned() + &homedir;
                final_s = HOME_DIR_TILDE.replace_all(&final_s, &*s).to_string();
            }
            _ => {}
        }
        return Some(final_s);
    }
fn prepare_command ( & mut self , s : & str , chunk : & Chunk , i : usize , ) -> Option < String > { let captures = CAPTURE_NUM . captures_iter ( s ) ; let mut final_s = s . to_string ( ) ; for capture in captures { let capture_str = capture . get ( 1 ) . unwrap ( ) . as_str ( ) ; let capture_num_res = capture_str . parse :: < usize > ( ) ; let capture_num = match capture_num_res { Ok ( n ) => { n } Err ( _ ) => { print_error ( chunk , i , " " ) ; return None ; } } ; let capture_el_rr_opt = self . stack . get ( self . stack . len ( ) - 1 - capture_num ) ; match capture_el_rr_opt { Some ( capture_el_rr ) => { let capture_el_rrb = capture_el_rr . borrow ( ) ; let capture_el_str_pre = capture_el_rrb . to_string ( ) ; let capture_el_str_opt = to_string_2 ( & capture_el_str_pre ) ; match capture_el_str_opt { Some ( capture_el_str ) => { let capture_str_with_brackets = format ! ( " \\ \\ " capture_str ) ; let cswb_regex = Regex :: new ( & capture_str_with_brackets ) . unwrap ( ) ; final_s = cswb_regex . replace_all ( & final_s , capture_el_str ) . to_string ( ) ; } _ => { print_error ( chunk , i , " " ) ; return None ; } } } None => { let err_str = format ! ( " " capture_num ) ; print_error ( chunk , i , & err_str ) ; return None ; } } } while CAPTURE_WITHOUT_NUM . is_match ( & final_s ) { if self . stack . len ( ) < 1 { print_error ( chunk , i , " " ) ; return None ; } let value_rr = self . stack . pop ( ) . unwrap ( ) ; let value_rrb = value_rr . borrow ( ) ; let value_pre = value_rrb . to_string ( ) ; let value_opt = to_string_2 ( & value_pre ) ; match value_opt { Some ( s ) => { final_s = CAPTURE_WITHOUT_NUM . replace ( & final_s , s ) . to_string ( ) ; } _ => { print_error ( chunk , i , " " ) ; return None ; } } } let homedir_res = std :: env :: var ( " " ) ; match homedir_res { Ok ( homedir ) => { let s = " " . to_owned ( ) + & homedir ; final_s = HOME_DIR_TILDE . replace_all ( & final_s , & * s ) . to_string ( ) ; } _ => { } } return Some ( final_s ) ; }
Takes a command string, substitutes for the {num} and {}
stack element placeholders as well as the ~ home directory
placeholder, and returns the resulting string.
Takes a command string substitutes for the { num } and {} stack element placeholders as well as the ~ home directory placeholder and returns the resulting string .
&mut self
s
&str
chunk
&Chunk
i
usize