use serde::Serialize;
use crate::FORMAT;
use crate::request::Id;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum RequestError {
InvalidRequest,
InvalidOptions,
NoSource,
ReadFailed,
TooLarge,
DecodeFailed,
Internal,
DownloadFailed,
UrlExpired,
HostNotAllowed,
}
impl RequestError {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::InvalidRequest => "invalid_request",
Self::InvalidOptions => "invalid_options",
Self::NoSource => "no_source",
Self::ReadFailed => "read_failed",
Self::TooLarge => "too_large",
Self::DecodeFailed => "decode_failed",
Self::Internal => "internal",
Self::DownloadFailed => "download_failed",
Self::UrlExpired => "url_expired",
Self::HostNotAllowed => "host_not_allowed",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum DefinitionError {
UnsupportedDefinition,
WrongSource,
InvalidParameter,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Status {
Success,
Error,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct SourceFacts {
pub bytes: u64,
pub sha1: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct DecodeFacts {
pub format: &'static str,
pub frames: u32,
pub orientation_tag: Option<u16>,
pub orientation_applied: bool,
pub truncated: Option<bool>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Runner {
pub name: &'static str,
pub version: &'static str,
pub hashes: &'static str,
pub decoder: &'static str,
}
pub const RUNNER: Runner = Runner {
name: "legit-hasher",
version: env!("CARGO_PKG_VERSION"),
hashes: "0.2.1",
decoder: "image 0.25.10",
};
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Timings {
#[serde(skip_serializing_if = "Option::is_none")]
pub read_us: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub decode_us: Option<u64>,
pub total_us: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct HashOutcome {
pub id: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub shingle: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tokens: Option<&'static str>,
#[serde(skip_serializing_if = "Option::is_none")]
pub value: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub quality: Option<u8>,
#[serde(skip_serializing_if = "Option::is_none")]
pub empty: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub chunk_keys: Option<Vec<i64>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub band_keys: Option<Vec<i64>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error_type: Option<DefinitionError>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub time_us: Option<u64>,
}
impl HashOutcome {
pub(crate) fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
shingle: None,
tokens: None,
value: None,
quality: None,
empty: None,
chunk_keys: None,
band_keys: None,
error_type: None,
error: None,
time_us: None,
}
}
pub(crate) fn failed(id: impl Into<String>, code: DefinitionError, message: impl Into<String>) -> Self {
Self {
error_type: Some(code),
error: Some(message.into()),
..Self::new(id)
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Outcome {
pub format: u32,
pub id: Option<Id>,
#[serde(skip_serializing_if = "Option::is_none")]
pub line: Option<u64>,
pub status: Status,
#[serde(skip_serializing_if = "Option::is_none")]
pub error_type: Option<RequestError>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub detected_format: Option<&'static str>,
#[serde(skip_serializing_if = "Option::is_none")]
pub width: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub height: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub source: Option<SourceFacts>,
#[serde(skip_serializing_if = "Option::is_none")]
pub decode: Option<DecodeFacts>,
pub runner: Runner,
#[serde(skip_serializing_if = "Option::is_none")]
pub hashes: Option<Vec<HashOutcome>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub timings: Option<Timings>,
}
impl Outcome {
pub(crate) const fn success(id: Option<Id>) -> Self {
Self {
format: FORMAT,
id,
line: None,
status: Status::Success,
error_type: None,
error: None,
detected_format: None,
width: None,
height: None,
source: None,
decode: None,
runner: RUNNER,
hashes: Some(Vec::new()),
timings: None,
}
}
#[must_use]
pub fn error(id: Option<Id>, code: RequestError, message: impl Into<String>) -> Self {
Self {
status: Status::Error,
error_type: Some(code),
error: Some(message.into()),
hashes: None,
..Self::success(id)
}
}
#[must_use]
pub fn is_failure(&self) -> bool {
self.status == Status::Error || self.hashes.iter().flatten().any(|hash| hash.error_type.is_some())
}
#[must_use]
pub fn to_json(&self) -> String {
serde_json::to_string(self).unwrap_or_else(|error| {
serde_json::json!({
"format": FORMAT,
"id": null,
"status": "error",
"error_type": "internal",
"error": error.to_string(),
})
.to_string()
})
}
}