mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-20 00:23:58 +01:00
Reformat project
This commit is contained in:
@@ -1,18 +1,18 @@
|
||||
use crate::error::Error::InvalidSyncDirectory;
|
||||
use crate::error::Result;
|
||||
use crate::sync::{
|
||||
apply_sync_ops, apply_sync_state_ops, compute_sync_ops, get_db_candidates, get_fs_candidates, FsCandidate,
|
||||
SyncOp,
|
||||
FsCandidate, SyncOp, apply_sync_ops, apply_sync_state_ops, compute_sync_ops, get_db_candidates,
|
||||
get_fs_candidates,
|
||||
};
|
||||
use crate::watch::{watch_directory, WatchEvent};
|
||||
use crate::watch::{WatchEvent, watch_directory};
|
||||
use chrono::Utc;
|
||||
use log::warn;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::Path;
|
||||
use tauri::ipc::Channel;
|
||||
use tauri::{command, AppHandle, Listener, Runtime};
|
||||
use tauri::{AppHandle, Listener, Runtime, command};
|
||||
use tokio::sync::watch;
|
||||
use ts_rs::TS;
|
||||
use crate::error::Error::InvalidSyncDirectory;
|
||||
|
||||
#[command]
|
||||
pub async fn calculate<R: Runtime>(
|
||||
@@ -21,7 +21,7 @@ pub async fn calculate<R: Runtime>(
|
||||
sync_dir: &Path,
|
||||
) -> Result<Vec<SyncOp>> {
|
||||
if !sync_dir.exists() {
|
||||
return Err(InvalidSyncDirectory(sync_dir.to_string_lossy().to_string()))
|
||||
return Err(InvalidSyncDirectory(sync_dir.to_string_lossy().to_string()));
|
||||
}
|
||||
|
||||
let db_candidates = get_db_candidates(&app_handle, workspace_id, sync_dir)?;
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
use crate::commands::{apply, calculate, calculate_fs, watch};
|
||||
use tauri::{
|
||||
generate_handler,
|
||||
Runtime, generate_handler,
|
||||
plugin::{Builder, TauriPlugin},
|
||||
Runtime,
|
||||
};
|
||||
|
||||
mod commands;
|
||||
|
||||
@@ -202,11 +202,7 @@ pub(crate) fn get_fs_candidates(dir: &Path) -> Result<Vec<FsCandidate>> {
|
||||
};
|
||||
|
||||
let rel_path = Path::new(&dir_entry.file_name()).to_path_buf();
|
||||
candidates.push(FsCandidate {
|
||||
rel_path,
|
||||
model,
|
||||
checksum,
|
||||
})
|
||||
candidates.push(FsCandidate { rel_path, model, checksum })
|
||||
}
|
||||
|
||||
Ok(candidates)
|
||||
@@ -236,28 +232,25 @@ pub(crate) fn compute_sync_ops(
|
||||
(None, Some(fs)) => SyncOp::DbCreate { fs: fs.to_owned() },
|
||||
|
||||
// DB unchanged <-> FS missing
|
||||
(Some(DbCandidate::Unmodified(model, sync_state)), None) => SyncOp::DbDelete {
|
||||
model: model.to_owned(),
|
||||
state: sync_state.to_owned(),
|
||||
},
|
||||
(Some(DbCandidate::Unmodified(model, sync_state)), None) => {
|
||||
SyncOp::DbDelete { model: model.to_owned(), state: sync_state.to_owned() }
|
||||
}
|
||||
|
||||
// DB modified <-> FS missing
|
||||
(Some(DbCandidate::Modified(model, sync_state)), None) => SyncOp::FsUpdate {
|
||||
model: model.to_owned(),
|
||||
state: sync_state.to_owned(),
|
||||
},
|
||||
(Some(DbCandidate::Modified(model, sync_state)), None) => {
|
||||
SyncOp::FsUpdate { model: model.to_owned(), state: sync_state.to_owned() }
|
||||
}
|
||||
|
||||
// DB added <-> FS missing
|
||||
(Some(DbCandidate::Added(model)), None) => SyncOp::FsCreate {
|
||||
model: model.to_owned(),
|
||||
},
|
||||
(Some(DbCandidate::Added(model)), None) => {
|
||||
SyncOp::FsCreate { model: model.to_owned() }
|
||||
}
|
||||
|
||||
// DB deleted <-> FS missing
|
||||
// Already deleted on FS, but sending it so the SyncState gets dealt with
|
||||
(Some(DbCandidate::Deleted(sync_state)), None) => SyncOp::FsDelete {
|
||||
state: sync_state.to_owned(),
|
||||
fs: None,
|
||||
},
|
||||
(Some(DbCandidate::Deleted(sync_state)), None) => {
|
||||
SyncOp::FsDelete { state: sync_state.to_owned(), fs: None }
|
||||
}
|
||||
|
||||
// DB unchanged <-> FS exists
|
||||
(Some(DbCandidate::Unmodified(_, sync_state)), Some(fs_candidate)) => {
|
||||
@@ -274,10 +267,7 @@ pub(crate) fn compute_sync_ops(
|
||||
// DB modified <-> FS exists
|
||||
(Some(DbCandidate::Modified(model, sync_state)), Some(fs_candidate)) => {
|
||||
if sync_state.checksum == fs_candidate.checksum {
|
||||
SyncOp::FsUpdate {
|
||||
model: model.to_owned(),
|
||||
state: sync_state.to_owned(),
|
||||
}
|
||||
SyncOp::FsUpdate { model: model.to_owned(), state: sync_state.to_owned() }
|
||||
} else if model.updated_at() < fs_candidate.model.updated_at() {
|
||||
// CONFLICT! Write to DB if the fs model is newer
|
||||
SyncOp::DbUpdate {
|
||||
@@ -286,19 +276,14 @@ pub(crate) fn compute_sync_ops(
|
||||
}
|
||||
} else {
|
||||
// CONFLICT! Write to FS if the db model is newer
|
||||
SyncOp::FsUpdate {
|
||||
model: model.to_owned(),
|
||||
state: sync_state.to_owned(),
|
||||
}
|
||||
SyncOp::FsUpdate { model: model.to_owned(), state: sync_state.to_owned() }
|
||||
}
|
||||
}
|
||||
|
||||
// DB added <-> FS anything
|
||||
(Some(DbCandidate::Added(model)), Some(_)) => {
|
||||
// This would be super rare (impossible?), so let's follow the user's intention
|
||||
SyncOp::FsCreate {
|
||||
model: model.to_owned(),
|
||||
}
|
||||
SyncOp::FsCreate { model: model.to_owned() }
|
||||
}
|
||||
|
||||
// DB deleted <-> FS exists
|
||||
@@ -389,11 +374,7 @@ pub(crate) fn apply_sync_ops<R: Runtime>(
|
||||
let (content, checksum) = model.to_file_contents(&rel_path)?;
|
||||
let mut f = File::create(&abs_path)?;
|
||||
f.write_all(&content)?;
|
||||
SyncStateOp::Create {
|
||||
model_id: model.id(),
|
||||
checksum,
|
||||
rel_path,
|
||||
}
|
||||
SyncStateOp::Create { model_id: model.id(), checksum, rel_path }
|
||||
}
|
||||
SyncOp::FsUpdate { model, state } => {
|
||||
// Always write the existing path
|
||||
@@ -408,21 +389,14 @@ pub(crate) fn apply_sync_ops<R: Runtime>(
|
||||
rel_path: rel_path.to_owned(),
|
||||
}
|
||||
}
|
||||
SyncOp::FsDelete {
|
||||
state,
|
||||
fs: fs_candidate,
|
||||
} => match fs_candidate {
|
||||
None => SyncStateOp::Delete {
|
||||
state: state.to_owned(),
|
||||
},
|
||||
SyncOp::FsDelete { state, fs: fs_candidate } => match fs_candidate {
|
||||
None => SyncStateOp::Delete { state: state.to_owned() },
|
||||
Some(_) => {
|
||||
// Always delete the existing path
|
||||
let rel_path = Path::new(&state.rel_path);
|
||||
let abs_path = Path::new(&state.sync_dir).join(&rel_path);
|
||||
fs::remove_file(&abs_path)?;
|
||||
SyncStateOp::Delete {
|
||||
state: state.to_owned(),
|
||||
}
|
||||
SyncStateOp::Delete { state: state.to_owned() }
|
||||
}
|
||||
},
|
||||
SyncOp::DbCreate { fs } => {
|
||||
@@ -463,9 +437,7 @@ pub(crate) fn apply_sync_ops<R: Runtime>(
|
||||
}
|
||||
SyncOp::DbDelete { model, state } => {
|
||||
delete_model(app_handle, &model)?;
|
||||
SyncStateOp::Delete {
|
||||
state: state.to_owned(),
|
||||
}
|
||||
SyncStateOp::Delete { state: state.to_owned() }
|
||||
}
|
||||
SyncOp::IgnorePrivate { .. } => SyncStateOp::NoOp,
|
||||
});
|
||||
@@ -541,11 +513,7 @@ pub(crate) fn apply_sync_state_ops<R: Runtime>(
|
||||
) -> Result<()> {
|
||||
for op in ops {
|
||||
match op {
|
||||
SyncStateOp::Create {
|
||||
checksum,
|
||||
rel_path,
|
||||
model_id,
|
||||
} => {
|
||||
SyncStateOp::Create { checksum, rel_path, model_id } => {
|
||||
let sync_state = SyncState {
|
||||
workspace_id: workspace_id.to_string(),
|
||||
model_id,
|
||||
@@ -557,11 +525,7 @@ pub(crate) fn apply_sync_state_ops<R: Runtime>(
|
||||
};
|
||||
app_handle.db().upsert_sync_state(&sync_state)?;
|
||||
}
|
||||
SyncStateOp::Update {
|
||||
state: sync_state,
|
||||
checksum,
|
||||
rel_path,
|
||||
} => {
|
||||
SyncStateOp::Update { state: sync_state, checksum, rel_path } => {
|
||||
let sync_state = SyncState {
|
||||
checksum,
|
||||
sync_dir: sync_dir.to_str().unwrap().to_string(),
|
||||
|
||||
Reference in New Issue
Block a user