domain: add optional on_line callback #15
3 changed files with 13 additions and 5 deletions
|
@ -51,6 +51,7 @@ pub fn execute(args: &Args) -> Result<(), Error> {
|
|||
content: &content,
|
||||
writer,
|
||||
sections: &args.sections,
|
||||
on_line: None,
|
||||
})
|
||||
.map_err(Error::Switch)
|
||||
}
|
||||
|
|
|
@ -44,7 +44,7 @@ impl Section {
|
|||
}
|
||||
}
|
||||
|
||||
struct SectionInfo {
|
||||
pub(crate) struct SectionInfo {
|
||||
enable_variable: bool,
|
||||
disable_variable: bool,
|
||||
}
|
||||
|
|
|
@ -33,6 +33,8 @@ impl std::fmt::Display for Error {
|
|||
|
||||
impl std::error::Error for Error {}
|
||||
|
||||
pub type OnLineFn = Box<dyn Fn(Option<&SectionInfo>, &String)>;
|
||||
|
||||
pub struct Request<'args, W>
|
||||
where
|
||||
W: Write,
|
||||
|
@ -40,6 +42,7 @@ where
|
|||
pub content: &'args str,
|
||||
pub writer: W,
|
||||
pub sections: &'args [String],
|
||||
pub on_line: Option<OnLineFn>,
|
||||
}
|
||||
|
||||
pub fn execute<W>(req: Request<W>) -> Result<(), Error>
|
||||
|
@ -54,14 +57,14 @@ where
|
|||
.map(|s| Section::parse(s.as_str()))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let mut current_sections: Option<SectionInfo> = None;
|
||||
let mut current_section: Option<SectionInfo> = None;
|
||||
|
||||
for line in req.content.split_inclusive('\n') {
|
||||
let new_line = if is_section_end(line) {
|
||||
current_sections = None;
|
||||
current_section = None;
|
||||
line.to_string()
|
||||
} 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<_>>();
|
||||
SectionInfo {
|
||||
enable_variable: should_enable_variable(&choose_sections, ¤t_sections),
|
||||
|
@ -69,7 +72,7 @@ where
|
|||
}
|
||||
});
|
||||
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 is_var = is_variable(trimmed_line);
|
||||
if is_var && section_info.enable_variable {
|
||||
|
@ -86,6 +89,10 @@ where
|
|||
writer
|
||||
.write_all(new_line.as_bytes())
|
||||
.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)
|
||||
|
|
Loading…
Reference in a new issue