Skip to content

Commit cf3b4d5

Browse files
committed
chore: some cleanup
Signed-off-by: Richard Zak <[email protected]>
1 parent 6653a23 commit cf3b4d5

File tree

6 files changed

+35
-40
lines changed

6 files changed

+35
-40
lines changed

client-py/src/lib.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use crate::types::{Label, ServerInfo, Source, SupportedFileType, UserInfo};
1717
use malwaredb_client::blocking::MdbClient;
1818

1919
use anyhow::{anyhow, Result};
20-
use malwaredb_client::malwaredb_api::PartialHashSearchType;
2120
use pyo3::prelude::*;
2221

2322
/// MDB version
@@ -108,18 +107,12 @@ impl MalwareDBClient {
108107
limit: u32,
109108
response_hash: &str,
110109
) -> Result<Vec<String>> {
111-
let hash_type = match hash_type {
112-
"md5" => PartialHashSearchType::MD5,
113-
"sha1" => PartialHashSearchType::SHA1,
114-
"sha256" => PartialHashSearchType::SHA256,
115-
"sha384" => PartialHashSearchType::SHA384,
116-
"sha512" => PartialHashSearchType::SHA512,
117-
x => return Err(anyhow!("unknown hash type {}", x)),
118-
};
110+
let hash_type = hash_type.try_into().map_err(|e: String| anyhow!(e))?;
111+
let response_hash = response_hash.try_into().map_err(|e: String| anyhow!(e))?;
119112
self.inner.partial_search(
120113
hash.map(|h| (hash_type, h)),
121114
file_name,
122-
Some(response_hash),
115+
response_hash,
123116
limit,
124117
)
125118
}

client/src/blocking.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::path::{Path, PathBuf};
55

66
use malwaredb_types::exec::pe32::EXE;
77

8-
use anyhow::{anyhow, bail, ensure, Context};
8+
use anyhow::{bail, ensure, Context};
99
use base64::engine::general_purpose;
1010
use base64::Engine;
1111
use fuzzyhash::FuzzyHash;
@@ -372,16 +372,13 @@ impl MdbClient {
372372
&self,
373373
partial_hash: Option<(PartialHashSearchType, String)>,
374374
name: Option<String>,
375-
response_hash: Option<&str>,
375+
response: PartialHashSearchType,
376376
limit: u32,
377377
) -> anyhow::Result<Vec<String>> {
378378
let query = SearchRequest {
379379
partial_hash,
380380
file_name: name,
381-
response: response_hash
382-
.unwrap_or("sha256")
383-
.try_into()
384-
.map_err(|e: &'static str| anyhow!(e))?,
381+
response,
385382
limit,
386383
};
387384

client/src/cli/search.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
use anyhow::{anyhow, Result};
44
use clap::Parser;
5-
use malwaredb_api::PartialHashSearchType;
65
use malwaredb_client::MdbClient;
76
use std::process::ExitCode;
87

@@ -32,26 +31,23 @@ pub struct SearchRequest {
3231
impl SearchRequest {
3332
pub async fn exec(&self, config: &MdbClient) -> Result<ExitCode> {
3433
let hash_search = if let Some(hash) = &self.hash {
35-
let hash_type = match self.hash_type.as_str() {
36-
"md5" => PartialHashSearchType::MD5,
37-
"sha1" => PartialHashSearchType::SHA1,
38-
"sha256" => PartialHashSearchType::SHA256,
39-
"sha384" => PartialHashSearchType::SHA384,
40-
"sha512" => PartialHashSearchType::SHA512,
41-
x => return Err(anyhow!("unknown hash type {}", x)),
42-
};
34+
let hash_type = self
35+
.hash_type
36+
.as_str()
37+
.try_into()
38+
.map_err(|e: String| anyhow!(e))?;
4339
Some((hash_type, hash.clone()))
4440
} else {
4541
None
4642
};
4743

44+
let response = self
45+
.response_type
46+
.as_str()
47+
.try_into()
48+
.map_err(|e: String| anyhow!(e))?;
4849
let response = config
49-
.partial_search(
50-
hash_search,
51-
self.file_name.clone(),
52-
Some(self.response_type.as_str()),
53-
self.limit,
54-
)
50+
.partial_search(hash_search, self.file_name.clone(), response, self.limit)
5551
.await?;
5652

5753
if response.is_empty() {

client/src/lib.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use std::fmt::{Debug, Formatter};
2020
use std::io::Cursor;
2121
use std::path::{Path, PathBuf};
2222

23-
use anyhow::{anyhow, bail, ensure, Context, Result};
23+
use anyhow::{bail, ensure, Context, Result};
2424
use base64::engine::general_purpose;
2525
use base64::Engine;
2626
use cart_container::JsonMap;
@@ -407,16 +407,13 @@ impl MdbClient {
407407
&self,
408408
partial_hash: Option<(PartialHashSearchType, String)>,
409409
name: Option<String>,
410-
response_hash: Option<&str>,
410+
response: PartialHashSearchType,
411411
limit: u32,
412412
) -> Result<Vec<String>> {
413413
let query = SearchRequest {
414414
partial_hash,
415415
file_name: name,
416-
response: response_hash
417-
.unwrap_or("sha256")
418-
.try_into()
419-
.map_err(|e: &'static str| anyhow!(e))?,
416+
response,
420417
limit,
421418
};
422419

crates/api/src/lib.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ impl Display for PartialHashSearchType {
464464
}
465465

466466
impl TryInto<PartialHashSearchType> for &str {
467-
type Error = &'static str;
467+
type Error = String;
468468

469469
fn try_into(self) -> Result<PartialHashSearchType, Self::Error> {
470470
match self {
@@ -474,7 +474,19 @@ impl TryInto<PartialHashSearchType> for &str {
474474
"sha256" => Ok(PartialHashSearchType::SHA256),
475475
"sha384" => Ok(PartialHashSearchType::SHA384),
476476
"sha512" => Ok(PartialHashSearchType::SHA512),
477-
_ => Err("Invalid hash type"),
477+
x => Err(format!("Invalid hash type {x}")),
478+
}
479+
}
480+
}
481+
482+
impl TryInto<PartialHashSearchType> for Option<&str> {
483+
type Error = String;
484+
485+
fn try_into(self) -> Result<PartialHashSearchType, Self::Error> {
486+
if let Some(hash) = self {
487+
hash.try_into()
488+
} else {
489+
Ok(PartialHashSearchType::SHA256)
478490
}
479491
}
480492
}

crates/server/src/http/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ mod tests {
895895
.partial_search(
896896
Some((PartialHashSearchType::Any, "AAAA".into())),
897897
None,
898-
None,
898+
PartialHashSearchType::Any,
899899
10,
900900
)
901901
.await

0 commit comments

Comments
 (0)