feat: Implement murmur hash command
This commit is contained in:
parent
a4c6ba731a
commit
764d9c00f2
2 changed files with 42 additions and 8 deletions
|
@ -2,6 +2,7 @@ use std::sync::Arc;
|
||||||
|
|
||||||
use clap::{Arg, ArgAction, ArgMatches, Command};
|
use clap::{Arg, ArgAction, ArgMatches, Command};
|
||||||
use color_eyre::eyre::Result;
|
use color_eyre::eyre::Result;
|
||||||
|
use sdk::murmur::{Murmur32, Murmur64};
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
|
|
||||||
pub(crate) fn command_definition() -> Command {
|
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)]
|
#[tracing::instrument(skip_all)]
|
||||||
pub(crate) async fn run(_ctx: Arc<RwLock<sdk::Context>>, _matches: &ArgMatches) -> Result<()> {
|
pub(crate) async fn run(_ctx: Arc<RwLock<sdk::Context>>, matches: &ArgMatches) -> Result<()> {
|
||||||
unimplemented!()
|
match matches.subcommand() {
|
||||||
|
Some(("hash", sub_matches)) => {
|
||||||
|
let s = sub_matches
|
||||||
|
.get_one::<String>("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"
|
||||||
|
),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,8 +30,14 @@ fn _swap_bytes_u64(value: u64) -> u64 {
|
||||||
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
|
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
|
||||||
pub struct Murmur64(u64);
|
pub struct Murmur64(u64);
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
|
impl Murmur64 {
|
||||||
pub struct Murmur32(u32);
|
pub fn hash<B>(s: B) -> Self
|
||||||
|
where
|
||||||
|
B: AsRef<[u8]>,
|
||||||
|
{
|
||||||
|
hash(s.as_ref(), SEED as u64).into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Deref for Murmur64 {
|
impl Deref for Murmur64 {
|
||||||
type Target = u64;
|
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<B>(s: B) -> Self
|
||||||
|
where
|
||||||
|
B: AsRef<[u8]>,
|
||||||
|
{
|
||||||
|
hash32(s.as_ref(), SEED).into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<u32> for Murmur32 {
|
impl From<u32> for Murmur32 {
|
||||||
fn from(value: u32) -> Self {
|
fn from(value: u32) -> Self {
|
||||||
Self(value)
|
Self(value)
|
||||||
|
|
Loading…
Add table
Reference in a new issue