From 014a4e0575a45a7b2042a420fbeb9cdc684e9234 Mon Sep 17 00:00:00 2001 From: Nikita Galaiko Date: Sun, 7 May 2023 16:53:51 +0200 Subject: [PATCH] move deltas inside app --- src-tauri/src/app/app.rs | 4 +-- src-tauri/src/{ => app}/deltas/delta.rs | 0 src-tauri/src/{ => app}/deltas/mod.rs | 0 .../src/{ => app}/deltas/operations/mod.rs | 0 .../{ => app}/deltas/operations/operations.rs | 0 .../src/{ => app}/deltas/operations/tests.rs | 2 +- .../src/{ => app}/deltas/text_document.rs | 0 .../{ => app}/deltas/text_document_tests.rs | 2 +- src-tauri/src/app/gb_repository_tests.rs | 17 ++++++---- src-tauri/src/app/mod.rs | 2 ++ src-tauri/src/app/sessions/reader.rs | 13 +++---- src-tauri/src/app/sessions/writer.rs | 6 ++-- src-tauri/src/app/watcher/events.rs | 4 +-- .../watcher/handlers/project_file_change.rs | 14 ++++---- .../handlers/project_file_change_tests.rs | 24 ++++++------- src-tauri/src/events.rs | 4 +-- src-tauri/src/main.rs | 4 +-- src-tauri/src/search/deltas.rs | 4 +-- src-tauri/src/search/deltas_test.rs | 34 ++++++++----------- 19 files changed, 65 insertions(+), 69 deletions(-) rename src-tauri/src/{ => app}/deltas/delta.rs (100%) rename src-tauri/src/{ => app}/deltas/mod.rs (100%) rename src-tauri/src/{ => app}/deltas/operations/mod.rs (100%) rename src-tauri/src/{ => app}/deltas/operations/operations.rs (100%) rename src-tauri/src/{ => app}/deltas/operations/tests.rs (96%) rename src-tauri/src/{ => app}/deltas/text_document.rs (100%) rename src-tauri/src/{ => app}/deltas/text_document_tests.rs (98%) diff --git a/src-tauri/src/app/app.rs b/src-tauri/src/app/app.rs index fbe01c796..3af4c3c1a 100644 --- a/src-tauri/src/app/app.rs +++ b/src-tauri/src/app/app.rs @@ -3,7 +3,7 @@ use std::{collections::HashMap, sync}; use anyhow::{Context, Result}; use crossbeam_channel::{bounded, Sender}; -use crate::{deltas, events, git::activity, projects, pty, search, storage, users}; +use crate::{app, events, git::activity, projects, pty, search, storage, users}; use super::{gb_repository, project_repository, watcher, sessions}; @@ -308,7 +308,7 @@ impl App { project_id: &str, session_id: &str, paths: Option>, - ) -> Result>> { + ) -> Result>> { let gb_repository = gb_repository::Repository::open( self.local_data_dir.clone(), project_id.to_string(), diff --git a/src-tauri/src/deltas/delta.rs b/src-tauri/src/app/deltas/delta.rs similarity index 100% rename from src-tauri/src/deltas/delta.rs rename to src-tauri/src/app/deltas/delta.rs diff --git a/src-tauri/src/deltas/mod.rs b/src-tauri/src/app/deltas/mod.rs similarity index 100% rename from src-tauri/src/deltas/mod.rs rename to src-tauri/src/app/deltas/mod.rs diff --git a/src-tauri/src/deltas/operations/mod.rs b/src-tauri/src/app/deltas/operations/mod.rs similarity index 100% rename from src-tauri/src/deltas/operations/mod.rs rename to src-tauri/src/app/deltas/operations/mod.rs diff --git a/src-tauri/src/deltas/operations/operations.rs b/src-tauri/src/app/deltas/operations/operations.rs similarity index 100% rename from src-tauri/src/deltas/operations/operations.rs rename to src-tauri/src/app/deltas/operations/operations.rs diff --git a/src-tauri/src/deltas/operations/tests.rs b/src-tauri/src/app/deltas/operations/tests.rs similarity index 96% rename from src-tauri/src/deltas/operations/tests.rs rename to src-tauri/src/app/deltas/operations/tests.rs index f15cb2b3f..1c6e104be 100644 --- a/src-tauri/src/deltas/operations/tests.rs +++ b/src-tauri/src/app/deltas/operations/tests.rs @@ -1,4 +1,4 @@ -use crate::deltas::operations::{get_delta_operations, Operation}; +use super::{get_delta_operations, Operation}; #[test] fn test_get_delta_operations_insert_end() { diff --git a/src-tauri/src/deltas/text_document.rs b/src-tauri/src/app/deltas/text_document.rs similarity index 100% rename from src-tauri/src/deltas/text_document.rs rename to src-tauri/src/app/deltas/text_document.rs diff --git a/src-tauri/src/deltas/text_document_tests.rs b/src-tauri/src/app/deltas/text_document_tests.rs similarity index 98% rename from src-tauri/src/deltas/text_document_tests.rs rename to src-tauri/src/app/deltas/text_document_tests.rs index 6184f709f..13f3ebd6f 100644 --- a/src-tauri/src/deltas/text_document_tests.rs +++ b/src-tauri/src/app/deltas/text_document_tests.rs @@ -1,4 +1,4 @@ -use crate::deltas::{operations::Operation, text_document::TextDocument, Delta}; +use super::{Operation, TextDocument, Delta}; #[test] fn test_new() { diff --git a/src-tauri/src/app/gb_repository_tests.rs b/src-tauri/src/app/gb_repository_tests.rs index 3833b88ed..77d2f1aee 100644 --- a/src-tauri/src/app/gb_repository_tests.rs +++ b/src-tauri/src/app/gb_repository_tests.rs @@ -1,7 +1,10 @@ use anyhow::Result; use tempfile::tempdir; -use crate::{app::gb_repository, deltas, projects, storage, users}; +use crate::{ + app::{self, gb_repository}, + projects, storage, users, +}; fn test_repository() -> Result { let path = tempdir()?.path().to_str().unwrap().to_string(); @@ -224,8 +227,8 @@ fn test_list_deltas_from_current_session() -> Result<()> { let writer = gb_repo.get_session_writer(¤t_session)?; writer.write_deltas( "test.txt", - &vec![deltas::Delta { - operations: vec![deltas::Operation::Insert((0, "Hello World".to_string()))], + &vec![app::Delta { + operations: vec![app::Operation::Insert((0, "Hello World".to_string()))], timestamp_ms: 0, }], )?; @@ -237,7 +240,7 @@ fn test_list_deltas_from_current_session() -> Result<()> { assert_eq!(deltas.get("test.txt").unwrap()[0].operations.len(), 1); assert_eq!( deltas.get("test.txt").unwrap()[0].operations[0], - deltas::Operation::Insert((0, "Hello World".to_string())) + app::Operation::Insert((0, "Hello World".to_string())) ); Ok(()) @@ -263,8 +266,8 @@ fn test_list_deltas_from_flushed_session() -> Result<()> { let writer = gb_repo.get_session_writer(¤t_session)?; writer.write_deltas( "test.txt", - &vec![deltas::Delta { - operations: vec![deltas::Operation::Insert((0, "Hello World".to_string()))], + &vec![app::Delta { + operations: vec![app::Operation::Insert((0, "Hello World".to_string()))], timestamp_ms: 0, }], )?; @@ -277,7 +280,7 @@ fn test_list_deltas_from_flushed_session() -> Result<()> { assert_eq!(deltas.get("test.txt").unwrap()[0].operations.len(), 1); assert_eq!( deltas.get("test.txt").unwrap()[0].operations[0], - deltas::Operation::Insert((0, "Hello World".to_string())) + app::Operation::Insert((0, "Hello World".to_string())) ); Ok(()) diff --git a/src-tauri/src/app/mod.rs b/src-tauri/src/app/mod.rs index 55b5c6c43..3f355993e 100644 --- a/src-tauri/src/app/mod.rs +++ b/src-tauri/src/app/mod.rs @@ -1,4 +1,5 @@ mod app; +mod deltas; pub mod gb_repository; pub mod project_repository; pub mod reader; @@ -12,5 +13,6 @@ mod gb_repository_tests; mod reader_tests; pub use app::{AddProjectError, App}; +pub use deltas::{Delta, Operation, TextDocument}; pub use project_repository::FileStatus; pub use sessions::{Meta, Session, SessionError}; diff --git a/src-tauri/src/app/sessions/reader.rs b/src-tauri/src/app/sessions/reader.rs index 65078bcb7..29043e8dc 100644 --- a/src-tauri/src/app/sessions/reader.rs +++ b/src-tauri/src/app/sessions/reader.rs @@ -2,12 +2,9 @@ use std::collections::HashMap; use anyhow::{anyhow, Context, Result}; -use crate::{ - app::{ - gb_repository, - reader::{self, CommitReader, Reader}, - }, - deltas, +use crate::app::{ + self, gb_repository, + reader::{self, CommitReader, Reader}, }; use super::Session; @@ -120,7 +117,7 @@ impl<'reader> SessionReader<'reader> { pub fn file_deltas>( &self, path: P, - ) -> Result>> { + ) -> Result>> { let path = path.as_ref(); let file_deltas_path = std::path::Path::new("session/deltas").join(path); match self @@ -140,7 +137,7 @@ impl<'reader> SessionReader<'reader> { } } - pub fn deltas(&self, paths: Option>) -> Result>> { + pub fn deltas(&self, paths: Option>) -> Result>> { let deltas_dir = std::path::Path::new("session/deltas"); let files = self.reader.list_files(deltas_dir.to_str().unwrap())?; let mut result = HashMap::new(); diff --git a/src-tauri/src/app/sessions/writer.rs b/src-tauri/src/app/sessions/writer.rs index 3970e271c..58b150e8d 100644 --- a/src-tauri/src/app/sessions/writer.rs +++ b/src-tauri/src/app/sessions/writer.rs @@ -4,11 +4,11 @@ use anyhow::{anyhow, Context, Result}; use crate::{ app::{ - gb_repository, + self, gb_repository, reader::{self, Reader}, writer::{self, Writer}, }, - deltas, pty, + pty, }; use super::Session; @@ -188,7 +188,7 @@ impl<'writer> SessionWriter<'writer> { pub fn write_deltas>( &self, path: P, - deltas: &Vec, + deltas: &Vec, ) -> Result<()> { self.repository.lock()?; defer! { diff --git a/src-tauri/src/app/watcher/events.rs b/src-tauri/src/app/watcher/events.rs index 47744dd23..fddf897e8 100644 --- a/src-tauri/src/app/watcher/events.rs +++ b/src-tauri/src/app/watcher/events.rs @@ -1,6 +1,6 @@ use std::{path, time}; -use crate::{app, deltas, projects}; +use crate::{app, projects}; pub enum Event { Tick(time::SystemTime), @@ -21,7 +21,7 @@ pub enum Event { projects::Project, app::Session, path::PathBuf, - Vec, + Vec, ), ), } diff --git a/src-tauri/src/app/watcher/handlers/project_file_change.rs b/src-tauri/src/app/watcher/handlers/project_file_change.rs index 27ab6adc2..b969f4377 100644 --- a/src-tauri/src/app/watcher/handlers/project_file_change.rs +++ b/src-tauri/src/app/watcher/handlers/project_file_change.rs @@ -4,10 +4,10 @@ use anyhow::{Context, Result}; use crate::{ app::{ - gb_repository, project_repository, + self, gb_repository, project_repository, reader::{self, Reader}, }, - deltas, projects, + projects, }; use super::events; @@ -135,7 +135,7 @@ impl<'listener> Handler<'listener> { } // returns deltas for the file that are already part of the current session (if any) - fn get_current_deltas(&self, path: &std::path::Path) -> Result>> { + fn get_current_deltas(&self, path: &std::path::Path) -> Result>> { let current_session = self.gb_repository.get_current_session()?; if current_session.is_none() { return Ok(None); @@ -197,13 +197,13 @@ impl<'listener> Handler<'listener> { let mut text_doc = match (latest_file_content, current_deltas) { (Some(latest_contents), Some(deltas)) => { - deltas::TextDocument::new(Some(&latest_contents), deltas)? + app::TextDocument::new(Some(&latest_contents), deltas)? } (Some(latest_contents), None) => { - deltas::TextDocument::new(Some(&latest_contents), vec![])? + app::TextDocument::new(Some(&latest_contents), vec![])? } - (None, Some(deltas)) => deltas::TextDocument::new(None, deltas)?, - (None, None) => deltas::TextDocument::new(None, vec![])?, + (None, Some(deltas)) => app::TextDocument::new(None, deltas)?, + (None, None) => app::TextDocument::new(None, vec![])?, }; if !text_doc diff --git a/src-tauri/src/app/watcher/handlers/project_file_change_tests.rs b/src-tauri/src/app/watcher/handlers/project_file_change_tests.rs index 2abafaebc..10df27aa5 100644 --- a/src-tauri/src/app/watcher/handlers/project_file_change_tests.rs +++ b/src-tauri/src/app/watcher/handlers/project_file_change_tests.rs @@ -3,7 +3,7 @@ use tempfile::tempdir; use crate::{ app::{self, gb_repository, project_repository}, - deltas, projects, storage, users, + projects, storage, users, }; use super::project_file_change::Handler; @@ -88,7 +88,7 @@ fn test_register_existing_commited_file() -> Result<()> { assert_eq!(deltas[0].operations.len(), 1); assert_eq!( deltas[0].operations[0], - deltas::Operation::Insert((4, "2".to_string())), + app::Operation::Insert((4, "2".to_string())), ); assert_eq!( std::fs::read_to_string(gb_repo.session_wd_path().join(file_path))?, @@ -189,7 +189,7 @@ fn test_register_new_file() -> Result<()> { assert_eq!(deltas[0].operations.len(), 1); assert_eq!( deltas[0].operations[0], - deltas::Operation::Insert((0, "test".to_string())), + app::Operation::Insert((0, "test".to_string())), ); assert_eq!( std::fs::read_to_string(gb_repo.session_wd_path().join(file_path))?, @@ -228,7 +228,7 @@ fn test_register_new_file_twice() -> Result<()> { assert_eq!(deltas[0].operations.len(), 1); assert_eq!( deltas[0].operations[0], - deltas::Operation::Insert((0, "test".to_string())), + app::Operation::Insert((0, "test".to_string())), ); assert_eq!( std::fs::read_to_string(gb_repo.session_wd_path().join(file_path))?, @@ -243,12 +243,12 @@ fn test_register_new_file_twice() -> Result<()> { assert_eq!(deltas[0].operations.len(), 1); assert_eq!( deltas[0].operations[0], - deltas::Operation::Insert((0, "test".to_string())), + app::Operation::Insert((0, "test".to_string())), ); assert_eq!(deltas[1].operations.len(), 1); assert_eq!( deltas[1].operations[0], - deltas::Operation::Insert((4, "2".to_string())), + app::Operation::Insert((4, "2".to_string())), ); assert_eq!( std::fs::read_to_string(gb_repo.session_wd_path().join(file_path))?, @@ -287,7 +287,7 @@ fn test_register_file_delted() -> Result<()> { assert_eq!(deltas[0].operations.len(), 1); assert_eq!( deltas[0].operations[0], - deltas::Operation::Insert((0, "test".to_string())), + app::Operation::Insert((0, "test".to_string())), ); assert_eq!( std::fs::read_to_string(gb_repo.session_wd_path().join(file_path))?, @@ -302,10 +302,10 @@ fn test_register_file_delted() -> Result<()> { assert_eq!(deltas[0].operations.len(), 1); assert_eq!( deltas[0].operations[0], - deltas::Operation::Insert((0, "test".to_string())), + app::Operation::Insert((0, "test".to_string())), ); assert_eq!(deltas[1].operations.len(), 1); - assert_eq!(deltas[1].operations[0], deltas::Operation::Delete((0, 4)),); + assert_eq!(deltas[1].operations[0], app::Operation::Delete((0, 4)),); Ok(()) } @@ -364,7 +364,7 @@ fn test_flow_with_commits() -> Result<()> { let sessions_slice = &mut sessions[i..]; // collect all operations from sessions in the reverse order - let mut operations: Vec = vec![]; + let mut operations: Vec = vec![]; sessions_slice.iter().for_each(|session| { let reader = gb_repo.get_session_reader(&session).unwrap(); let deltas_by_filepath = reader.deltas(None).unwrap(); @@ -456,7 +456,7 @@ fn test_flow_no_commits() -> Result<()> { let sessions_slice = &mut sessions[i..]; // collect all operations from sessions in the reverse order - let mut operations: Vec = vec![]; + let mut operations: Vec = vec![]; sessions_slice.iter().for_each(|session| { let reader = gb_repo.get_session_reader(&session).unwrap(); let deltas_by_filepath = reader.deltas(None).unwrap(); @@ -526,7 +526,7 @@ fn test_flow_signle_session() -> Result<()> { } // collect all operations from sessions in the reverse order - let mut operations: Vec = vec![]; + let mut operations: Vec = vec![]; let session = gb_repo.get_current_session()?.unwrap(); let reader = gb_repo.get_session_reader(&session).unwrap(); let deltas_by_filepath = reader.deltas(None).unwrap(); diff --git a/src-tauri/src/events.rs b/src-tauri/src/events.rs index 851507943..8be87258a 100644 --- a/src-tauri/src/events.rs +++ b/src-tauri/src/events.rs @@ -1,4 +1,4 @@ -use crate::{app, deltas, projects}; +use crate::{app, projects}; #[derive(Debug)] pub struct Event { @@ -42,7 +42,7 @@ impl Event { pub fn detlas( project: &projects::Project, session: &app::Session, - deltas: &Vec, + deltas: &Vec, relative_file_path: &std::path::Path, ) -> Self { let event_name = format!("project://{}/sessions/{}/deltas", project.id, session.id); diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index aaf5ab2ac..5de719f73 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -4,7 +4,6 @@ extern crate scopeguard; extern crate lazy_static; mod app; -mod deltas; mod events; mod fs; mod git; @@ -18,7 +17,6 @@ mod users; extern crate log; use anyhow::{Context, Result}; -use deltas::Delta; use git::activity; use serde::{ser::SerializeMap, Serialize}; use std::{collections::HashMap, ops::Range}; @@ -286,7 +284,7 @@ async fn list_deltas( project_id: &str, session_id: &str, paths: Option>, -) -> Result>, Error> { +) -> Result>, Error> { let app = handle.state::(); let deltas = app .list_session_deltas(project_id, session_id, paths) diff --git a/src-tauri/src/search/deltas.rs b/src-tauri/src/search/deltas.rs index 5926b6146..eaa5fc804 100644 --- a/src-tauri/src/search/deltas.rs +++ b/src-tauri/src/search/deltas.rs @@ -11,7 +11,7 @@ use serde::Serialize; use similar::{ChangeTag, TextDiff}; use tantivy::{collector, directory::MmapDirectory, schema, IndexWriter}; -use crate::{app, deltas, storage}; +use crate::{app, storage}; const CURRENT_VERSION: u64 = 4; // should not decrease @@ -238,7 +238,7 @@ fn index_delta( file_text: &mut Vec, file_path: &str, i: usize, - delta: &deltas::Delta, + delta: &app::Delta, ) -> Result<()> { let mut doc = tantivy::Document::default(); doc.add_u64( diff --git a/src-tauri/src/search/deltas_test.rs b/src-tauri/src/search/deltas_test.rs index d13acc8c5..ed47539de 100644 --- a/src-tauri/src/search/deltas_test.rs +++ b/src-tauri/src/search/deltas_test.rs @@ -4,11 +4,7 @@ use std::path::Path; use anyhow::Result; use tempfile::tempdir; -use crate::{ - app, - deltas::{self, Operation}, - projects, storage, users, -}; +use crate::{app, projects, storage, users}; fn test_repository() -> Result { let path = tempdir()?.path().to_str().unwrap().to_string(); @@ -63,16 +59,16 @@ fn test_filter_by_timestamp() -> Result<()> { writer.write_deltas( Path::new("test.txt"), &vec![ - deltas::Delta { - operations: vec![Operation::Insert((0, "Hello".to_string()))], + app::Delta { + operations: vec![app::Operation::Insert((0, "Hello".to_string()))], timestamp_ms: 0, }, - deltas::Delta { - operations: vec![Operation::Insert((5, "World".to_string()))], + app::Delta { + operations: vec![app::Operation::Insert((5, "World".to_string()))], timestamp_ms: 1, }, - deltas::Delta { - operations: vec![Operation::Insert((5, " ".to_string()))], + app::Delta { + operations: vec![app::Operation::Insert((5, " ".to_string()))], timestamp_ms: 2, }, ], @@ -139,12 +135,12 @@ fn test_sorted_by_timestamp() -> Result<()> { writer.write_deltas( Path::new("test.txt"), &vec![ - deltas::Delta { - operations: vec![Operation::Insert((0, "Hello".to_string()))], + app::Delta { + operations: vec![app::Operation::Insert((0, "Hello".to_string()))], timestamp_ms: 0, }, - deltas::Delta { - operations: vec![Operation::Insert((5, " World".to_string()))], + app::Delta { + operations: vec![app::Operation::Insert((5, " World".to_string()))], timestamp_ms: 1, }, ], @@ -196,12 +192,12 @@ fn test_simple() -> Result<()> { writer.write_deltas( Path::new("test.txt"), &vec![ - deltas::Delta { - operations: vec![Operation::Insert((0, "Hello".to_string()))], + app::Delta { + operations: vec![app::Operation::Insert((0, "Hello".to_string()))], timestamp_ms: 0, }, - deltas::Delta { - operations: vec![Operation::Insert((5, " World".to_string()))], + app::Delta { + operations: vec![app::Operation::Insert((5, " World".to_string()))], timestamp_ms: 0, }, ],