Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Line by line mode #254

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/replacer/line_replacer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#[derive(Debug)]
pub struct Pair {
regex: regex::Regex,
rep: String,
}

#[derive(Debug)]
pub enum Opt {
ReplaceLineIfMatch,
AnotherOption,
}

pub fn replace_line<P: AsRef<[Pair]>, S: AsRef<str>>(
pairs: P,
opts: Vec<Opt>,
content: S,
) -> Vec<String> {
let pairs = pairs.as_ref();
let content = content.as_ref();

let mut lines = content.lines().map(|x| x.to_owned()).collect::<Vec<_>>();

for opt in opts {
match opt {
Opt::ReplaceLineIfMatch => replace_line_if_match(pairs, &mut lines),
Opt::AnotherOption => other(pairs, &mut lines),
}
}
lines
}

fn replace_line_if_match(pairs: &[Pair], lines: &mut Vec<String>) {
for line in lines.iter_mut() {
for Pair { regex, rep } in pairs {
if regex.is_match(&line) {
*line = rep.clone();
}
}
}
}

fn other(pairs: &[Pair], lines: &mut Vec<String>) {
unimplemented!()
}
File renamed without changes.