From b37de489b44c4485eba9d4d8b4454fc75547c8ed Mon Sep 17 00:00:00 2001 From: ardi Date: Sun, 22 Oct 2023 21:38:28 +0200 Subject: [PATCH] example implementation --- src/replacer/line_replacer.rs | 44 ++++++++++++++++++++++++++++ src/{replacer.rs => replacer/mod.rs} | 0 2 files changed, 44 insertions(+) create mode 100644 src/replacer/line_replacer.rs rename src/{replacer.rs => replacer/mod.rs} (100%) diff --git a/src/replacer/line_replacer.rs b/src/replacer/line_replacer.rs new file mode 100644 index 0000000..3ebd879 --- /dev/null +++ b/src/replacer/line_replacer.rs @@ -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, S: AsRef>( + pairs: P, + opts: Vec, + content: S, +) -> Vec { + let pairs = pairs.as_ref(); + let content = content.as_ref(); + + let mut lines = content.lines().map(|x| x.to_owned()).collect::>(); + + 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) { + 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) { + unimplemented!() +} diff --git a/src/replacer.rs b/src/replacer/mod.rs similarity index 100% rename from src/replacer.rs rename to src/replacer/mod.rs