use regex::Regex;
use crate::directives::is_default_directive;
use crate::lang::{Fence, Spec};
#[derive(Clone, Debug)]
pub struct Options {
pub width: usize,
pub forced_prefix: Option<String>,
pub(crate) default_skips: bool,
pub(crate) extra_skips: Vec<Regex>,
pub(crate) ignore_markers: &'static [&'static str],
pub(crate) fences: &'static [Fence],
}
impl Options {
pub fn new(width: usize) -> Self {
Self {
width,
forced_prefix: None,
default_skips: true,
extra_skips: Vec::new(),
ignore_markers: &[],
fences: &[],
}
}
pub fn with_default_skips(mut self, on: bool) -> Self {
self.default_skips = on;
self
}
pub fn with_forced_prefix(mut self, prefix: String) -> Self {
self.forced_prefix = Some(prefix);
self
}
pub fn with_skip(mut self, pattern: &str) -> Result<Self, regex::Error> {
self.extra_skips.push(Regex::new(pattern)?);
Ok(self)
}
pub(crate) fn with_spec(mut self, spec: Spec) -> Self {
self.ignore_markers = spec.ignore_markers;
self.fences = spec.fences;
self
}
pub(crate) fn matches_skip(&self, line: &str) -> bool {
let trimmed = line.trim_start();
if self.default_skips && is_default_directive(trimmed) {
return true;
}
self.extra_skips.iter().any(|r| r.is_match(line))
}
pub(crate) fn matches_ignore_marker(&self, line: &str) -> bool {
let trimmed = line.trim_start();
self.ignore_markers.iter().any(|m| trimmed.starts_with(m))
}
}