diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/actions/shift.rs | 22 | ||||
| -rw-r--r-- | src/actions/update.rs | 18 | ||||
| -rw-r--r-- | src/files.rs | 84 |
3 files changed, 62 insertions, 62 deletions
diff --git a/src/actions/shift.rs b/src/actions/shift.rs index 1bd8864..437fcb4 100644 --- a/src/actions/shift.rs +++ b/src/actions/shift.rs @@ -7,7 +7,7 @@ use std::{ use anyhow::Result; use crate::{ - files::{Locations, RepositoryPaths}, + files::{Locations, FileState}, history::FileHistory, }; @@ -16,8 +16,8 @@ use super::ActionOptions; pub fn shift(command_options: ActionOptions, path: &str, new_cursor: usize) -> Result<()> { let locations = Locations::from(&command_options); - match RepositoryPaths::from_tracked(&locations, Path::new(path))? { - RepositoryPaths::Tracked(tracked) => { + match FileState::from_working(&locations, Path::new(path))? { + FileState::Tracked(tracked) => { let mut history_file = tracked.load_history_file()?; let mut file_history = FileHistory::from_file(&mut history_file)?; @@ -27,30 +27,30 @@ pub fn shift(command_options: ActionOptions, path: &str, new_cursor: usize) -> R fs::remove_file(path)?; } else { let new_content = file_history.get_content(); - let mut tracked_file = tracked.create_tracked_file()?; + let mut working_file = tracked.create_working_file()?; - tracked_file.rewind()?; - tracked_file.set_len(0)?; + working_file.rewind()?; + working_file.set_len(0)?; - tracked_file.write_all(new_content.as_bytes())?; + working_file.write_all(new_content.as_bytes())?; } file_history.write_to_file(&mut history_file)?; } - RepositoryPaths::Deleted(deleted) => { + FileState::Deleted(deleted) => { let mut history_file = deleted.load_history_file()?; let mut file_history = FileHistory::from_file(&mut history_file)?; file_history.set_cursor(new_cursor); if !file_history.file_is_deleted() { - let mut new_tracked_file = deleted.create_tracked_file(&locations)?; + let mut new_working_file = deleted.create_working_file(&locations)?; let new_content = file_history.get_content(); - new_tracked_file.write_all(new_content.as_bytes())?; + new_working_file.write_all(new_content.as_bytes())?; } } - RepositoryPaths::Untracked(_) => panic!("File is not tracked with Ka."), + FileState::Untracked(_) => panic!("File is not tracked with Ka."), } Ok(()) diff --git a/src/actions/update.rs b/src/actions/update.rs index a7d1aad..73a9222 100644 --- a/src/actions/update.rs +++ b/src/actions/update.rs @@ -3,7 +3,7 @@ use std::io::Read; use anyhow::{Context, Result}; use crate::{ - files::{Locations, RepositoryPaths}, + files::{Locations, FileState}, history::{FileChange, FileHistory}, text_diff::TextChange, }; @@ -14,7 +14,7 @@ pub fn update(command_options: ActionOptions) -> Result<()> { let locations = Locations::from(&command_options); let entries = locations - .get_repository_paths() + .get_repository_files() .context("Could not traverse files.")?; for element in entries { @@ -24,9 +24,9 @@ pub fn update(command_options: ActionOptions) -> Result<()> { Ok(()) } -fn update_file(paths: RepositoryPaths, locations: &Locations) -> Result<()> { - let new_state = match paths { - RepositoryPaths::Deleted(deleted) => { +fn update_file(file_state: FileState, locations: &Locations) -> Result<()> { + let new_state = match file_state { + FileState::Deleted(deleted) => { let mut history_file = deleted.load_history_file()?; let file_history = FileHistory::from_file(&mut history_file)?; if !file_history.file_is_deleted() { @@ -37,7 +37,7 @@ fn update_file(paths: RepositoryPaths, locations: &Locations) -> Result<()> { None } } - RepositoryPaths::Untracked(untracked) => { + FileState::Untracked(untracked) => { let mut file = untracked.load_file()?; let mut file_content = String::new(); @@ -53,14 +53,14 @@ fn update_file(paths: RepositoryPaths, locations: &Locations) -> Result<()> { Some((untracked.create_history_file(locations)?, new_history)) } - RepositoryPaths::Tracked(tracked) => { + FileState::Tracked(tracked) => { let mut history_file = tracked.load_history_file()?; - let mut tracked_file = tracked.load_tracked_file()?; + let mut working_file = tracked.load_working_file()?; let file_history = FileHistory::from_file(&mut history_file)?; let mut new_content = String::new(); - tracked_file.read_to_string(&mut new_content)?; + working_file.read_to_string(&mut new_content)?; let old_content = file_history.get_content(); diff --git a/src/files.rs b/src/files.rs index 81bdd25..c4131d2 100644 --- a/src/files.rs +++ b/src/files.rs @@ -15,51 +15,51 @@ pub struct Locations { } impl Locations { - pub fn get_repository_paths(&self) -> Result<Vec<RepositoryPaths>, Error> { - let repository_entries = - fs::read_dir(&self.repository_path).context("Failed reading tracked file entries.")?; + pub fn get_repository_files(&self) -> Result<Vec<FileState>, Error> { + let working_entries = + fs::read_dir(&self.repository_path).context("Failed reading working file entries.")?; let history_entries = fs::read_dir(&self.ka_files_path).context("Failed reading history file entries.")?; - let tracked_paths = Self::walk_directory(repository_entries, &|entry| { + let working_files = Self::walk_directory(working_entries, &|entry| { let file_path = entry.path(); if file_path != self.ka_path { - RepositoryPaths::from_tracked(&self, &file_path).ok() + FileState::from_working(&self, &file_path).ok() } else { None } })?; - let deleted_paths = Self::walk_directory(history_entries, &|entry| { + let deleted_files = Self::walk_directory(history_entries, &|entry| { let file_path = entry.path(); - let file = RepositoryPaths::from_history(&self, &file_path).ok()?; + let file = FileState::from_history(&self, &file_path).ok()?; match file { - RepositoryPaths::Deleted { .. } => Some(file), - RepositoryPaths::Tracked { .. } => None, + FileState::Deleted { .. } => Some(file), + FileState::Tracked { .. } => None, _ => unreachable!(), } })?; - let mut all_paths = tracked_paths; - all_paths.extend(deleted_paths); + let mut all_files = working_files; + all_files.extend(deleted_files); - Ok(all_paths) + Ok(all_files) } - pub fn tracked_from_history(&self, history_file_path: &Path) -> Result<PathBuf> { + pub fn working_from_history(&self, history_file_path: &Path) -> Result<PathBuf> { let raw_path = history_file_path.strip_prefix(&self.ka_files_path)?; Ok(self.repository_path.join(raw_path)) } - pub fn history_from_tracked(&self, tracked_file_path: &Path) -> Result<PathBuf> { - let raw_path = tracked_file_path.strip_prefix(&self.repository_path)?; + pub fn history_from_working(&self, working_file_path: &Path) -> Result<PathBuf> { + let raw_path = working_file_path.strip_prefix(&self.repository_path)?; Ok(self.ka_files_path.join(raw_path)) } fn walk_directory( directory: ReadDir, - filter_map: &dyn Fn(DirEntry) -> Option<RepositoryPaths>, - ) -> Result<Vec<RepositoryPaths>> { + filter_map: &dyn Fn(DirEntry) -> Option<FileState>, + ) -> Result<Vec<FileState>> { let mut entries = Vec::new(); for res in directory { @@ -67,11 +67,11 @@ impl Locations { let file_type = entry.file_type()?; if file_type.is_dir() { let nested_directory = fs::read_dir(entry.path())?; - let nested_paths = Self::walk_directory(nested_directory, filter_map)?; - entries.extend(nested_paths); + let nested_files = Self::walk_directory(nested_directory, filter_map)?; + entries.extend(nested_files); } else { - if let Some(paths) = filter_map(entry) { - entries.push(paths); + if let Some(states) = filter_map(entry) { + entries.push(states); } } } @@ -93,37 +93,37 @@ impl From<&ActionOptions> for Locations { } } -pub enum RepositoryPaths { +pub enum FileState { Deleted(FileDeleted), Untracked(FileUntracked), Tracked(FileTracked), } -impl RepositoryPaths { +impl FileState { pub fn from_history(locations: &Locations, history_file_path: &Path) -> Result<Self> { - let tracked_path = locations.tracked_from_history(history_file_path)?; - Ok(if !tracked_path.exists() { - RepositoryPaths::Deleted(FileDeleted { + let working_path = locations.working_from_history(history_file_path)?; + Ok(if !working_path.exists() { + FileState::Deleted(FileDeleted { history_path: history_file_path.to_path_buf(), }) } else { - RepositoryPaths::Tracked(FileTracked { + FileState::Tracked(FileTracked { history_path: history_file_path.to_path_buf(), - tracked_path, + working_path, }) }) } - pub fn from_tracked(locations: &Locations, tracked_file_path: &Path) -> Result<Self> { - let history_path = locations.history_from_tracked(tracked_file_path)?; + pub fn from_working(locations: &Locations, working_file_path: &Path) -> Result<Self> { + let history_path = locations.history_from_working(working_file_path)?; Ok(if !history_path.exists() { - RepositoryPaths::Untracked(FileUntracked { - path: tracked_file_path.to_path_buf(), + FileState::Untracked(FileUntracked { + path: working_file_path.to_path_buf(), }) } else { - RepositoryPaths::Tracked(FileTracked { + FileState::Tracked(FileTracked { history_path, - tracked_path: tracked_file_path.to_path_buf(), + working_path: working_file_path.to_path_buf(), }) }) } @@ -141,9 +141,9 @@ impl FileDeleted { .open(&self.history_path) } - pub fn create_tracked_file(&self, locations: &Locations) -> Result<File> { + pub fn create_working_file(&self, locations: &Locations) -> Result<File> { Ok(File::create( - locations.tracked_from_history(&self.history_path)?, + locations.working_from_history(&self.history_path)?, )?) } } @@ -158,7 +158,7 @@ impl FileUntracked { } pub fn create_history_file(&self, locations: &Locations) -> Result<File> { - let history_path = locations.history_from_tracked(&self.path)?; + let history_path = locations.history_from_working(&self.path)?; if let Some(parent_path) = history_path.parent(){ if !parent_path.exists() { @@ -172,7 +172,7 @@ impl FileUntracked { pub struct FileTracked { history_path: PathBuf, - tracked_path: PathBuf, + working_path: PathBuf, } impl FileTracked { @@ -183,11 +183,11 @@ impl FileTracked { .open(&self.history_path) } - pub fn load_tracked_file(&self) -> io::Result<File> { - File::open(&self.tracked_path) + pub fn load_working_file(&self) -> io::Result<File> { + File::open(&self.working_path) } - pub fn create_tracked_file(&self) -> io::Result<File> { - File::create(&self.tracked_path) + pub fn create_working_file(&self) -> io::Result<File> { + File::create(&self.working_path) } } |
