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

View file

@ -16,7 +16,7 @@
use std::{fs, path::PathBuf};
use clap::Parser;
use clap::{Parser, ValueEnum};
use serde::{Deserialize, Serialize};
use crate::{LprsError, LprsResult};
@ -29,6 +29,12 @@ mod validator;
pub use bitwarden::*;
pub use validator::*;
#[derive(Clone, Debug, ValueEnum)]
pub enum Format {
Lprs,
BitWarden,
}
/// The password struct
#[serde_with_macros::skip_serializing_none]
#[derive(Clone, Debug, Deserialize, Serialize, Parser)]
@ -145,3 +151,12 @@ impl Passwords {
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()
}
}