gnostr_asyncgit/
fetch_job.rs1use std::sync::{Arc, Mutex};
4
5use crate::{
6 asyncjob::{AsyncJob, RunParams},
7 error::Result,
8 sync::{cred::BasicAuthCredential, remotes::fetch_all, RepoPath},
9 AsyncGitNotification, ProgressPercent,
10};
11
12enum JobState {
13 Request(Option<BasicAuthCredential>),
14 Response(Result<()>),
15}
16
17#[derive(Clone)]
19pub struct AsyncFetchJob {
20 state: Arc<Mutex<Option<JobState>>>,
21 repo: RepoPath,
22}
23
24impl AsyncFetchJob {
26 pub fn new(repo: RepoPath, basic_credential: Option<BasicAuthCredential>) -> Self {
28 Self {
29 repo,
30 state: Arc::new(Mutex::new(Some(JobState::Request(basic_credential)))),
31 }
32 }
33}
34
35impl AsyncJob for AsyncFetchJob {
36 type Notification = AsyncGitNotification;
37 type Progress = ProgressPercent;
38
39 fn run(
40 &mut self,
41 _params: RunParams<Self::Notification, Self::Progress>,
42 ) -> Result<Self::Notification> {
43 if let Ok(mut state) = self.state.lock() {
44 *state = state.take().map(|state| match state {
45 JobState::Request(basic_credentials) => {
46 let result = fetch_all(&self.repo, &basic_credentials, &None);
48
49 JobState::Response(result)
50 }
51 JobState::Response(result) => JobState::Response(result),
52 });
53 }
54
55 Ok(AsyncGitNotification::Fetch)
56 }
57}