crates/brain/src/review.rs 2.3 KiB raw
1
//! Per-hunk review state.
2
//!
3
//! Tracks which hunks of one commit the reviewer has accepted. Each reviewed
4
//! commit owns its own [`Review`], since the decoded hunk ids restart per
5
//! commit. The state is plainly owned by the session; the commit's viewer
6
//! decorator receives immutable snapshots of it (see [`crate::decorate`])
7
//! whenever it changes.
8
9
use std::collections::BTreeSet;
10
11
use rustik_diff::HunkId;
12
13
/// Set of hunks visible in commit diffs.
14
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
15
pub enum HunkVisibility {
16
    /// Show only hunks pending acceptance.
17
    #[default]
18
    Pending,
19
    /// Show all hunks.
20
    All,
21
}
22
23
impl HunkVisibility {
24
    /// Switches between pending and all hunks.
25
    pub fn toggle(&mut self) {
26
        *self = match self {
27
            Self::Pending => Self::All,
28
            Self::All => Self::Pending,
29
        };
30
    }
31
}
32
33
/// Visibility of the active file outside its diff hunks.
34
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
35
pub enum FileVisibility {
36
    /// Show bounded diff context.
37
    #[default]
38
    Context,
39
    /// Show the full file.
40
    Full,
41
}
42
43
/// Visibility modes reported by the active review view.
44
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
45
pub struct Visibility {
46
    /// Set of hunks visible in the diff.
47
    pub hunks: HunkVisibility,
48
    /// Visibility of the active file.
49
    pub file: FileVisibility,
50
}
51
52
/// Per-hunk acceptance state for one commit.
53
#[derive(Clone, Debug, Default)]
54
pub struct Review {
55
    /// Hunks the reviewer has accepted.
56
    accepted: BTreeSet<HunkId>,
57
}
58
59
impl Review {
60
    /// Creates empty review state with no accepted hunks.
61
    pub fn new() -> Self {
62
        Self::default()
63
    }
64
65
    /// Returns whether `hunk` is accepted.
66
    pub fn is_accepted(&self, hunk: HunkId) -> bool {
67
        self.accepted.contains(&hunk)
68
    }
69
70
    /// Returns the set of accepted hunks.
71
    pub fn accepted(&self) -> &BTreeSet<HunkId> {
72
        &self.accepted
73
    }
74
75
    /// Replaces the accepted hunk set.
76
    pub fn replace(&mut self, accepted: BTreeSet<HunkId>) {
77
        self.accepted = accepted;
78
    }
79
80
    /// Returns whether every hunk in `hunks` has been accepted.
81
    pub fn accepts_all(&self, hunks: impl IntoIterator<Item = HunkId>) -> bool {
82
        let mut hunks = hunks.into_iter().peekable();
83
84
        hunks.peek().is_some() && hunks.all(|hunk| self.accepted.contains(&hunk))
85
    }
86
}