use serde::{Deserialize, Serialize};
use std::collections::HashMap;
pub type Extras = serde_json::Map<String, serde_json::Value>;
pub type HashAlgos = HashMap<String, String>;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct FileQuery {
pub text: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub hashes: Option<HashAlgos>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub mimetypes: Option<Vec<String>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub extensions: Option<Vec<String>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub filename: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub size_min: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub size_max: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub published_after: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub published_before: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub lang: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub limit: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub offset: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub extras: Option<Extras>,
}
impl FileQuery {
pub fn new(text: impl Into<String>) -> Self {
Self {
text: text.into(),
hashes: None,
mimetypes: None,
extensions: None,
filename: None,
size_min: None,
size_max: None,
published_after: None,
published_before: None,
lang: None,
limit: None,
offset: None,
extras: None,
}
}
pub fn with_hashes(mut self, hashes: HashAlgos) -> Self {
self.hashes = (!hashes.is_empty()).then_some(hashes);
self
}
pub fn with_hash(mut self, algorithm: impl Into<String>, value: impl Into<String>) -> Self {
self.hashes
.get_or_insert_with(HashMap::new)
.insert(algorithm.into(), value.into());
self
}
pub fn with_mimetypes<I, S>(mut self, mimetypes: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
let mimetypes: Vec<String> = mimetypes.into_iter().map(Into::into).collect();
self.mimetypes = (!mimetypes.is_empty()).then_some(mimetypes);
self
}
pub fn with_mimetype(mut self, mimetype: impl Into<String>) -> Self {
self.mimetypes
.get_or_insert_with(Vec::new)
.push(mimetype.into());
self
}
pub fn with_extensions<I, S>(mut self, extensions: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
let extensions: Vec<String> = extensions
.into_iter()
.map(|extension| normalize_extension(extension.into()))
.collect();
self.extensions = (!extensions.is_empty()).then_some(extensions);
self
}
pub fn with_extension(mut self, extension: impl Into<String>) -> Self {
self.extensions
.get_or_insert_with(Vec::new)
.push(normalize_extension(extension.into()));
self
}
pub fn with_filename(mut self, filename: impl Into<String>) -> Self {
self.filename = Some(filename.into());
self
}
pub fn with_size_range(mut self, min: u64, max: u64) -> Self {
self.size_min = Some(min);
self.size_max = Some(max);
self
}
pub fn with_size_min(mut self, min: u64) -> Self {
self.size_min = Some(min);
self
}
pub fn with_size_max(mut self, max: u64) -> Self {
self.size_max = Some(max);
self
}
pub fn with_published_after(mut self, instant: impl Into<String>) -> Self {
self.published_after = Some(instant.into());
self
}
pub fn with_published_before(mut self, instant: impl Into<String>) -> Self {
self.published_before = Some(instant.into());
self
}
pub fn with_lang(mut self, lang: impl Into<String>) -> Self {
self.lang = Some(lang.into());
self
}
pub fn with_limit(mut self, limit: u32) -> Self {
self.limit = Some(limit);
self
}
pub fn with_offset(mut self, offset: u32) -> Self {
self.offset = Some(offset);
self
}
pub fn with_extras(mut self, extras: Extras) -> Self {
self.extras = (!extras.is_empty()).then_some(extras);
self
}
pub fn with_extra(
mut self,
key: impl Into<String>,
value: impl Into<serde_json::Value>,
) -> Self {
self.extras
.get_or_insert_with(Extras::new)
.insert(key.into(), value.into());
self
}
}
fn normalize_extension(extension: String) -> String {
extension
.trim()
.trim_start_matches('.')
.to_ascii_lowercase()
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Address {
pub scheme: String,
pub uri: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub hashes: Option<HashAlgos>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub size: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub extras: Option<Extras>,
}
impl Address {
pub fn new(scheme: impl Into<String>, uri: impl Into<String>) -> Self {
Self {
scheme: scheme.into(),
uri: uri.into(),
hashes: None,
size: None,
extras: None,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct FileResult {
#[serde(default)]
pub provider: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
pub addresses: Vec<Address>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub size: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub hashes: Option<HashAlgos>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub mimetype: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub lang: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub published: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub last_seen: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub score: Option<f64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub raw: Option<serde_json::Value>,
}
impl FileResult {
pub fn new(provider: impl Into<String>, addresses: Vec<Address>) -> Self {
Self {
provider: provider.into(),
title: None,
addresses,
size: None,
hashes: None,
mimetype: None,
lang: None,
published: None,
last_seen: None,
score: None,
raw: None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct ErrorCode(pub String);
impl ErrorCode {
pub fn new(code: impl Into<String>) -> Self {
Self(code.into())
}
pub fn timeout() -> Self {
Self("timeout".into())
}
pub fn unsupported() -> Self {
Self("unsupported".into())
}
pub fn refused() -> Self {
Self("refused".into())
}
pub fn transport() -> Self {
Self("transport".into())
}
pub fn malformed() -> Self {
Self("malformed".into())
}
pub fn cancelled() -> Self {
Self("cancelled".into())
}
pub fn internal() -> Self {
Self("internal".into())
}
}
impl std::fmt::Display for ErrorCode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
impl From<&str> for ErrorCode {
fn from(value: &str) -> Self {
Self(value.to_owned())
}
}
impl From<String> for ErrorCode {
fn from(value: String) -> Self {
Self(value)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ErrorInfo {
pub code: ErrorCode,
pub message: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Failure {
pub provider: String,
pub error: ErrorInfo,
}
#[allow(clippy::large_enum_variant)] #[derive(Debug, Clone, PartialEq)]
pub enum Outcome {
Result(FileResult),
Failure(Failure),
}
impl Outcome {
pub fn provider(&self) -> &str {
match self {
Outcome::Result(result) => &result.provider,
Outcome::Failure(failure) => &failure.provider,
}
}
pub fn as_result(&self) -> Option<&FileResult> {
match self {
Outcome::Result(result) => Some(result),
Outcome::Failure(_) => None,
}
}
pub fn as_failure(&self) -> Option<&Failure> {
match self {
Outcome::Result(_) => None,
Outcome::Failure(failure) => Some(failure),
}
}
}
pub trait Validate {
fn validate(&self) -> Result<(), ValidationError>;
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum ValidationError {
#[error("field `{0}` must not be empty")]
Empty(&'static str),
#[error("`addresses` must not be empty")]
NoAddresses,
#[error("size_min ({min}) must not exceed size_max ({max})")]
SizeRange {
min: u64,
max: u64,
},
}
impl Validate for FileQuery {
fn validate(&self) -> Result<(), ValidationError> {
if let (Some(min), Some(max)) = (self.size_min, self.size_max) {
if min > max {
return Err(ValidationError::SizeRange { min, max });
}
}
if let Some(mimetypes) = &self.mimetypes {
if mimetypes.iter().any(|mimetype| mimetype.trim().is_empty()) {
return Err(ValidationError::Empty("mimetypes entry"));
}
}
if let Some(extensions) = &self.extensions {
if extensions
.iter()
.any(|extension| extension.trim().is_empty())
{
return Err(ValidationError::Empty("extensions entry"));
}
}
if let Some(filename) = &self.filename {
if filename.trim().is_empty() {
return Err(ValidationError::Empty("filename"));
}
}
if let Some(lang) = &self.lang {
if lang.trim().is_empty() {
return Err(ValidationError::Empty("lang"));
}
}
Ok(())
}
}
impl Validate for Address {
fn validate(&self) -> Result<(), ValidationError> {
if self.scheme.is_empty() {
return Err(ValidationError::Empty("scheme"));
}
if self.uri.is_empty() {
return Err(ValidationError::Empty("uri"));
}
Ok(())
}
}
impl Validate for FileResult {
fn validate(&self) -> Result<(), ValidationError> {
if self.provider.is_empty() {
return Err(ValidationError::Empty("provider"));
}
if self.addresses.is_empty() {
return Err(ValidationError::NoAddresses);
}
for address in &self.addresses {
address.validate()?;
}
Ok(())
}
}
impl Validate for Failure {
fn validate(&self) -> Result<(), ValidationError> {
if self.provider.is_empty() {
return Err(ValidationError::Empty("provider"));
}
Ok(())
}
}
#[cfg(test)]
mod builder_tests {
use super::*;
#[test]
fn with_helpers_set_every_selector() {
let query = FileQuery::new("annual report")
.with_hash("sha256", "abc")
.with_hash("infohash", "def")
.with_mimetype("image/*")
.with_mimetypes(["audio/*", "video/*"])
.with_extension("JPG")
.with_extensions(["epub", ".TXT"])
.with_filename("*report*")
.with_size_range(10, 99)
.with_published_after("2020-01-01T00:00:00Z")
.with_published_before("2021-01-01T00:00:00Z")
.with_lang("en")
.with_offset(10)
.with_limit(5)
.with_extra("lab.foo", "bar")
.with_extra("lab.count", 3);
assert_eq!(query.text, "annual report");
let hashes = query.hashes.as_ref().unwrap();
assert_eq!(hashes["sha256"], "abc");
assert_eq!(hashes["infohash"], "def");
assert_eq!(
query.mimetypes.as_ref().unwrap(),
&vec!["audio/*".to_owned(), "video/*".to_owned()]
);
assert_eq!(
query.extensions.as_ref().unwrap(),
&vec!["epub".to_owned(), "txt".to_owned()]
);
assert_eq!(query.filename.as_deref(), Some("*report*"));
assert_eq!(query.size_min, Some(10));
assert_eq!(query.size_max, Some(99));
assert_eq!(
query.published_after.as_deref(),
Some("2020-01-01T00:00:00Z")
);
assert_eq!(
query.published_before.as_deref(),
Some("2021-01-01T00:00:00Z")
);
assert_eq!(query.lang.as_deref(), Some("en"));
assert_eq!(query.limit, Some(5));
assert_eq!(query.offset, Some(10));
let extras = query.extras.as_ref().unwrap();
assert_eq!(extras["lab.foo"], serde_json::json!("bar"));
assert_eq!(extras["lab.count"], serde_json::json!(3));
query.validate().unwrap();
}
#[test]
fn empty_collections_clear_fields() {
let query = FileQuery::new("x")
.with_hashes(HashAlgos::new())
.with_mimetypes(Vec::<String>::new())
.with_extensions(Vec::<String>::new())
.with_extras(Extras::new());
assert!(query.hashes.is_none());
assert!(query.mimetypes.is_none());
assert!(query.extensions.is_none());
assert!(query.extras.is_none());
}
#[test]
fn extension_helpers_normalize() {
let query = FileQuery::new("x").with_extension(" .TaR.Gz ");
assert_eq!(
query.extensions.as_deref(),
Some(["tar.gz".to_owned()].as_slice())
);
}
#[test]
fn size_bounds_are_independent() {
let query = FileQuery::new("x").with_size_min(1).with_size_max(2);
assert_eq!(query.size_min, Some(1));
assert_eq!(query.size_max, Some(2));
}
}