pass_manager/src/models.rs

60 lines
1.4 KiB
Rust
Raw Normal View History

2023-05-15 16:09:15 +00:00
//! Models to export and import the accounts
use crate::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct DecryptedAccount {
pub name: String,
pub login: String,
pub password: String,
}
impl DecryptedAccount {
2023-11-16 18:51:46 +00:00
/// Constructs `DecryptedAccount` by decrypting the provided account
2023-05-15 16:09:15 +00:00
#[inline]
pub fn from_account(account: account::Model, master_pass: &str) -> crate::Result<Self> {
let (login, password) = account.decrypt(master_pass)?;
Ok(Self {
2023-05-15 16:09:15 +00:00
name: account.name,
login,
password,
})
}
2023-11-16 18:51:46 +00:00
/// Constructs `ActiveModel` with eath field Set by encrypting `self`
2023-05-15 16:09:15 +00:00
#[inline]
pub fn into_account(
self,
user_id: u64,
master_pass: &str,
) -> crate::Result<account::ActiveModel> {
2023-05-15 16:09:15 +00:00
account::ActiveModel::from_unencrypted(
user_id,
self.name,
&self.login,
&self.password,
master_pass,
)
.map_err(Into::into)
}
2023-05-09 17:27:58 +00:00
/// Returns true if the account's fields are valid
#[inline]
pub fn validate(&self) -> bool {
[
self.name.as_str(),
self.login.as_str(),
self.password.as_str(),
]
.into_iter()
.all(validate_field)
}
}
#[derive(Serialize, Deserialize)]
2023-06-11 12:58:41 +00:00
#[repr(transparent)]
pub struct User {
pub accounts: Vec<DecryptedAccount>,
}