use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use std::borrow::Cow;
use std::fmt;
use std::time::Duration;
use std::time::SystemTime;
mod complex_field_types;
mod reserved_events;
mod reserved_fields;
pub use complex_field_types::*;
pub use reserved_events::*;
pub use reserved_fields::*;
use crate::common::{abuse_type_serialize, deserialize_ms, serialize_ms, AbuseType};
#[derive(Debug, Serialize, Deserialize)]
pub struct Micros(u64);
impl Micros {
pub fn from_base_units(base_units: u64) -> Self {
Micros(base_units * 10_000)
}
pub fn from_raw(raw: u64) -> Self {
Micros(raw)
}
}
#[derive(Debug, Default)]
pub struct EventOptions {
pub return_score: Option<bool>,
pub abuse_types: Option<Vec<AbuseType>>,
pub return_action: Option<bool>,
pub return_workflow_status: Option<bool>,
pub timeout: Option<Duration>,
pub api_key: Option<String>,
pub version: Option<ApiVersion>,
pub path: Option<Cow<'static, str>>,
}
#[skip_serializing_none]
#[derive(Debug, Serialize)]
pub(crate) struct EventQueryParams {
pub(crate) return_score: Option<bool>,
#[serde(serialize_with = "abuse_type_serialize")]
pub(crate) abuse_types: Option<Vec<AbuseType>>,
pub(crate) return_action: Option<bool>,
pub(crate) return_workflow_status: Option<bool>,
}
impl From<EventOptions> for EventQueryParams {
fn from(options: EventOptions) -> Self {
EventQueryParams {
return_score: options.return_score,
abuse_types: options.abuse_types,
return_action: options.return_action,
return_workflow_status: options.return_workflow_status,
}
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct EventResponse {
pub(crate) status: i32,
pub(crate) error_message: String,
pub(crate) score_response: Option<ScoreResponse>,
}
#[skip_serializing_none]
#[derive(Debug, Serialize, Deserialize)]
pub struct ScoreResponse {
pub status: i32,
pub error_message: String,
pub scores: Option<Scores>,
pub entity_id: Option<String>,
pub entity_type: Option<String>,
pub latest_labels: Option<LatestLabels>,
pub latest_decisions: Option<serde_json::Value>,
}
#[skip_serializing_none]
#[derive(Debug, Serialize, Deserialize)]
pub struct Scores {
pub payment_abuse: Option<AbuseScore>,
pub promotion_abuse: Option<AbuseScore>,
pub account_abuse: Option<AbuseScore>,
pub account_takeover: Option<AbuseScore>,
pub content_abuse: Option<AbuseScore>,
}
#[skip_serializing_none]
#[derive(Debug, Serialize, Deserialize)]
pub struct AbuseScore {
pub score: f32,
#[serde(default)]
pub reasons: Vec<AbuseScoreReason>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct AbuseScoreReason {
pub name: String,
pub value: Option<String>,
pub details: Option<serde_json::Value>,
}
#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LatestLabels {
pub payment_abuse: Option<Label>,
pub promotion_abuse: Option<Label>,
pub account_abuse: Option<Label>,
pub account_takeover: Option<Label>,
pub content_abuse: Option<Label>,
}
#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Label {
is_bad: bool,
#[serde(serialize_with = "serialize_ms", deserialize_with = "deserialize_ms")]
time: SystemTime,
description: Option<String>,
}
#[derive(Copy, Clone, Debug)]
pub enum ApiVersion {
V205,
}
impl fmt::Display for ApiVersion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ApiVersion::V205 => write!(f, "v205"),
}
}
}