quick_replace/
worker.rs

1use std::{borrow::Cow, fs::File, io::{self, Write}};
2
3/// Finds and returns the number of string matches
4pub fn find_matches(contents: &Cow<str>, from: &String) -> usize {
5    let string_matches = contents.matches(&*from).into_iter().count();
6    println!("Found {:#?} instances of {:#?}", string_matches, from);
7
8    string_matches
9}
10
11/// Replaces the from_string to to_string
12pub fn replace(contents: Cow<str>, from: &String, to: &String) -> String {
13    println!("Replacing {:?} to {:?}..", from, to);
14    contents.replace(&*from, &to)
15}
16
17/// Creates a new file with the string replaced contents
18pub fn create_file_and_put_contents(content_to_write: String, new_path: &String) -> io::Result<()> {
19    let mut new_file = File::create(new_path)?;
20    println!("Saving to {:?}", new_path);
21    new_file.write_all(content_to_write.as_bytes())?;
22
23    Ok(())
24}