Skip to content

Commit 46e0c1c

Browse files
Merge pull request spicylemonade#14 from 0101coding/anyerror
Anyhow and Thiserror
2 parents 0847f40 + 7743fe4 commit 46e0c1c

File tree

9 files changed

+466
-290
lines changed

9 files changed

+466
-290
lines changed

Cargo.lock

Lines changed: 79 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,10 @@ clap = {version = "3.2.14", features = ["derive"]}
1111
home = "0.5.3"
1212
rusqlite = {version="0.28.0", features = ["chrono","bundled"]}
1313
chrono = "0.4.19"
14+
shellexpand = "2.1.0"
15+
16+
anyhow = "1.0"
17+
thiserror = "1.0"
18+
1419
opener = "0.5.0"
15-
tabled = "0.8.0"
20+
tabled = "0.8.0"

src/db.rs

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use chrono::prelude::*;
2+
use anyhow::Result;
23
use rusqlite::Connection;
34
use std::fs;
45
use std::path::Path;
@@ -45,6 +46,7 @@ macro_rules! db_connect {
4546
}};
4647
}
4748

49+
use crate::error::PunchError;
4850
//database struct
4951
#[derive(Debug, Tabled)]
5052
struct Files {
@@ -55,7 +57,12 @@ struct Files {
5557
_action: String,
5658
}
5759

58-
pub fn push(paths: &Vec<String>, action: &str, current_dir: &Path) {
60+
pub fn push(paths: &Vec<String>, action: &str, current_dir: &Path) -> Result<()> {
61+
62+
let _home_path = match home::home_dir() {
63+
Some(path) => path,
64+
_ => return Err(PunchError::TrashCanError.into()).into(),
65+
};
5966
//accessing current date & time
6067
let conn = db_connect!(".punch/punch.db", conn);
6168

@@ -92,16 +99,18 @@ pub fn push(paths: &Vec<String>, action: &str, current_dir: &Path) {
9299
)
93100
.expect("sql query failed");
94101
}
102+
Ok(())
95103
}
96104
//prints db to screen
97-
pub fn show() {
105+
pub fn show() -> Result<()> {
98106
let table_vector = db_connect!(".punch/punch.db", pull);
99107

100108
println!("{}", Table::new(table_vector).to_string());
109+
Ok(())
101110
}
102111

103112
//if the action preformed on the file was "Trash"
104-
fn u_trash(name: &Path, path: &Path) {
113+
fn u_trash(name: &Path, path: &Path) -> Result<()> {
105114
//systems home dir
106115
//check if trashed file is a directory
107116
let home_path = match home::home_dir() {
@@ -114,60 +123,77 @@ fn u_trash(name: &Path, path: &Path) {
114123
let entries = fs::read_dir(home_path.join(".punch/trash/").join(name))
115124
.expect("unable to parse directory");
116125

117-
punch::create_directory(path);
126+
if let Err(_) = punch::create_directory(path) {
127+
return Err(PunchError::CreateDirectoryError(path.display().to_string()).into()).into()
128+
}
118129

119130
for entry in entries {
120131
if let Ok(entry) = entry {
121132
let file_type = entry.file_type().ok().unwrap();
122133
if file_type.is_dir() {
123-
u_trash(&name.join(entry.file_name()), &path.join(entry.file_name()))
134+
u_trash(&name.join(entry.file_name()), &path.join(entry.file_name()))?
124135
} else {
125-
punch::move_file(
126-
&home_path
136+
let from = &home_path
127137
.join(".punch/trash/")
128-
.join(&name.join(entry.file_name())),
129-
&path.join(entry.file_name()),
130-
);
138+
.join(&name.join(entry.file_name()));
139+
let to = &path.join(entry.file_name());
140+
if let Err(_) = punch::move_file(from, to){
141+
return Err(PunchError::MoveFielError(from.display().to_string(), to.display().to_string() ).into()).into();
142+
}
131143
}
132144
}
133145
}
134146
} else {
135-
punch::move_file(
136-
&home_path.join(".punch/trash/").join(name),
137-
&path.join(name),
138-
);
147+
let from = &home_path.join(".punch/trash/").join(name);
148+
let to = &path.join(name);
149+
if let Err(_) = punch::move_file(from, to){
150+
return Err(PunchError::MoveFielError(from.display().to_string(), to.display().to_string() ).into()).into();
151+
}
139152
}
140153
//delete file in trash after
141154

142155
if trash_file.is_dir() {
143-
punch::remove_directory(&trash_file);
156+
if let Err(_) = punch::remove_directory(&trash_file) {
157+
return Err(PunchError::DeleteDirectoryError(trash_file.display().to_string()).into()).into()
158+
}
159+
;
144160
} else {
145-
fs::remove_file(&trash_file)
146-
.expect(format!("error deleting file: {}", &trash_file.display()).as_str());
161+
if let Err(_) = punch::remove_file(&trash_file) {
162+
return Err(PunchError::DeleteFileError(trash_file.display().to_string()).into()).into()
163+
}
147164
}
165+
Ok(())
148166
}
149167
//if the action preformed on the file was "Create"
150-
fn u_create(name: &Path, path: &Path) {
168+
fn u_create(name: &Path, path: &Path) -> Result<()> {
151169
if (path.join(name)).is_dir() {
152-
punch::remove_directory(path.join(name).as_path());
170+
if let Err(_) = punch::remove_directory(path.join(name).as_path()){
171+
return Err(PunchError::DeleteDirectoryError(path.join(name).display().to_string()).into()).into();
172+
}
153173
} else {
154-
punch::remove_file(path.join(name).as_path());
174+
if let Err(_) = punch::remove_file(path.join(name).as_path()){
175+
return Err(PunchError::DeleteFileError(path.join(name).display().to_string()).into()).into();
176+
}
155177
}
178+
179+
Ok(())
156180
}
157-
pub fn undo() {
181+
pub fn undo() -> Result<()> {
158182
let file_iter = db_connect!(".punch/punch.db", pull);
159183

160184
let latest_file = file_iter.last().unwrap();
161185

162186
if latest_file._action == "Create" {
163-
u_create(Path::new(&latest_file._name), Path::new(&latest_file._path));
187+
u_create(Path::new(&latest_file._name), Path::new(&latest_file._path))?;
164188
} else if latest_file._action == "Trash" {
165-
u_trash(Path::new(&latest_file._name), Path::new(&latest_file._path));
189+
u_trash(Path::new(&latest_file._name), Path::new(&latest_file._path))?;
166190
}
191+
Ok(())
167192
}
168193

169-
pub fn delete(name: String) {
194+
pub fn delete(name: String) -> Result<()> {
170195
let conn = db_connect!(".punch/punch.db", conn);
171196
conn.execute("DELETE FROM files WHERE name=(?1)", [&name])
172197
.unwrap();
198+
Ok(())
173199
}

src/error.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use thiserror::Error;
2+
3+
#[derive(Error, Debug)]
4+
pub enum PunchError{
5+
#[error("Unable to create file {0}")]
6+
CreateFileError(String),
7+
#[error("Unable to create directory {0}")]
8+
CreateDirectoryError(String),
9+
#[error("Unable to delete file {0}")]
10+
DeleteFileError(String),
11+
#[error("Unable to delete directory {0}")]
12+
DeleteDirectoryError(String),
13+
#[error("Unable to rename file")]
14+
RenameFileError,
15+
#[error("Unable to copy file {0}")]
16+
CopyFileError(String),
17+
#[error("Unable to Trash file {0}")]
18+
TrashFileError(String),
19+
#[error("Unable to Create Trash Can")]
20+
TrashCanError,
21+
#[error("Can not find trash can in default path")]
22+
TrashNotFound,
23+
#[error("Can not move file from {0} to {1}")]
24+
MoveFielError(String, String),
25+
#[error("Some Db Error Occured")]
26+
_DbError,
27+
}

src/in_directory.rs

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)