diff --git a/crates/dtmt/src/cmd/murmur.rs b/crates/dtmt/src/cmd/murmur.rs index a5e53f4..0062692 100644 --- a/crates/dtmt/src/cmd/murmur.rs +++ b/crates/dtmt/src/cmd/murmur.rs @@ -2,6 +2,7 @@ use std::sync::Arc; use clap::{Arg, ArgAction, ArgMatches, Command}; use color_eyre::eyre::Result; +use sdk::murmur::{Murmur32, Murmur64}; use tokio::sync::RwLock; pub(crate) fn command_definition() -> Command { @@ -24,13 +25,28 @@ pub(crate) fn command_definition() -> Command { ), ), ) - .subcommand( - Command::new("lookup") - .arg(Arg::new("hash").required(true).help("The hash to look up.")), - ) } #[tracing::instrument(skip_all)] -pub(crate) async fn run(_ctx: Arc>, _matches: &ArgMatches) -> Result<()> { - unimplemented!() +pub(crate) async fn run(_ctx: Arc>, matches: &ArgMatches) -> Result<()> { + match matches.subcommand() { + Some(("hash", sub_matches)) => { + let s = sub_matches + .get_one::("string") + .expect("missing required argument"); + + if sub_matches.get_flag("half") { + let hash = Murmur32::hash(&s); + println!("{hash:08X}"); + } else { + let hash = Murmur64::hash(&s); + println!("{hash:016X}"); + } + + Ok(()) + } + _ => unreachable!( + "clap is configured to require a subcommand, and they're all handled above" + ), + } } diff --git a/lib/sdk/src/murmur/mod.rs b/lib/sdk/src/murmur/mod.rs index f3323d0..df958ee 100644 --- a/lib/sdk/src/murmur/mod.rs +++ b/lib/sdk/src/murmur/mod.rs @@ -30,8 +30,14 @@ fn _swap_bytes_u64(value: u64) -> u64 { #[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)] pub struct Murmur64(u64); -#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)] -pub struct Murmur32(u32); +impl Murmur64 { + pub fn hash(s: B) -> Self + where + B: AsRef<[u8]>, + { + hash(s.as_ref(), SEED as u64).into() + } +} impl Deref for Murmur64 { type Target = u64; @@ -123,6 +129,18 @@ impl Serialize for Murmur64 { } } +#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)] +pub struct Murmur32(u32); + +impl Murmur32 { + pub fn hash(s: B) -> Self + where + B: AsRef<[u8]>, + { + hash32(s.as_ref(), SEED).into() + } +} + impl From for Murmur32 { fn from(value: u32) -> Self { Self(value)