use crate::{
Finding,
grammar::{KeyDerivation, KeyScope, Only},
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Capture {
pub key: String,
pub line: usize,
pub heading_slug: Option<String>,
pub scope: KeyScope,
pub rule_id: Option<String>,
pub section: Option<usize>,
pub pairs: Option<Paired>,
pub repeats: bool,
pub spot: Spot,
pub derives: Option<KeyDerivation>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Spot {
Heading(usize),
Inline(usize, usize),
Before(usize),
}
impl Spot {
pub fn line(self) -> usize {
match self {
Spot::Heading(l) | Spot::Inline(l, _) | Spot::Before(l) => l,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Placement {
pub id: String,
pub key: String,
pub spot: Spot,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Paired {
pub with: Vec<String>,
pub by: usize,
}
pub(crate) fn rules_named(with: &[String]) -> String {
let quoted: Vec<String> = with.iter().map(|r| format!("`{r}`")).collect();
match quoted.as_slice() {
[] => "rule".to_string(),
[one] => format!("rule {one}"),
[a, b] => format!("rule {a} or {b}"),
[rest @ .., last] => format!("rule {}, or {last}", rest.join(", ")),
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Repair {
pub line: usize,
pub end_line: usize,
pub prefix: String,
pub text: String,
}
impl Repair {
pub fn heading(line: usize, end_line: usize, level: u8, text: String) -> Self {
Repair {
line,
end_line,
prefix: format!("{} ", "#".repeat(usize::from(level))),
text,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Gate {
pub line: usize,
pub only: Only,
pub rule: usize,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct CheckOutput {
pub findings: Vec<Finding>,
pub captures: Vec<Capture>,
pub repairs: Vec<Repair>,
pub gates: Vec<Gate>,
pub anchors: Vec<Placement>,
pub anchors_stale: bool,
}