Skip to content

Commit

Permalink
Added support for post-processing deplicate records.
Browse files Browse the repository at this point in the history
  • Loading branch information
oubiwann committed Dec 21, 2023
1 parent b9e10aa commit 319380b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 18 deletions.
45 changes: 44 additions & 1 deletion rucksack/src/command/handlers/dedupe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@
//! ```shell
//! rucksack deduple --type updated
//! ```
use std::fmt;

// use anyhow::{anyhow, Result};
use anyhow::Result;
use clap::ArgMatches;

// use rucksack_db as store;
use rucksack_db::db::DB;
// use rucksack_db::records;
use rucksack_db::records::DecryptedRecord;

use crate::app::App;
use crate::output::{result, Column};

// use crate::input::{query, Flag};

pub fn new(matches: &ArgMatches, app: &App) -> Result<()> {
Expand All @@ -45,6 +49,45 @@ pub fn new(matches: &ArgMatches, app: &App) -> Result<()> {
Ok(())
}

pub fn post_process(in_groups: result::GroupByString) -> result::GroupByString {
let mut out_groups = result::GroupByString::new();
for (key, group) in in_groups.into_iter() {
let entry = out_groups.entry(key).or_default();
for r in group.iter() {
let mut updated = r.clone();
updated.add(Column::DupeInfo, DupeInfo::Duplicate.name());
entry.push(updated);
}
}
out_groups
}

pub fn dupe_info(_record_a: DecryptedRecord, _record_b: DecryptedRecord) -> DupeInfo {
DupeInfo::Primary
}

#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum DupeInfo {
Primary,
InHistory,
Duplicate,
}

impl DupeInfo {
pub fn name(&self) -> String {
match self {
DupeInfo::InHistory => "in history".to_string(),
_ => format!("{self}").to_lowercase(),
}
}
}

impl fmt::Display for DupeInfo {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}

fn dedupe_exact(_matches: &ArgMatches, _db: &DB) -> Result<(), anyhow::Error> {
log::debug!("Performing exact record deduplication ...");
Ok(())
Expand Down
34 changes: 18 additions & 16 deletions rucksack/src/command/handlers/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ use crate::app::App;
use crate::input::{options, query, Flag};
use crate::output::{result, table, Column, Opts};

use super::backup;
use super::{backup, dedupe};

// TODO: once there's config for it, pull from config and pass
// options here from top-level app.
Expand All @@ -157,6 +157,21 @@ pub fn backups(matches: &ArgMatches, app: &App) -> Result<()> {
backup::list(matches, app)
}

// TODO: once there's config for it, pull from config and pass
// options here from top-level app.
// TODO: or not, depending upon the outcome of this ticket:
// * https://github.com/oxur/rucksack/issues/92
pub fn deleted(matches: &ArgMatches, app: &App) -> Result<()> {
process_records(
matches,
app,
Opts {
only_deleted: true,
..Default::default()
},
)
}

pub fn duplicates(matches: &ArgMatches, app: &App) -> Result<()> {
let mut hash_fields: Vec<Column> = Vec::new();
let mut default_hash_fields = exact_hash_fields();
Expand Down Expand Up @@ -189,21 +204,6 @@ pub fn duplicates(matches: &ArgMatches, app: &App) -> Result<()> {
)
}

// TODO: once there's config for it, pull from config and pass
// options here from top-level app.
// TODO: or not, depending upon the outcome of this ticket:
// * https://github.com/oxur/rucksack/issues/92
pub fn deleted(matches: &ArgMatches, app: &App) -> Result<()> {
process_records(
matches,
app,
Opts {
only_deleted: true,
..Default::default()
},
)
}

// TODO: once there's config for it, pull from config and pass
// options here from top-level app.
pub fn keys(matches: &ArgMatches, app: &App) -> Result<()> {
Expand Down Expand Up @@ -308,6 +308,8 @@ fn process_records(matches: &ArgMatches, app: &App, mut opts: Opts) -> Result<()
} else if opts.group_by_password {
(group_count, count) = print_password_group(groups, sort_by, &opts);
} else if opts.group_by_hash {
// Post-process groups
groups = dedupe::post_process(groups);
(group_count, count) = print_hash_group(groups, sort_by, &opts);
} else {
let mut t = table::new(results.to_owned(), opts.clone());
Expand Down
9 changes: 8 additions & 1 deletion rucksack/src/output/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub enum Column {
Category,
Count,
Created,
DupeInfo,
Hash,
Id,
Imported,
Expand Down Expand Up @@ -37,6 +38,7 @@ impl Column {
pub fn name(&self) -> String {
match self {
Column::Count => "Access Count".to_string(),
Column::DupeInfo => "Duplicate Info".to_string(),
Column::Kind => "Type".to_string(),
Column::LastUpdated => "Last Updated".to_string(),
Column::Score => "Score / Strength".to_string(),
Expand Down Expand Up @@ -140,7 +142,12 @@ pub struct ColsGroupByHash;

impl Columns for ColsGroupByHash {
fn pre(&self, _opts: &Opts) -> Vec<Column> {
vec![Column::Name, Column::Kind, Column::Category]
vec![
Column::Name,
Column::Kind,
Column::Category,
Column::DupeInfo,
]
}

fn post(&self, _opts: &Opts, mut cols: Vec<Column>) -> Vec<Column> {
Expand Down

0 comments on commit 319380b

Please sign in to comment.