//! Per-hunk review state. //! //! Tracks which hunks of one commit the reviewer has accepted. Each reviewed //! commit owns its own [`Review`], since the decoded hunk ids restart per //! commit. The state is plainly owned by the session; the commit's viewer //! decorator receives immutable snapshots of it (see [`crate::decorate`]) //! whenever it changes. use std::collections::BTreeSet; use rustik_diff::HunkId; /// Set of hunks visible in commit diffs. #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] pub enum HunkVisibility { /// Show only hunks pending acceptance. #[default] Pending, /// Show all hunks. All, } impl HunkVisibility { /// Switches between pending and all hunks. pub fn toggle(&mut self) { *self = match self { Self::Pending => Self::All, Self::All => Self::Pending, }; } } /// Visibility of the active file outside its diff hunks. #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] pub enum FileVisibility { /// Show bounded diff context. #[default] Context, /// Show the full file. Full, } /// Visibility modes reported by the active review view. #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] pub struct Visibility { /// Set of hunks visible in the diff. pub hunks: HunkVisibility, /// Visibility of the active file. pub file: FileVisibility, } /// Per-hunk acceptance state for one commit. #[derive(Clone, Debug, Default)] pub struct Review { /// Hunks the reviewer has accepted. accepted: BTreeSet, } impl Review { /// Creates empty review state with no accepted hunks. pub fn new() -> Self { Self::default() } /// Returns whether `hunk` is accepted. pub fn is_accepted(&self, hunk: HunkId) -> bool { self.accepted.contains(&hunk) } /// Returns the set of accepted hunks. pub fn accepted(&self) -> &BTreeSet { &self.accepted } /// Replaces the accepted hunk set. pub fn replace(&mut self, accepted: BTreeSet) { self.accepted = accepted; } /// Returns whether every hunk in `hunks` has been accepted. pub fn accepts_all(&self, hunks: impl IntoIterator) -> bool { let mut hunks = hunks.into_iter().peekable(); hunks.peek().is_some() && hunks.all(|hunk| self.accepted.contains(&hunk)) } }