Skip to content

Commit 794e2f9

Browse files
author
replydev
committed
File file creation and file writing
1 parent 17452df commit 794e2f9

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

src/database_loader.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,15 @@ pub fn export_database() -> Result<String, String> {
189189
}
190190
}
191191

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)
195195
}
196196

197-
pub fn overwrite_database_json(json: &str){
197+
pub fn overwrite_database_json(json: &str) -> Result<(),std::io::Error>{
198198
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)
200201
}
201202

202203
fn check_elements(id: usize,elements: &Vec<OTPElement>) -> Result<(),String>{

src/main.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ fn init() -> Result<(), String>{
3434
Ok(()) => {},
3535
Err(()) => {
3636
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")),
3842
}
39-
Ok(())
4043
}
4144

4245
fn main() {

src/utils.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,26 @@ fn get_cotp_folder() -> PathBuf{
2727
}
2828

2929
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){
3233
Ok(()) => println!("Created .cotp folder"),
3334
Err(_e) => (),
3435
}
3536
}
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+
}
3843
}
3944
Ok(())
4045
}
4146

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()
4550
}
4651

4752
pub fn print_progress_bar(){

0 commit comments

Comments
 (0)