vissue_tui/backend.rs
1//! Sync board facade shared by the file-backed and socket-backed clients.
2
3use vissue_core::config::Layout;
4use vissue_core::error::Error;
5use vissue_core::views::{
6 AgendaRow, ClaimRow, Excerpt, IssueDetail, ListQuery, RelatedHit, SearchHit, TreeNode,
7};
8
9/// Which store the board is talking to.
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub enum BackendKind {
12 /// File-backed [`crate::CoreBackend`].
13 Core,
14 /// Socket-backed `ControlBackend`.
15 Control,
16}
17
18/// One page of list/ready rows.
19#[derive(Debug, Clone, Default)]
20pub struct ListPage {
21 /// Rows for this page. Empty when [`Self::unchanged`].
22 pub issues: Vec<vissue_core::views::IssueRow>,
23 /// Total issues in the catalog on serve; core repeats [`Self::matched`].
24 pub total: u64,
25 /// Rows that matched the query.
26 pub matched: u64,
27 /// Serve catalog revision. Core is 0.
28 pub revision: u64,
29 /// File-watcher generation.
30 pub generation: u64,
31 /// Serve says the catalog is unchanged since `since_revision`.
32 pub unchanged: bool,
33}
34
35/// Outcome of claim, note, or update.
36#[derive(Debug, Clone)]
37pub struct MutResult {
38 /// Mutation succeeded.
39 pub ok: bool,
40 /// Status text from the op. Core includes a trailing newline.
41 pub report: String,
42 /// Issue after the write, when the backend returns one.
43 pub issue: Option<IssueDetail>,
44 /// Serve revision after the write. Core is 0.
45 pub revision: u64,
46 /// File-watcher generation after the write.
47 pub generation: u64,
48}
49
50/// Fields `issue/update` accepts.
51#[derive(Debug, Clone, Default)]
52pub struct UpdateReq {
53 /// Issue to change.
54 pub id: String,
55 /// New org TODO state, if any.
56 pub state: Option<String>,
57 /// New priority letter, if any.
58 pub priority: Option<char>,
59 /// Blocker id to add.
60 pub block: Option<String>,
61 /// Blocker id to drop.
62 pub unblock: Option<String>,
63}
64
65/// Drops `since_revision` for one fetch after attach.
66#[derive(Debug)]
67pub struct SinceGate {
68 skip_once: std::sync::atomic::AtomicBool,
69}
70
71impl SinceGate {
72 /// After `initialize`, the next list must not send a core generation.
73 pub fn after_attach() -> Self {
74 Self {
75 skip_once: std::sync::atomic::AtomicBool::new(true),
76 }
77 }
78
79 /// Consume the skip flag, or return `Some(revision)` when `revision > 0`.
80 pub fn next(&self, revision: u64) -> Option<u64> {
81 if self
82 .skip_once
83 .swap(false, std::sync::atomic::Ordering::SeqCst)
84 {
85 None
86 } else if revision > 0 {
87 Some(revision)
88 } else {
89 None
90 }
91 }
92
93 /// Next list/ready must omit `since_revision` (pane or query changed).
94 pub fn invalidate(&self) {
95 self.skip_once
96 .store(true, std::sync::atomic::Ordering::SeqCst);
97 }
98}
99
100/// Read and mutate the board. Implementations are `CoreBackend` and
101/// `ControlBackend`.
102pub trait BoardBackend: Send + Sync + std::fmt::Debug {
103 /// Vault this backend reads and writes.
104 fn layout(&self) -> &Layout;
105 /// File-watcher generation.
106 fn generation(&self) -> u64;
107 /// Serve catalog revision. Core is always 0.
108 fn revision(&self) -> u64;
109 /// Which store this backend is.
110 fn live(&self) -> BackendKind;
111 /// Claim and update identity (core: constructor; control: serve `initialize`).
112 fn identity(&self) -> &str;
113
114 /// Filtered issue list for the List pane.
115 ///
116 /// # Errors
117 ///
118 /// Returns an error if the list cannot be loaded.
119 fn list(&self, q: ListQuery) -> Result<ListPage, Error>;
120 /// Actionable ready queue, optionally scoped to `project`.
121 ///
122 /// # Errors
123 ///
124 /// Returns an error if the ready list cannot be loaded.
125 fn ready(&self, project: Option<&str>) -> Result<ListPage, Error>;
126 /// Full metadata for one issue.
127 ///
128 /// # Errors
129 ///
130 /// Returns an error if the issue does not exist or cannot be fetched.
131 fn get(&self, id: &str) -> Result<IssueDetail, Error>;
132 /// On-disk heading range, capped and screened for secrets.
133 ///
134 /// # Errors
135 ///
136 /// Returns an error if the issue does not exist or its file cannot be read.
137 fn excerpt(&self, id: &str) -> Result<Excerpt, Error>;
138 /// Title and body search hits, capped at `limit`.
139 ///
140 /// # Errors
141 ///
142 /// Returns an error if search cannot run.
143 fn search(&self, query: &str, limit: usize) -> Result<Vec<SearchHit>, Error>;
144 /// Open claims, optionally filtered by holder and project.
145 ///
146 /// # Errors
147 ///
148 /// Returns an error if claims cannot be listed.
149 fn claims(&self, holder: Option<&str>, project: Option<&str>) -> Result<Vec<ClaimRow>, Error>;
150 /// Deadlines and scheduled dates inside `days`.
151 ///
152 /// # Errors
153 ///
154 /// Returns an error if the agenda cannot be listed.
155 fn agenda(&self, days: i64, project: Option<&str>) -> Result<Vec<AgendaRow>, Error>;
156 /// Parent and child tree rooted at `id`.
157 ///
158 /// # Errors
159 ///
160 /// Returns an error if the issue does not exist or the tree cannot be built.
161 fn tree(&self, id: &str) -> Result<TreeNode, Error>;
162 /// Related issues by graph walk and text overlap.
163 ///
164 /// # Errors
165 ///
166 /// Returns an error if the issue does not exist or related hits cannot be scored.
167 fn related(&self, id: &str, depth: usize, limit: usize) -> Result<Vec<RelatedHit>, Error>;
168 /// Project names under the layout prefix.
169 ///
170 /// # Errors
171 ///
172 /// Returns an error if the project list cannot be read.
173 fn projects(&self) -> Result<Vec<String>, Error>;
174 /// Claim `id` as [`Self::identity`]. `force` takes over another holder.
175 ///
176 /// # Errors
177 ///
178 /// Returns an error if the issue does not exist, is DONE or CANCELLED, is
179 /// held by another identity without `force`, or the write fails.
180 fn claim(&self, id: &str, force: bool) -> Result<MutResult, Error>;
181 /// Append a one-line logbook note.
182 ///
183 /// # Errors
184 ///
185 /// Returns an error if the issue does not exist, the text is empty, or the
186 /// write fails.
187 fn note(&self, id: &str, text: &str) -> Result<MutResult, Error>;
188 /// Change state, priority, or blocker edges.
189 ///
190 /// # Errors
191 ///
192 /// Returns an error if the issue does not exist, the change is refused, or
193 /// the write fails.
194 fn update(&self, req: UpdateReq) -> Result<MutResult, Error>;
195 /// Same metadata as [`Self::get`]; control also marks the issue opened.
196 ///
197 /// # Errors
198 ///
199 /// Returns an error if the issue does not exist or cannot be fetched.
200 fn open(&self, id: &str) -> Result<IssueDetail, Error>;
201
202 /// Core: wait on the file generation. Control: wait for `vault/changed`.
203 ///
204 /// # Errors
205 ///
206 /// Returns an error if the wait cannot be started or a newer catalog cannot
207 /// be re-read.
208 fn wait(&self, last: u64, timeout_ms: u64) -> Result<u64, Error>;
209
210 /// Last `since_revision` sent on list/ready. `None` means the field was
211 /// omitted. Default is "not recorded".
212 fn last_since_revision(&self) -> Option<Option<u64>> {
213 None
214 }
215
216 /// Drop `since_revision` on the next list/ready. Serve `unchanged` is
217 /// catalog-wide, so a pane or project change must fetch a full page.
218 fn invalidate_since(&self) {}
219
220 /// Re-read the files. Core uses this after an out-of-band write such as
221 /// `ops::create`. Control is a no-op; serve sees the file event.
222 ///
223 /// # Errors
224 ///
225 /// Returns an error if the catalog cannot be re-read. Control never fails.
226 fn refresh(&self) -> Result<(), Error> {
227 Ok(())
228 }
229}
230
231#[cfg(test)]
232mod tests {
233 use super::SinceGate;
234
235 #[test]
236 fn after_attach_first_list_omits_since_revision() {
237 let gate = SinceGate::after_attach();
238 assert_eq!(gate.next(7), None);
239 assert_eq!(gate.next(7), Some(7));
240 assert_eq!(gate.next(8), Some(8));
241 }
242
243 #[test]
244 fn a_zero_revision_never_sends_since() {
245 let gate = SinceGate::after_attach();
246 assert_eq!(gate.next(0), None);
247 assert_eq!(gate.next(0), None);
248 }
249
250 #[test]
251 fn invalidate_omits_the_next_since() {
252 let gate = SinceGate::after_attach();
253 assert_eq!(gate.next(7), None);
254 assert_eq!(gate.next(7), Some(7));
255 gate.invalidate();
256 assert_eq!(gate.next(7), None);
257 assert_eq!(gate.next(7), Some(7));
258 }
259}