Skip to content

refactor code #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 34 additions & 34 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ license = "MIT"
readme = "README.md"
version = "1.2.0"
authors = ["Joern Bernhardt <[email protected]>"]
edition = "2021"

# See https://crates.io/category_slugs
categories = ["command-line-utilities"]

exclude = [
"docs/*"
]
exclude = ["docs/*"]

[dependencies]
base64 = "0.21.5"
Expand Down
58 changes: 16 additions & 42 deletions src/diff/entry.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,29 @@
use base64::{engine::general_purpose, Engine as _};
use keepass::db::Value;
use std::collections::HashMap;

use crate::diff::field::{Field, ValueType};
use crate::diff::field::Field;
use crate::diff::{Diff, DiffResult, DiffResultFormat};

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Entry {
pub fields: HashMap<String, Field>,
fields: HashMap<String, Field>,
use_verbose: bool,
mask_passwords: bool,
}

impl Entry {
pub fn from_keepass(e: &keepass::db::Entry, use_verbose: bool, mask_passwords: bool) -> Self {
pub fn from_keepass(
entry: &keepass::db::Entry,
use_verbose: bool,
mask_passwords: bool,
) -> Self {
// username, password, etc. are just fields
let fields = e
let fields = entry
.fields
.iter()
.map(|(k, v)| {
.map(|(key, value)| {
(
k.to_owned(),
Field {
name: k.to_owned(),
value: match v {
Value::Bytes(b) => general_purpose::STANDARD_NO_PAD.encode(b),
Value::Unprotected(v) => v.to_owned(),
Value::Protected(p) => String::from_utf8(p.unsecure().to_owned())
.unwrap()
.to_owned(),
},
kind: match v {
Value::Bytes(_) => ValueType::Binary,
Value::Unprotected(_) => ValueType::Unprotected,
Value::Protected(_) => ValueType::Protected,
},
use_verbose,
mask_passwords,
},
key.to_owned(),
Field::from_keepass(key.to_owned(), value, use_verbose, mask_passwords),
)
})
.collect();
Expand All @@ -56,11 +42,10 @@ impl Diff for Entry {
crate::diff::diff_entry(&self.fields, &other.fields);

if has_differences {
let mut inner_differences: Vec<Box<dyn DiffResultFormat>> = Vec::new();

for dr in field_differences {
inner_differences.push(Box::new(dr))
}
let inner_differences = field_differences
.into_iter()
.map(|dr| Box::new(dr) as Box<dyn DiffResultFormat>)
.collect();

DiffResult::InnerDifferences {
left: self,
Expand All @@ -78,18 +63,7 @@ impl Diff for Entry {

impl std::fmt::Display for Entry {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let name = self
.fields
.get("Title")
.unwrap_or(&Field {
name: "Title".to_string(),
value: "".to_string(),
kind: ValueType::Unprotected,
use_verbose: self.use_verbose,
mask_passwords: self.mask_passwords,
})
.value
.clone();
let name = self.fields.get("Title").map(|f| f.value()).unwrap_or("");
if self.use_verbose {
write!(f, "Entry '{}'", name)
} else {
Expand Down
68 changes: 45 additions & 23 deletions src/diff/field.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use base64::{engine::general_purpose, Engine};
use keepass::db::Value;

use crate::diff::{Diff, DiffResult};

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
Expand All @@ -9,11 +12,42 @@ pub enum ValueType {

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Field {
pub name: String,
pub value: String,
pub kind: ValueType,
pub use_verbose: bool,
pub mask_passwords: bool,
name: String,
value: String,
kind: ValueType,
use_verbose: bool,
mask_passwords: bool,
}

impl Field {
pub fn from_keepass(
name: String,
value: &Value,
use_verbose: bool,
mask_passwords: bool,
) -> Self {
Field {
name,
value: match value {
Value::Bytes(b) => general_purpose::STANDARD_NO_PAD.encode(b),
Value::Unprotected(v) => v.to_owned(),
Value::Protected(p) => String::from_utf8(p.unsecure().to_owned())
.unwrap()
.to_owned(),
},
kind: match value {
Value::Bytes(_) => ValueType::Binary,
Value::Unprotected(_) => ValueType::Unprotected,
Value::Protected(_) => ValueType::Protected,
},
use_verbose,
mask_passwords,
}
}

pub fn value(&self) -> &str {
self.value.as_str()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be written as follows:

- self.value.as_str(),
+ &self.value,

}
}

impl Diff for Field {
Expand All @@ -34,26 +68,14 @@ impl Diff for Field {

impl std::fmt::Display for Field {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let password_str = match (self.mask_passwords, self.kind) {
(true, ValueType::Protected) => "***",
_ => self.value.as_str(),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be written as follows:

- _ => self.value.as_str(),
+ _ => &self.value,

};
if self.use_verbose {
write!(
f,
"Field '{}' = '{}'",
self.name,
match (self.mask_passwords, self.kind) {
(true, ValueType::Protected) => "***".to_owned(),
_ => self.value.to_owned(),
}
)
write!(f, "Field '{}' = '{}'", self.name, password_str)
} else {
write!(
f,
"{} = {}",
self.name,
match (self.mask_passwords, self.kind) {
(true, ValueType::Protected) => "***".to_owned(),
_ => self.value.to_owned(),
}
)
write!(f, "{} = {}", self.name, password_str)
}
}
}
Loading