fn inject_text_in_status_block_of_a_record(
    pathbuf_of_file: &PathBuf,
    translated_status_heading_string: &String,
    inject_line: &String,
    start_of_block: bool,
    replace_block: bool,
    remove_strings: &[&String],
) -> Result<(), Error> {
    let mut temp_file_content: String = String::from("");
    let mut filetype: String = String::from("undefined");
    let mut bool_in_block: bool = false;
    let mut bool_found_search_line: bool = false; // Only used in RST files
    let mut bool_after_block: bool = false;
    let mut last_line: String = String::from("");

    let re_filetype_md = Regex::new(r"\.md$").unwrap();
    let re_filetype_rst = Regex::new(r"\.rst$").unwrap();
    let mut re_search_line = Regex::new(&String::from(translated_status_heading_string)).unwrap();
    let mut re_find_delimiter = Regex::new("").unwrap();

    if re_filetype_md.is_match(&pathbuf_of_file.display().to_string()) {
        filetype = String::from("md");
        re_find_delimiter = Regex::new(r"^\s*#+\s+\S").unwrap();
        let mut str_search_line: String = String::from(r"^\s*#+\s+");
        str_search_line.push_str(&translated_status_heading_string);
        str_search_line.push_str(r"\s*$");
        re_search_line = Regex::new(&str_search_line).unwrap();
    } else if re_filetype_rst.is_match(&pathbuf_of_file.display().to_string()) {
        filetype = String::from("rst");
        re_find_delimiter = Regex::new(r"^\s*([*]+|[#]+)\s*$").unwrap();
    }

    if let Ok(lines) = get_lines_from_a_file(&pathbuf_of_file) {
        for line in lines {
            if let Ok(line) = line {
                let mut prune_line: bool = false;
                for prune_item in remove_strings {
                    let mut str_prune_item: String = String::from(r"^\s*");
                    str_prune_item.push_str(prune_item);
                    str_prune_item.push_str(r"\s*.*$");
                    let re_prune_item = Regex::new(&str_prune_item).unwrap();
                    if re_prune_item.is_match(&line) {
                        prune_line = true;
                    }
                }
                if !prune_line {
                    if !(last_line.len() == 0 && line.len() == 0) {
                        if bool_after_block {
                            temp_file_content.push_str(&line);
                            temp_file_content.push_str("\u{000A}");
                            last_line = line;
                        } else if bool_in_block {
                            if re_find_delimiter.is_match(&line) {
                                bool_after_block = true;
                                if start_of_block == false {
                                    temp_file_content.push_str(&inject_line);
                                    temp_file_content.push_str("\u{000A}\u{000A}");
                                }
                            }
                            temp_file_content.push_str(&line);
                            temp_file_content.push_str("\u{000A}");
                            last_line = line;
                        } else if filetype == "md" {
                            temp_file_content.push_str(&line);
                            temp_file_content.push_str("\u{000A}");
                            if re_search_line.is_match(&line) {
                                bool_in_block = true;
                                if start_of_block || replace_block {
                                    temp_file_content.push_str("\u{000A}");
                                    temp_file_content.push_str(&inject_line);
                                    temp_file_content.push_str("\u{000A}");
                                    if replace_block {
                                        bool_after_block = true;
                                        temp_file_content.push_str("\u{000A}");
                                    }
                                }
                            }
                            last_line = line;
                        } else if filetype == "rst" {
                            temp_file_content.push_str(&line);
                            temp_file_content.push_str("\u{000A}");

                            if re_search_line.is_match(&line) {
                                bool_found_search_line = true;
                            } else if bool_found_search_line {
                                bool_in_block = true;
                                if start_of_block || replace_block {
                                    temp_file_content.push_str("\u{000A}");
                                    temp_file_content.push_str(&inject_line);
                                    temp_file_content.push_str("\u{000A}");
                                    if replace_block {
                                        bool_after_block = true;
                                        temp_file_content.push_str("\u{000A}");
                                    }
                                }
                            }
                            last_line = line;
                        }
                    }
                }
            }
        }
    }

    create_file(pathbuf_of_file, temp_file_content)?;
    return Ok(());
}
// This function endevours to read the content of a file, find a search string, and then either inject the string at the start or end of that block
// or replace the whole string entirely.
This function endevours to read the content of a file, find a search string, and then either inject the string at the start or end of that block
or replace the whole string entirely.
This function endevours to read the content of a file find a search string and then either inject the string at the start or end of that block or replace the whole string entirely .
fn inject_text_in_status_block_of_a_record(
    pathbuf_of_file: &PathBuf,
    translated_status_heading_string: &String,
    inject_line: &String,
    start_of_block: bool,
    replace_block: bool,
    remove_strings: &[&String],
) -> Result<(), Error> {
    let mut temp_file_content: String = String::from("");
    let mut filetype: String = String::from("undefined");
    let mut bool_in_block: bool = false;
    let mut bool_found_search_line: bool = false; // Only used in RST files
    let mut bool_after_block: bool = false;
    let mut last_line: String = String::from("");
    let re_filetype_md = Regex::new(r"\.md$").unwrap();
    let re_filetype_rst = Regex::new(r"\.rst$").unwrap();
    let mut re_search_line = Regex::new(&String::from(translated_status_heading_string)).unwrap();
    let mut re_find_delimiter = Regex::new("").unwrap();
    if re_filetype_md.is_match(&pathbuf_of_file.display().to_string()) {
        filetype = String::from("md");
        re_find_delimiter = Regex::new(r"^\s*#+\s+\S").unwrap();
        let mut str_search_line: String = String::from(r"^\s*#+\s+");
        str_search_line.push_str(&translated_status_heading_string);
        str_search_line.push_str(r"\s*$");
        re_search_line = Regex::new(&str_search_line).unwrap();
    } else if re_filetype_rst.is_match(&pathbuf_of_file.display().to_string()) {
        filetype = String::from("rst");
        re_find_delimiter = Regex::new(r"^\s*([*]+|[#]+)\s*$").unwrap();
    }
    if let Ok(lines) = get_lines_from_a_file(&pathbuf_of_file) {
        for line in lines {
            if let Ok(line) = line {
                let mut prune_line: bool = false;
                for prune_item in remove_strings {
                    let mut str_prune_item: String = String::from(r"^\s*");
                    str_prune_item.push_str(prune_item);
                    str_prune_item.push_str(r"\s*.*$");
                    let re_prune_item = Regex::new(&str_prune_item).unwrap();
                    if re_prune_item.is_match(&line) {
                        prune_line = true;
                    }
                }
                if !prune_line {
                    if !(last_line.len() == 0 && line.len() == 0) {
                        if bool_after_block {
                            temp_file_content.push_str(&line);
                            temp_file_content.push_str("\u{000A}");
                            last_line = line;
                        } else if bool_in_block {
                            if re_find_delimiter.is_match(&line) {
                                bool_after_block = true;
                                if start_of_block == false {
                                    temp_file_content.push_str(&inject_line);
                                    temp_file_content.push_str("\u{000A}\u{000A}");
                                }
                            }
                            temp_file_content.push_str(&line);
                            temp_file_content.push_str("\u{000A}");
                            last_line = line;
                        } else if filetype == "md" {
                            temp_file_content.push_str(&line);
                            temp_file_content.push_str("\u{000A}");
                            if re_search_line.is_match(&line) {
                                bool_in_block = true;
                                if start_of_block || replace_block {
                                    temp_file_content.push_str("\u{000A}");
                                    temp_file_content.push_str(&inject_line);
                                    temp_file_content.push_str("\u{000A}");
                                    if replace_block {
                                        bool_after_block = true;
                                        temp_file_content.push_str("\u{000A}");
                                    }
                                }
                            }
                            last_line = line;
                        } else if filetype == "rst" {
                            temp_file_content.push_str(&line);
                            temp_file_content.push_str("\u{000A}");
                            if re_search_line.is_match(&line) {
                                bool_found_search_line = true;
                            } else if bool_found_search_line {
                                bool_in_block = true;
                                if start_of_block || replace_block {
                                    temp_file_content.push_str("\u{000A}");
                                    temp_file_content.push_str(&inject_line);
                                    temp_file_content.push_str("\u{000A}");
                                    if replace_block {
                                        bool_after_block = true;
                                        temp_file_content.push_str("\u{000A}");
                                    }
                                }
                            }
                            last_line = line;
                        }
                    }
                }
            }
        }
    }
    create_file(pathbuf_of_file, temp_file_content)?;
    return Ok(());
}
fn inject_text_in_status_block_of_a_record ( pathbuf_of_file : & PathBuf , translated_status_heading_string : & String , inject_line : & String , start_of_block : bool , replace_block : bool , remove_strings : & [ & String ] , ) -> Result < ( ) , Error > { let mut temp_file_content : String = String :: from ( " " ) ; let mut filetype : String = String :: from ( " " ) ; let mut bool_in_block : bool = false ; let mut bool_found_search_line : bool = false ; // Only used in RST files let mut bool_after_block : bool = false ; let mut last_line : String = String :: from ( " " ) ; let re_filetype_md = Regex :: new ( r"\.md$" ) . unwrap ( ) ; let re_filetype_rst = Regex :: new ( r"\.rst$" ) . unwrap ( ) ; let mut re_search_line = Regex :: new ( & String :: from ( translated_status_heading_string ) ) . unwrap ( ) ; let mut re_find_delimiter = Regex :: new ( " " ) . unwrap ( ) ; if re_filetype_md . is_match ( & pathbuf_of_file . display ( ) . to_string ( ) ) { filetype = String :: from ( " " ) ; re_find_delimiter = Regex :: new ( r"^\s*#+\s+\S" ) . unwrap ( ) ; let mut str_search_line : String = String :: from ( r"^\s*#+\s+" ) ; str_search_line . push_str ( & translated_status_heading_string ) ; str_search_line . push_str ( r"\s*$" ) ; re_search_line = Regex :: new ( & str_search_line ) . unwrap ( ) ; } else if re_filetype_rst . is_match ( & pathbuf_of_file . display ( ) . to_string ( ) ) { filetype = String :: from ( " " ) ; re_find_delimiter = Regex :: new ( r"^\s*([*]+|[#]+)\s*$" ) . unwrap ( ) ; } if let Ok ( lines ) = get_lines_from_a_file ( & pathbuf_of_file ) { for line in lines { if let Ok ( line ) = line { let mut prune_line : bool = false ; for prune_item in remove_strings { let mut str_prune_item : String = String :: from ( r"^\s*" ) ; str_prune_item . push_str ( prune_item ) ; str_prune_item . push_str ( r"\s*.*$" ) ; let re_prune_item = Regex :: new ( & str_prune_item ) . unwrap ( ) ; if re_prune_item . is_match ( & line ) { prune_line = true ; } } if ! prune_line { if ! ( last_line . len ( ) == 0 && line . len ( ) == 0 ) { if bool_after_block { temp_file_content . push_str ( & line ) ; temp_file_content . push_str ( " \u{000A} " ) ; last_line = line ; } else if bool_in_block { if re_find_delimiter . is_match ( & line ) { bool_after_block = true ; if start_of_block == false { temp_file_content . push_str ( & inject_line ) ; temp_file_content . push_str ( " \u{000A} \u{000A} " ) ; } } temp_file_content . push_str ( & line ) ; temp_file_content . push_str ( " \u{000A} " ) ; last_line = line ; } else if filetype == " " { temp_file_content . push_str ( & line ) ; temp_file_content . push_str ( " \u{000A} " ) ; if re_search_line . is_match ( & line ) { bool_in_block = true ; if start_of_block || replace_block { temp_file_content . push_str ( " \u{000A} " ) ; temp_file_content . push_str ( & inject_line ) ; temp_file_content . push_str ( " \u{000A} " ) ; if replace_block { bool_after_block = true ; temp_file_content . push_str ( " \u{000A} " ) ; } } } last_line = line ; } else if filetype == " " { temp_file_content . push_str ( & line ) ; temp_file_content . push_str ( " \u{000A} " ) ; if re_search_line . is_match ( & line ) { bool_found_search_line = true ; } else if bool_found_search_line { bool_in_block = true ; if start_of_block || replace_block { temp_file_content . push_str ( " \u{000A} " ) ; temp_file_content . push_str ( & inject_line ) ; temp_file_content . push_str ( " \u{000A} " ) ; if replace_block { bool_after_block = true ; temp_file_content . push_str ( " \u{000A} " ) ; } } } last_line = line ; } } } } } } create_file ( pathbuf_of_file , temp_file_content ) ? ; return Ok ( ( ) ) ; }
This function endevours to read the content of a file, find a search string, and then either inject the string at the start or end of that block
or replace the whole string entirely.
This function endevours to read the content of a file find a search string and then either inject the string at the start or end of that block or replace the whole string entirely .
pathbuf_of_file
&PathBuf
translated_status_heading_string
&String
inject_line
&String
start_of_block
bool
replace_block
bool
remove_strings
&[&String]
pathbuf_of_file
&PathBuf
translated_status_heading_string
&String
inject_line
&String
start_of_block
bool
replace_block
bool
remove_strings
&[&String]