From 1d084981311133e14646b65eef2ee251cd6a9de1 Mon Sep 17 00:00:00 2001 From: Lucas Schwiderski Date: Fri, 17 Feb 2023 11:10:56 +0100 Subject: [PATCH] feat(dtmt): Add command to print the dictionary This is mostly helpful to check/debug whether the internal dictionary actually contains the expected data. For manually looking through the entire dictionary, opening the CSV file is still more convenient. --- Cargo.lock | 23 +++++++++++++++++++++ crates/dtmt/Cargo.toml | 1 + crates/dtmt/src/cmd/dictionary.rs | 34 +++++++++++++++++++++++++++++++ lib/sdk/src/murmur/dictionary.rs | 22 ++++++++++++++++++++ lib/sdk/src/murmur/mod.rs | 3 +-- 5 files changed, 81 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4dddb00..2e3679a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -291,6 +291,28 @@ dependencies = [ "os_str_bytes", ] +[[package]] +name = "cli-table" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adfbb116d9e2c4be7011360d0c0bee565712c11e969c9609b25b619366dc379d" +dependencies = [ + "cli-table-derive", + "termcolor", + "unicode-width", +] + +[[package]] +name = "cli-table-derive" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2af3bfb9da627b0a6c467624fb7963921433774ed435493b5c08a3053e829ad4" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "clipboard-win" version = "4.4.2" @@ -654,6 +676,7 @@ name = "dtmt" version = "0.2.0" dependencies = [ "clap", + "cli-table", "color-eyre", "confy", "csv-async", diff --git a/crates/dtmt/Cargo.toml b/crates/dtmt/Cargo.toml index 73d2d72..cb8f646 100644 --- a/crates/dtmt/Cargo.toml +++ b/crates/dtmt/Cargo.toml @@ -26,6 +26,7 @@ confy = "0.5.1" zip = "0.6.3" string_template = "0.2.1" promptly = "0.3.1" +cli-table = { version = "0.4.7", default-features = false, features = ["derive"] } [dev-dependencies] tempfile = "3.3.0" diff --git a/crates/dtmt/src/cmd/dictionary.rs b/crates/dtmt/src/cmd/dictionary.rs index 22a225b..e94e2ad 100644 --- a/crates/dtmt/src/cmd/dictionary.rs +++ b/crates/dtmt/src/cmd/dictionary.rs @@ -1,8 +1,10 @@ use std::path::PathBuf; use clap::{value_parser, Arg, ArgAction, ArgMatches, Command, ValueEnum}; +use cli_table::{print_stdout, WithTitle}; use color_eyre::eyre::{Context, Result}; use color_eyre::{Help, SectionExt}; +use sdk::murmur::{IdString64, Murmur32, Murmur64}; use tokio::fs::File; use tokio::io::{AsyncBufReadExt, BufReader}; use tokio_stream::wrappers::LinesStream; @@ -27,6 +29,29 @@ impl From for sdk::murmur::HashGroup { } } +#[derive(cli_table::Table)] +struct TableRow { + #[table(title = "Value")] + value: String, + #[table(title = "Murmur64")] + long: Murmur64, + #[table(title = "Murmur32")] + short: Murmur32, + #[table(title = "Group")] + group: sdk::murmur::HashGroup, +} + +impl From<&sdk::murmur::Entry> for TableRow { + fn from(entry: &sdk::murmur::Entry) -> Self { + Self { + value: entry.value().clone(), + long: entry.long(), + short: entry.short(), + group: entry.group(), + } + } +} + pub(crate) fn command_definition() -> Command { Command::new("dictionary") .about("Manipulate a hash dictionary file.") @@ -67,6 +92,7 @@ pub(crate) fn command_definition() -> Command { .value_parser(value_parser!(PathBuf)), ), ) + .subcommand(Command::new("show").about("Show the contents of the dictionary")) .subcommand(Command::new("save").about( "Save back the currently loaded dictionary, with hashes pre-computed. \ Pre-computing hashes speeds up loading large dictionaries, as they would \ @@ -176,6 +202,14 @@ pub(crate) async fn run(mut ctx: sdk::Context, matches: &ArgMatches) -> Result<( .await .wrap_err("Failed to write dictionary to disk") } + Some(("show", _)) => { + let lookup = &ctx.lookup; + let rows: Vec<_> = lookup.entries().iter().map(TableRow::from).collect(); + + print_stdout(rows.with_title())?; + + Ok(()) + } _ => unreachable!( "clap is configured to require a subcommand, and they're all handled above" ), diff --git a/lib/sdk/src/murmur/dictionary.rs b/lib/sdk/src/murmur/dictionary.rs index 322dded..2d51af1 100644 --- a/lib/sdk/src/murmur/dictionary.rs +++ b/lib/sdk/src/murmur/dictionary.rs @@ -55,6 +55,24 @@ pub struct Entry { group: HashGroup, } +impl Entry { + pub fn value(&self) -> &String { + &self.value + } + + pub fn long(&self) -> Murmur64 { + self.long + } + + pub fn short(&self) -> Murmur32 { + self.short + } + + pub fn group(&self) -> HashGroup { + self.group + } +} + pub struct Dictionary { entries: Vec, } @@ -172,4 +190,8 @@ impl Dictionary { pub fn is_empty(&self) -> bool { self.entries.is_empty() } + + pub fn entries(&self) -> &Vec { + &self.entries + } } diff --git a/lib/sdk/src/murmur/mod.rs b/lib/sdk/src/murmur/mod.rs index 95e66fa..784a6df 100644 --- a/lib/sdk/src/murmur/mod.rs +++ b/lib/sdk/src/murmur/mod.rs @@ -13,8 +13,7 @@ mod murmurhash64; pub const SEED: u32 = 0; -pub use dictionary::Dictionary; -pub use dictionary::HashGroup; +pub use dictionary::{Dictionary, Entry, HashGroup}; pub use murmurhash64::hash; pub use murmurhash64::hash32; pub use murmurhash64::hash_inverse as inverse;