domain: add optional on_line callback

This commit is contained in:
Dmitriy Pleshevskiy 2022-11-08 00:07:21 +03:00
parent 25d28531a1
commit 2be25a330f
Signed by: pleshevskiy
GPG Key ID: 1B59187B161C0215
3 changed files with 13 additions and 5 deletions

View File

@ -51,6 +51,7 @@ pub fn execute(args: &Args) -> Result<(), Error> {
content: &content, content: &content,
writer, writer,
sections: &args.sections, sections: &args.sections,
on_line: None,
}) })
.map_err(Error::Switch) .map_err(Error::Switch)
} }

View File

@ -44,7 +44,7 @@ impl Section {
} }
} }
struct SectionInfo { pub(crate) struct SectionInfo {
enable_variable: bool, enable_variable: bool,
disable_variable: bool, disable_variable: bool,
} }

View File

@ -33,6 +33,8 @@ impl std::fmt::Display for Error {
impl std::error::Error for Error {} impl std::error::Error for Error {}
pub type OnLineFn = Box<dyn Fn(Option<&SectionInfo>, &String)>;
pub struct Request<'args, W> pub struct Request<'args, W>
where where
W: Write, W: Write,
@ -40,6 +42,7 @@ where
pub content: &'args str, pub content: &'args str,
pub writer: W, pub writer: W,
pub sections: &'args [String], pub sections: &'args [String],
pub on_line: Option<OnLineFn>,
} }
pub fn execute<W>(req: Request<W>) -> Result<(), Error> pub fn execute<W>(req: Request<W>) -> Result<(), Error>
@ -54,14 +57,14 @@ where
.map(|s| Section::parse(s.as_str())) .map(|s| Section::parse(s.as_str()))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let mut current_sections: Option<SectionInfo> = None; let mut current_section: Option<SectionInfo> = None;
for line in req.content.split_inclusive('\n') { for line in req.content.split_inclusive('\n') {
let new_line = if is_section_end(line) { let new_line = if is_section_end(line) {
current_sections = None; current_section = None;
line.to_string() line.to_string()
} else if let Some(section_info) = line.strip_prefix("### ") { } else if let Some(section_info) = line.strip_prefix("### ") {
current_sections = section_info.split_whitespace().next().map(|r| { current_section = section_info.split_whitespace().next().map(|r| {
let current_sections = r.split(',').map(Section::parse).collect::<Vec<_>>(); let current_sections = r.split(',').map(Section::parse).collect::<Vec<_>>();
SectionInfo { SectionInfo {
enable_variable: should_enable_variable(&choose_sections, &current_sections), enable_variable: should_enable_variable(&choose_sections, &current_sections),
@ -69,7 +72,7 @@ where
} }
}); });
line.to_string() line.to_string()
} else if let Some(section_info) = current_sections.as_ref() { } else if let Some(section_info) = current_section.as_ref() {
let trimmed_line = line.trim_start_matches(['#', ' ']); let trimmed_line = line.trim_start_matches(['#', ' ']);
let is_var = is_variable(trimmed_line); let is_var = is_variable(trimmed_line);
if is_var && section_info.enable_variable { if is_var && section_info.enable_variable {
@ -86,6 +89,10 @@ where
writer writer
.write_all(new_line.as_bytes()) .write_all(new_line.as_bytes())
.map_err(|_| Error::WriteData)?; .map_err(|_| Error::WriteData)?;
if let Some(on_line) = req.on_line.as_ref() {
on_line(current_section.as_ref(), &new_line);
}
} }
writer.flush().map_err(|_| Error::WriteData) writer.flush().map_err(|_| Error::WriteData)