1
1
use chrono:: prelude:: * ;
2
+ use anyhow:: Result ;
2
3
use rusqlite:: Connection ;
3
4
use std:: fs;
4
5
use std:: path:: Path ;
@@ -45,6 +46,7 @@ macro_rules! db_connect {
45
46
} } ;
46
47
}
47
48
49
+ use crate :: error:: PunchError ;
48
50
//database struct
49
51
#[ derive( Debug , Tabled ) ]
50
52
struct Files {
@@ -55,7 +57,12 @@ struct Files {
55
57
_action : String ,
56
58
}
57
59
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
+ } ;
59
66
//accessing current date & time
60
67
let conn = db_connect ! ( ".punch/punch.db" , conn) ;
61
68
@@ -92,16 +99,18 @@ pub fn push(paths: &Vec<String>, action: &str, current_dir: &Path) {
92
99
)
93
100
. expect ( "sql query failed" ) ;
94
101
}
102
+ Ok ( ( ) )
95
103
}
96
104
//prints db to screen
97
- pub fn show ( ) {
105
+ pub fn show ( ) -> Result < ( ) > {
98
106
let table_vector = db_connect ! ( ".punch/punch.db" , pull) ;
99
107
100
108
println ! ( "{}" , Table :: new( table_vector) . to_string( ) ) ;
109
+ Ok ( ( ) )
101
110
}
102
111
103
112
//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 < ( ) > {
105
114
//systems home dir
106
115
//check if trashed file is a directory
107
116
let home_path = match home:: home_dir ( ) {
@@ -114,60 +123,77 @@ fn u_trash(name: &Path, path: &Path) {
114
123
let entries = fs:: read_dir ( home_path. join ( ".punch/trash/" ) . join ( name) )
115
124
. expect ( "unable to parse directory" ) ;
116
125
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
+ }
118
129
119
130
for entry in entries {
120
131
if let Ok ( entry) = entry {
121
132
let file_type = entry. file_type ( ) . ok ( ) . unwrap ( ) ;
122
133
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 ( ) ) ) ?
124
135
} else {
125
- punch:: move_file (
126
- & home_path
136
+ let from = & home_path
127
137
. 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
+ }
131
143
}
132
144
}
133
145
}
134
146
} 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
+ }
139
152
}
140
153
//delete file in trash after
141
154
142
155
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
+ ;
144
160
} 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
+ }
147
164
}
165
+ Ok ( ( ) )
148
166
}
149
167
//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 < ( ) > {
151
169
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
+ }
153
173
} 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
+ }
155
177
}
178
+
179
+ Ok ( ( ) )
156
180
}
157
- pub fn undo ( ) {
181
+ pub fn undo ( ) -> Result < ( ) > {
158
182
let file_iter = db_connect ! ( ".punch/punch.db" , pull) ;
159
183
160
184
let latest_file = file_iter. last ( ) . unwrap ( ) ;
161
185
162
186
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 ) ) ? ;
164
188
} 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 ) ) ? ;
166
190
}
191
+ Ok ( ( ) )
167
192
}
168
193
169
- pub fn delete ( name : String ) {
194
+ pub fn delete ( name : String ) -> Result < ( ) > {
170
195
let conn = db_connect ! ( ".punch/punch.db" , conn) ;
171
196
conn. execute ( "DELETE FROM files WHERE name=(?1)" , [ & name] )
172
197
. unwrap ( ) ;
198
+ Ok ( ( ) )
173
199
}
0 commit comments