Move Format of export and import to password/mod.rs

This commit is contained in:
TheAwiteb 2023-12-29 10:25:16 +03:00
parent f00725aec0
commit 0fe3e748d9
No known key found for this signature in database
GPG key ID: ABF818BD15DC2D34
2 changed files with 22 additions and 24 deletions

View file

@ -16,45 +16,28 @@
use std::{fs, path::PathBuf}; use std::{fs, path::PathBuf};
use clap::{Args, ValueEnum}; use clap::Args;
use crate::{ use crate::{
password::{BitWardenPasswords, Passwords}, password::{BitWardenPasswords, Format, Passwords},
LprsError, LprsResult, RunCommand, LprsError, LprsResult, RunCommand,
}; };
#[derive(Clone, Debug, ValueEnum)]
pub enum ExportFormat {
Lprs,
BitWarden,
}
#[derive(Debug, Args)] #[derive(Debug, Args)]
#[command(author, version, about, long_about = None)] #[command(author, version, about, long_about = None)]
pub struct Export { pub struct Export {
/// The path to export to /// The path to export to
path: PathBuf, path: PathBuf,
/// Format to export passwords in /// Format to export passwords in
#[arg(short, long, value_name = "FORMAT", default_value_t= ExportFormat::Lprs)] #[arg(short, long, value_name = "FORMAT", default_value_t= Format::Lprs)]
format: ExportFormat, format: Format,
}
impl ToString for ExportFormat {
fn to_string(&self) -> String {
self.to_possible_value()
.expect("There is no skiped values")
.get_name()
.to_owned()
}
} }
impl RunCommand for Export { impl RunCommand for Export {
fn run(&self, password_manager: Passwords) -> LprsResult<()> { fn run(&self, password_manager: Passwords) -> LprsResult<()> {
let exported_data = match self.format { let exported_data = match self.format {
ExportFormat::Lprs => serde_json::to_string(&password_manager.encrypt()?.passwords), Format::Lprs => serde_json::to_string(&password_manager.encrypt()?.passwords),
ExportFormat::BitWarden => { Format::BitWarden => serde_json::to_string(&BitWardenPasswords::from(password_manager)),
serde_json::to_string(&BitWardenPasswords::from(password_manager))
}
} }
.map_err(LprsError::from)?; .map_err(LprsError::from)?;

View file

@ -16,7 +16,7 @@
use std::{fs, path::PathBuf}; use std::{fs, path::PathBuf};
use clap::Parser; use clap::{Parser, ValueEnum};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{LprsError, LprsResult}; use crate::{LprsError, LprsResult};
@ -29,6 +29,12 @@ mod validator;
pub use bitwarden::*; pub use bitwarden::*;
pub use validator::*; pub use validator::*;
#[derive(Clone, Debug, ValueEnum)]
pub enum Format {
Lprs,
BitWarden,
}
/// The password struct /// The password struct
#[serde_with_macros::skip_serializing_none] #[serde_with_macros::skip_serializing_none]
#[derive(Clone, Debug, Deserialize, Serialize, Parser)] #[derive(Clone, Debug, Deserialize, Serialize, Parser)]
@ -145,3 +151,12 @@ impl Passwords {
self.passwords.push(password) self.passwords.push(password)
} }
} }
impl ToString for Format {
fn to_string(&self) -> String {
self.to_possible_value()
.expect("There is no skiped values")
.get_name()
.to_owned()
}
}