coven_sync/
upload_phase.rs1use crate::files::{UploadPhase, UploadWait};
10use crate::sync_status::{Facts, Progress, Upload};
11use crate::{PendingRecord, SyncWait};
12use coven_database::{DbError, TransferHolds, UploadPhaseFacts, UploadPhaseRow};
13
14impl SyncWait {
15 fn held_transfers(self) -> Option<UploadWait> {
19 match self {
20 Self::NotSyncing => Some(UploadWait::NotSyncing),
21 Self::CoolingDown { until } => Some(UploadWait::CoolingDown { until }),
22 Self::KeyCopy { .. } | Self::Snapshot { .. } => Some(UploadWait::Sync),
23 Self::Retrying { .. } => None,
24 }
25 }
26}
27
28impl Facts {
29 pub(crate) fn upload_phase(&self, holds: &TransferHolds, upload: &Upload) -> UploadPhase {
33 match upload_phase(self.held_all(), holds, upload, |record| record) {
34 Phase::Uploading => UploadPhase::Uploading,
35 Phase::Stored => UploadPhase::Stored,
36 Phase::Waiting(wait) => UploadPhase::Waiting(wait),
37 Phase::Failed(record) => UploadPhase::Failed(record.clone()),
38 }
39 }
40}
41
42enum Phase<R> {
45 Uploading,
46 Stored,
47 Waiting(UploadWait),
48 Failed(R),
49}
50
51fn upload_phase<'a, R>(
60 held_all: Option<SyncWait>,
61 holds: &TransferHolds,
62 upload: &'a Upload<R>,
63 record_of: impl Fn(&R) -> &PendingRecord,
64) -> Phase<&'a R> {
65 match upload.progress {
66 Progress::Moving => Phase::Uploading,
67 Progress::Stored => Phase::Stored,
68 Progress::Queued => {
69 if let Some(record) =
70 upload.record.as_ref().filter(|record| record_of(record).reason.names_a_state())
71 {
72 return Phase::Failed(record);
73 }
74 Phase::Waiting(if let Some(held) = held_all.and_then(SyncWait::held_transfers) {
75 held
76 } else if holds.paused && !upload.bound {
77 UploadWait::Paused
78 } else if holds.unmetered_only
79 && holds.metered
80 && crate::files::held_on_metered(upload.bound)
81 {
82 UploadWait::Metered
83 } else if holds.keys_changing && !upload.bound {
84 UploadWait::KeysChanging
85 } else if let Some(at) = upload.retry_at {
86 UploadWait::Retrying { at }
87 } else {
88 UploadWait::Queued
89 })
90 }
91 }
92}
93
94fn upload_phase_row(facts: UploadPhaseFacts<PendingRecord>) -> Result<UploadPhaseRow, DbError> {
99 let last = crate::sync_status::last_pass(facts.last_pass)?;
100 let original = facts.original.as_deref();
101 let record = facts.own.or_else(|| {
102 facts.moving.filter(|(_, record)| crate::files::names_original(&record.reason, original))
103 });
104 let upload = Upload {
105 progress: if facts.sending {
106 Progress::Moving
107 } else if facts.stored {
108 Progress::Stored
109 } else {
110 Progress::Queued
111 },
112 bound: facts.bound,
113 retry_at: facts.retry_at,
114 record,
115 };
116 let held_all = crate::sync_status::held_all(facts.sync, last.as_ref());
117 Ok(match upload_phase(held_all, &facts.holds, &upload, |(_, record)| record) {
119 Phase::Uploading => UploadPhaseRow::Uploading,
120 Phase::Stored => UploadPhaseRow::Stored,
121 Phase::Waiting(wait) => UploadPhaseRow::Waiting(wait.kind()),
122 Phase::Failed((position, _)) => UploadPhaseRow::Failed(*position),
123 })
124}
125
126pub fn sync_functions() -> coven_database::SyncFunctions {
132 coven_database::SyncFunctions::new(crate::sync_status::SyncStatus::row, upload_phase_row)
133}
134
135#[cfg(test)]
136#[path = "upload_phase_tests.rs"]
137mod tests;