Skip to main content

samod_core/
doc_search.rs

1use std::collections::HashMap;
2
3use crate::ConnectionId;
4
5/// The state of a search for a document
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct DocSearch {
8    pub(crate) phase: DocSearchPhase,
9    pub(crate) pending_connections: Vec<url::Url>,
10}
11
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub enum DocSearchPhase {
14    Loading,
15    Searching(HashMap<ConnectionId, PeerRequestState>),
16    Ready,
17}
18
19impl DocSearch {
20    pub fn phase(&self) -> &DocSearchPhase {
21        &self.phase
22    }
23
24    pub fn pending_connections(&self) -> &[url::Url] {
25        &self.pending_connections
26    }
27
28    pub fn is_currently_unavailable(&self) -> bool {
29        let DocSearchPhase::Searching(peers) = &self.phase else {
30            return false;
31        };
32        if !self.pending_connections.is_empty() {
33            return false;
34        }
35        peers
36            .values()
37            .all(|p| matches!(p, PeerRequestState::Unavailable))
38    }
39}
40
41/// State of searching with a specific peer.
42#[derive(Debug, Clone, PartialEq, Eq)]
43pub enum PeerRequestState {
44    /// We've asked this peer and are waiting for a response.
45    Requested,
46    /// This peer doesn't have the document.
47    Unavailable,
48    /// We're syncing the document from this peer.
49    Syncing,
50    /// The peer has the document and we are in sync (Ready phase, idle).
51    Available,
52}