File tree Expand file tree Collapse file tree 3 files changed +23
-14
lines changed Expand file tree Collapse file tree 3 files changed +23
-14
lines changed Original file line number Diff line number Diff line change @@ -189,14 +189,15 @@ pub fn export_database() -> Result<String, String> {
189
189
}
190
190
}
191
191
192
- pub fn overwrite_database ( elements : Vec < OTPElement > ) {
193
- let json_string: & str = & serde_json:: to_string ( & elements) . unwrap ( ) ;
194
- overwrite_database_json ( json_string) ;
192
+ pub fn overwrite_database ( elements : Vec < OTPElement > ) -> Result < ( ) , std :: io :: Error > {
193
+ let json_string: & str = & serde_json:: to_string ( & elements) ? ;
194
+ overwrite_database_json ( json_string)
195
195
}
196
196
197
- pub fn overwrite_database_json ( json : & str ) {
197
+ pub fn overwrite_database_json ( json : & str ) -> Result < ( ) , std :: io :: Error > {
198
198
let encrypted = cryptograpy:: encrypt_string ( json. to_string ( ) , & cryptograpy:: prompt_for_passwords ( "Insert password for database encryption: " , 8 ) ) ;
199
- utils:: write_to_file ( & encrypted, & mut File :: create ( utils:: get_db_path ( ) ) . expect ( "Failed to open file" ) ) ;
199
+ let mut file = File :: create ( utils:: get_db_path ( ) ) ?;
200
+ utils:: write_to_file ( & encrypted, & mut file)
200
201
}
201
202
202
203
fn check_elements ( id : usize , elements : & Vec < OTPElement > ) -> Result < ( ) , String > {
Original file line number Diff line number Diff line change @@ -34,9 +34,12 @@ fn init() -> Result<(), String>{
34
34
Ok ( ( ) ) => { } ,
35
35
Err ( ( ) ) => {
36
36
return Err ( String :: from ( "An error occurred during database creation" ) ) ;
37
- }
37
+ } ,
38
+ }
39
+ match database_loader:: overwrite_database_json ( "[]" ) {
40
+ Ok ( ( ) ) => Ok ( ( ) ) ,
41
+ Err ( _e) => Err ( String :: from ( "An error occurred during database overwriting" ) ) ,
38
42
}
39
- Ok ( ( ) )
40
43
}
41
44
42
45
fn main ( ) {
Original file line number Diff line number Diff line change @@ -27,21 +27,26 @@ fn get_cotp_folder() -> PathBuf{
27
27
}
28
28
29
29
pub fn create_db_if_needed ( ) -> Result < ( ) , ( ) > {
30
- if !get_cotp_folder ( ) . exists ( ) {
31
- match std:: fs:: create_dir ( get_cotp_folder ( ) ) {
30
+ let cotp_folder = get_cotp_folder ( ) ;
31
+ if !cotp_folder. exists ( ) {
32
+ match std:: fs:: create_dir ( cotp_folder) {
32
33
Ok ( ( ) ) => println ! ( "Created .cotp folder" ) ,
33
34
Err ( _e) => ( ) ,
34
35
}
35
36
}
36
- if !Path :: new ( & get_db_path ( ) ) . exists ( ) {
37
- database_loader:: overwrite_database_json ( "[]" ) ;
37
+ let db_path = get_db_path ( ) ;
38
+ if !db_path. exists ( ) {
39
+ match std:: fs:: File :: create ( db_path) {
40
+ Ok ( _f) => return Ok ( ( ) ) ,
41
+ Err ( _e) => return Err ( ( ) ) ,
42
+ }
38
43
}
39
44
Ok ( ( ) )
40
45
}
41
46
42
- pub fn write_to_file ( content : & str , file : & mut File ) {
43
- file. write_all ( content. as_bytes ( ) ) . expect ( "Error writing to file" ) ;
44
- file. sync_all ( ) . expect ( "Sync failed" ) ;
47
+ pub fn write_to_file ( content : & str , file : & mut File ) -> Result < ( ) , std :: io :: Error > {
48
+ file. write_all ( content. as_bytes ( ) ) ? ;
49
+ file. sync_all ( )
45
50
}
46
51
47
52
pub fn print_progress_bar ( ) {
You can’t perform that action at this time.
0 commit comments