use std::fmt;
use std::time::Duration;
use chrono::{DateTime, SecondsFormat, Utc};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct RequestOptions {
pub max_retries: Option<u32>,
pub timeout: Option<Duration>,
}
impl RequestOptions {
pub fn new() -> Self {
Self::default()
}
pub fn max_retries(mut self, value: u32) -> Self {
self.max_retries = Some(value);
self
}
pub fn timeout(mut self, value: Duration) -> Self {
self.timeout = Some(value);
self
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Freshness {
#[serde(rename = "live")]
Live,
#[serde(rename = "24h")]
TwentyFourHours,
#[serde(rename = "7d")]
SevenDays,
#[serde(rename = "30d")]
ThirtyDays,
}
impl Freshness {
pub fn as_str(self) -> &'static str {
match self {
Self::Live => "live",
Self::TwentyFourHours => "24h",
Self::SevenDays => "7d",
Self::ThirtyDays => "30d",
}
}
}
impl fmt::Display for Freshness {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum Platform {
Instagram,
#[serde(rename = "tiktok")]
TikTok,
Twitter,
Reddit,
#[serde(rename = "youtube")]
YouTube,
#[serde(other)]
Unknown,
}
impl Platform {
pub fn as_str(self) -> &'static str {
match self {
Self::Instagram => "instagram",
Self::TikTok => "tiktok",
Self::Twitter => "twitter",
Self::Reddit => "reddit",
Self::YouTube => "youtube",
Self::Unknown => "unknown",
}
}
}
impl fmt::Display for Platform {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum Source {
Live,
Cache,
#[serde(other)]
Unknown,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct FetchOptions {
pub freshness: Option<Freshness>,
pub request_options: RequestOptions,
}
impl FetchOptions {
pub fn new() -> Self {
Self::default()
}
pub fn freshness(mut self, value: Freshness) -> Self {
self.freshness = Some(value);
self
}
pub fn max_retries(mut self, value: u32) -> Self {
self.request_options.max_retries = Some(value);
self
}
pub fn timeout(mut self, value: Duration) -> Self {
self.request_options.timeout = Some(value);
self
}
}
pub(crate) trait QueryValue {
fn encode(&self) -> Option<String>;
}
impl QueryValue for String {
fn encode(&self) -> Option<String> {
(!self.is_empty()).then(|| self.clone())
}
}
impl QueryValue for i64 {
fn encode(&self) -> Option<String> {
Some(self.to_string())
}
}
impl QueryValue for bool {
fn encode(&self) -> Option<String> {
Some(self.to_string())
}
}
impl QueryValue for Freshness {
fn encode(&self) -> Option<String> {
Some(self.as_str().to_owned())
}
}
impl QueryValue for Platform {
fn encode(&self) -> Option<String> {
Some(self.as_str().to_owned())
}
}
impl QueryValue for DateTime<Utc> {
fn encode(&self) -> Option<String> {
Some(self.to_rfc3339_opts(SecondsFormat::Secs, true))
}
}
impl<T: QueryValue> QueryValue for Option<T> {
fn encode(&self) -> Option<String> {
self.as_ref().and_then(QueryValue::encode)
}
}
#[derive(Clone, Debug, Default)]
pub(crate) struct Query(pub(crate) Vec<(&'static str, String)>);
impl Query {
pub(crate) fn push(&mut self, name: &'static str, value: &impl QueryValue) {
if let Some(encoded) = value.encode() {
self.0.push((name, encoded));
}
}
pub(crate) fn require(
&mut self,
name: &'static str,
value: &impl QueryValue,
) -> Result<(), crate::Error> {
match value.encode() {
Some(encoded) => {
self.0.push((name, encoded));
Ok(())
}
None => Err(crate::Error::invalid_argument(format!(
"option {name} is required"
))),
}
}
pub(crate) fn set_cursor(&mut self, cursor: &str) {
self.0.retain(|(name, _)| *name != "cursor");
self.0.push(("cursor", cursor.to_owned()));
}
}