mod names;
mod session;
mod words;
use std::{
collections::{BTreeSet, HashMap},
ops::Range,
};
pub(crate) use names::name_of;
pub use session::Session;
use crate::{Finding, RepoPath};
pub const WARNING: &str = "CLI-4_AC-49";
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Pair {
pub path: RepoPath,
pub name: String,
pub fragment: bool,
pub expected: String,
pub actual: String,
pub source: Option<Source>,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Source {
pub path: RepoPath,
pub range: Option<Range<u32>>,
pub lines: Vec<u32>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
struct Reading {
current: bool,
line: Option<usize>,
warning: Option<Finding>,
unsettled: bool,
}
#[derive(Debug, Clone, Default)]
pub struct Layout {
readings: HashMap<Pair, Reading>,
unsettled: BTreeSet<RepoPath>,
}
impl Layout {
pub fn same(&self, pair: &Pair) -> bool {
pair.expected == pair.actual || self.readings.get(pair).is_some_and(|r| r.current)
}
pub fn warning(&self, pair: &Pair) -> Option<Finding> {
self.readings.get(pair).and_then(|r| r.warning.clone())
}
pub fn line(&self, pair: &Pair) -> Option<usize> {
self.readings
.get(pair)
.filter(|r| !r.current)
.and_then(|r| r.line)
}
pub fn unsettled(&self) -> &BTreeSet<RepoPath> {
&self.unsettled
}
pub(crate) fn holds(&self, pair: &Pair) -> bool {
self.readings.contains_key(pair)
}
pub fn extend(&mut self, other: Layout) {
self.readings.extend(other.readings);
self.unsettled.extend(other.unsettled);
}
fn insert(&mut self, pair: Pair, reading: Reading) {
if reading.unsettled {
self.unsettled.insert(pair.path.clone());
}
self.readings.insert(pair, reading);
}
}
pub fn stale_message(written: &str, source: Option<&RepoPath>, line: Option<usize>) -> String {
match (source, line) {
(Some(source), Some(line)) => {
format!("`sokf:include {written}` is stale: `{source}` differs from line {line}")
}
_ => format!("`sokf:include {written}` is stale"),
}
}
#[cfg(test)]
mod tests;