use std::collections::HashMap;
use super::blocker::BlockerError;
use super::events::WorkEvent;
use super::reference::{WorkKind, WorkRef};
use super::schema::{DocumentRow, ParsedDocument};
use super::tracker::{WorkState, WorkStatus, WorkTrackerError};
pub type Blockers = HashMap<String, Result<bool, BlockerError>>;
pub type Statuses = HashMap<WorkRef, Result<WorkStatus, WorkTrackerError>>;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DocumentAlertItem {
Event {
event: WorkEvent,
rows: Vec<String>,
},
Stale {
row: String,
reference: WorkRef,
state: WorkState,
},
DeadBlocker {
row: String,
blocker: String,
},
Problem(String),
}
#[derive(Debug, Clone)]
pub struct DocumentReview {
document: ParsedDocument,
statuses: Statuses,
blockers: Blockers,
findings: Vec<DocumentAlertItem>,
}
impl DocumentReview {
pub(crate) const fn new(
document: ParsedDocument,
statuses: Statuses,
blockers: Blockers,
findings: Vec<DocumentAlertItem>,
) -> Self {
Self {
document,
statuses,
blockers,
findings,
}
}
#[must_use]
pub const fn document(&self) -> &ParsedDocument {
&self.document
}
#[must_use]
pub fn findings(&self) -> &[DocumentAlertItem] {
&self.findings
}
#[must_use]
pub fn status(&self, reference: &WorkRef) -> Option<&Result<WorkStatus, WorkTrackerError>> {
self.statuses.get(reference)
}
#[must_use]
pub fn blocker_holds(&self, blocker: &str) -> bool {
!matches!(self.blockers.get(blocker), Some(Ok(false)))
}
#[must_use]
pub fn waits(&self, row: &DocumentRow) -> bool {
if row
.blocker()
.is_some_and(|blocker| self.blocker_holds(blocker))
{
return true;
}
let issue = row
.references()
.iter()
.find(|reference| reference.kind() == WorkKind::Issue);
match issue.and_then(|issue| self.statuses.get(issue)) {
None => false,
Some(Ok(status)) => status.is_blocked(),
Some(Err(_)) => true,
}
}
pub fn actionable_rows(&self) -> impl Iterator<Item = &DocumentRow> {
self.document
.rows()
.iter()
.filter(|row| row.is_open() && !self.waits(row))
}
}