//! Models to export and import the accounts use crate::entity::prelude::*; use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize)] pub struct DecryptedAccount { pub name: String, pub login: String, pub password: String, } impl DecryptedAccount { /// Constructs DecryptedAccount by decrypting the provided account #[inline] pub fn from_account(account: account::Model, master_pass: &str) -> crate::Result { let (login, password) = account.decrypt(master_pass)?; Ok(Self { name: account.name, login, password, }) } /// Constructs ActiveModel with eath field Set by encrypting `self` #[inline] pub fn into_account( self, user_id: u64, master_pass: &str, ) -> crate::Result { account::ActiveModel::from_unencrypted( user_id, self.name, &self.login, &self.password, master_pass, ) } /// Returns true if the account's fields are valid #[inline] pub fn validate(&self) -> bool { for string in [&self.name, &self.login, &self.password] { let is_invalid = string .chars() .any(|char| char == '`' || char == '\\' || char == '\n'); if is_invalid { return false; } } true } } #[derive(Serialize, Deserialize)] pub struct User { pub accounts: Vec, }