diff --git a/Cargo.toml b/Cargo.toml index 8366cee..bac6871 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,5 +5,7 @@ edition = "2021" [dependencies] base64 = "0.22.1" +bincode = "1.3.3" rand = "0.8.5" ring = "0.17.8" +serde = { version = "1.0.203", features = ["derive"] } \ No newline at end of file diff --git a/src/manager.rs b/src/manager.rs index b600d6f..ad644af 100644 --- a/src/manager.rs +++ b/src/manager.rs @@ -1,12 +1,18 @@ // Libraries +use bincode; use rand::Rng; +use serde::{Serialize, Deserialize}; +use crate::{interface, resource}; + // Structs +#[derive(Serialize, Deserialize)] pub struct Password { pub name: String, pub user: String, pub phrase: String, } +#[derive(Serialize, Deserialize)] pub struct Manager{ passwords: Vec } @@ -15,10 +21,40 @@ pub struct Manager{ impl Manager { // Constructors pub fn init() -> Self { - return Manager { passwords: vec![] }; + let mut manager: Manager = Manager { passwords: vec![] }; + + if resource::ResourceFileBin::exists("res/manager.dat") { + manager.load(); + } + + return manager; } // Functions + fn save(&self) { + // Turning the manager into binary data + let manager_binary: Vec = bincode::serialize(self).expect("Failed to serialize manager"); + + // Saving via Binary Reader + resource::ResourceFileBin::write("res/manager.dat", manager_binary); + + // Message: + println!("{}[Saved]{}", interface::COLOR_GREEN, interface::COLOR_RESET); + } + fn load(&mut self) { + // Saving via Binary Reader + let content: resource::ResourceFileBin = resource::ResourceFileBin::read("res/manager.dat"); + + // Turning the manager into binary data + let manager: Manager = bincode::deserialize(&content.content).expect("Failed to serialize manager"); + + // Setting values + self.passwords = manager.passwords; + + // Message: + println!("{}[Loaded]{}", interface::COLOR_GREEN, interface::COLOR_RESET); + } + pub fn password_generate(length: u32, numbers: bool, special: bool) -> String { // Variables let mut result: String = String::new(); @@ -50,6 +86,9 @@ impl Manager { // Adding it to our list self.passwords.push(new_password); + + // Saving the password manager + self.save(); } pub fn password_view(&mut self, name: String) -> (bool, Password) { // Variables diff --git a/src/resource.rs b/src/resource.rs index abf529f..e3545ec 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -1,11 +1,15 @@ // Libraries -use std::{fs, path::Path}; +use std::{fs::{read_to_string, write, File}, io::{self, Read}, path::Path}; // Structures pub struct ResourceFile { pub path: String, pub content: String } +pub struct ResourceFileBin { + pub path: String, + pub content: Vec +} // Implementations impl ResourceFile { @@ -14,16 +18,38 @@ impl ResourceFile { } pub fn read(file_path: &str) -> Self { // Reading from a file: - let content: String = fs::read_to_string(String::from(file_path)).expect("Failed to read file."); + let content: String = read_to_string(String::from(file_path)).expect("Failed to read file."); // Returning the value: return ResourceFile {path: String::from(file_path), content}; } pub fn write(file_path: &str, content: String) -> Self { // Writing to file - fs::write(file_path, &content).expect("Failed writing to file"); + write(file_path, &content).expect("Failed writing to file"); // Returning the value: return ResourceFile {path: String::from(file_path), content: content}; } +} +impl ResourceFileBin { + pub fn exists(file_path: &str) -> bool{ + return Path::new(file_path).exists(); + } + pub fn read(file_path: &str) -> Self { + // Reading from a file: + let file: File = File::open(String::from(file_path)).expect("Failed to read file."); + let mut reader: io::BufReader = io::BufReader::new(file); + let mut content: Vec = Vec::new(); + reader.read_to_end(&mut content).expect("Failed to buffer file."); + + // Returning the value: + return ResourceFileBin {path: String::from(file_path), content}; + } + pub fn write(file_path: &str, content: Vec) -> Self { + // Writing to file + write(file_path, &content).expect("Failed writing to file"); + + // Returning the value: + return ResourceFileBin {path: String::from(file_path), content: content}; + } } \ No newline at end of file