about summary refs log tree commit diff
path: root/src/actions
diff options
context:
space:
mode:
Diffstat (limited to 'src/actions')
-rw-r--r--src/actions/create.rs6
-rw-r--r--src/actions/mod.rs11
-rw-r--r--src/actions/shift.rs12
-rw-r--r--src/actions/update.rs14
4 files changed, 23 insertions, 20 deletions
diff --git a/src/actions/create.rs b/src/actions/create.rs
index 26d20c8..f00e95d 100644
--- a/src/actions/create.rs
+++ b/src/actions/create.rs
@@ -1,10 +1,12 @@
-use std::{fs, io};
+use std::fs;
+
+use anyhow::Result;
 
 use crate::{actions::update, files::Locations};
 
 use super::ActionOptions;
 
-pub fn create(command_options: ActionOptions) -> io::Result<()> {
+pub fn create(command_options: ActionOptions) -> Result<()> {
     let locations = Locations::from(&command_options);
 
     if locations.ka_path.exists() {
diff --git a/src/actions/mod.rs b/src/actions/mod.rs
index 588ce8f..86af955 100644
--- a/src/actions/mod.rs
+++ b/src/actions/mod.rs
@@ -4,6 +4,7 @@ mod update;
 
 use std::path::{Path, PathBuf};
 
+use anyhow::Result;
 pub use create::create;
 pub use shift::shift;
 pub use update::update;
@@ -19,12 +20,8 @@ impl ActionOptions {
         }
     }
 
-    pub fn from_pwd() -> Result<Self, ()> {
-        let current_path = std::env::current_dir();
-        if let Ok(repository_path) = current_path {
-            Ok(ActionOptions { repository_path })
-        } else {
-            Err(())
-        }
+    pub fn from_pwd() -> Result<Self> {
+        let repository_path = std::env::current_dir()?;
+        Ok(ActionOptions { repository_path })
     }
 }
diff --git a/src/actions/shift.rs b/src/actions/shift.rs
index c02f0ed..1bd8864 100644
--- a/src/actions/shift.rs
+++ b/src/actions/shift.rs
@@ -1,9 +1,11 @@
 use std::{
     fs,
-    io::{self, Seek, Write},
+    io::{Seek, Write},
     path::Path,
 };
 
+use anyhow::Result;
+
 use crate::{
     files::{Locations, RepositoryPaths},
     history::FileHistory,
@@ -11,14 +13,14 @@ use crate::{
 
 use super::ActionOptions;
 
-pub fn shift(command_options: ActionOptions, path: &str, new_cursor: usize) -> io::Result<()> {
+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)) {
+    match RepositoryPaths::from_tracked(&locations, Path::new(path))? {
         RepositoryPaths::Tracked(tracked) => {
             let mut history_file = tracked.load_history_file()?;
 
-            let mut file_history = FileHistory::from_file(&mut history_file).unwrap();
+            let mut file_history = FileHistory::from_file(&mut history_file)?;
             file_history.set_cursor(new_cursor);
 
             if file_history.file_is_deleted() {
@@ -38,7 +40,7 @@ pub fn shift(command_options: ActionOptions, path: &str, new_cursor: usize) -> i
         RepositoryPaths::Deleted(deleted) => {
             let mut history_file = deleted.load_history_file()?;
 
-            let mut file_history = FileHistory::from_file(&mut history_file).unwrap();
+            let mut file_history = FileHistory::from_file(&mut history_file)?;
             file_history.set_cursor(new_cursor);
 
             if !file_history.file_is_deleted() {
diff --git a/src/actions/update.rs b/src/actions/update.rs
index a2bc08d..a7d1aad 100644
--- a/src/actions/update.rs
+++ b/src/actions/update.rs
@@ -1,4 +1,6 @@
-use std::io::{self, Read};
+use std::io::Read;
+
+use anyhow::{Context, Result};
 
 use crate::{
     files::{Locations, RepositoryPaths},
@@ -8,12 +10,12 @@ use crate::{
 
 use super::ActionOptions;
 
-pub fn update(command_options: ActionOptions) -> io::Result<()> {
+pub fn update(command_options: ActionOptions) -> Result<()> {
     let locations = Locations::from(&command_options);
 
     let entries = locations
         .get_repository_paths()
-        .expect("Could not traverse files.");
+        .context("Could not traverse files.")?;
 
     for element in entries {
         update_file(element, &locations)?;
@@ -22,11 +24,11 @@ pub fn update(command_options: ActionOptions) -> io::Result<()> {
     Ok(())
 }
 
-fn update_file(paths: RepositoryPaths, locations: &Locations) -> io::Result<()> {
+fn update_file(paths: RepositoryPaths, locations: &Locations) -> Result<()> {
     let new_state = match paths {
         RepositoryPaths::Deleted(deleted) => {
             let mut history_file = deleted.load_history_file()?;
-            let file_history = FileHistory::from_file(&mut history_file).unwrap();
+            let file_history = FileHistory::from_file(&mut history_file)?;
             if !file_history.file_is_deleted() {
                 let mut new_history = file_history;
                 new_history.add_change(FileChange::Deleted);
@@ -55,7 +57,7 @@ fn update_file(paths: RepositoryPaths, locations: &Locations) -> io::Result<()>
             let mut history_file = tracked.load_history_file()?;
             let mut tracked_file = tracked.load_tracked_file()?;
 
-            let file_history = FileHistory::from_file(&mut history_file).unwrap();
+            let file_history = FileHistory::from_file(&mut history_file)?;
 
             let mut new_content = String::new();
             tracked_file.read_to_string(&mut new_content)?;