use crate::format::{Pattern, State};
use crate::logging::{record_line_log, Log};
use log::Level::Warn;
use std::path::Path;
#[derive(Clone, Debug)]
pub struct Verbatim {
pub actual: i8,
pub visual: bool,
}
impl Verbatim {
#[must_use]
pub const fn new() -> Self {
Self {
actual: 0,
visual: false,
}
}
}
impl Default for Verbatim {
fn default() -> Self {
Self::new()
}
}
#[allow(clippy::too_many_arguments)]
pub fn get_verbatim(
line: &str,
state: &State,
logs: &mut Vec<Log>,
file: &Path,
warn: bool,
pattern: &Pattern,
verbatims_begin: &[String],
verbatims_end: &[String],
) -> Verbatim {
let diff = get_verbatim_diff(line, pattern, verbatims_begin, verbatims_end);
let actual = state.verbatim.actual + diff;
let visual = actual > 0 || state.verbatim.actual > 0;
if warn && (actual < 0) {
record_line_log(
logs,
Warn,
file,
state.linum_new,
state.linum_old,
line,
"Verbatim count is negative.",
);
}
Verbatim { actual, visual }
}
fn get_verbatim_diff(
line: &str,
pattern: &Pattern,
verbatims_begin: &[String],
verbatims_end: &[String],
) -> i8 {
if pattern.contains_env_begin
&& verbatims_begin.iter().any(|r| line.contains(r))
{
1
} else if pattern.contains_env_end
&& verbatims_end.iter().any(|r| line.contains(r))
{
-1
} else {
0
}
}