use std::{collections::HashMap, ops::Index};
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, Copy)]
pub struct ListBefore {}
#[derive(Debug, Clone, Copy)]
pub struct ListAfter {}
#[derive(Debug, Clone, Copy)]
pub struct TimeNotSpecified {}
#[must_use]
#[derive(Debug, Clone, Serialize)]
pub struct ListOptions<List = TimeNotSpecified> {
#[serde(skip)]
list: std::marker::PhantomData<List>,
#[serde(skip_serializing_if = "Option::is_none")]
limit: Option<u8>,
#[serde(rename = "before")]
before_id: Option<String>,
#[serde(rename = "after")]
after_id: Option<String>,
#[serde(flatten)]
other: Option<HashMap<String, Value>>,
}
impl Default for ListOptions {
fn default() -> Self {
Self {
list: std::marker::PhantomData::<TimeNotSpecified>,
limit: None,
before_id: None,
after_id: None,
other: None,
}
}
}
impl<T> ListOptions<T> {
#[inline]
pub const fn with_limit(mut self, limit: u8) -> Self {
self.limit = Some(limit);
self
}
pub fn with_other(mut self, key: &str, value: Value) -> Self {
let other = self.other.get_or_insert_with(HashMap::new);
let _old = other.insert(key.to_owned(), value);
self
}
}
impl ListOptions<TimeNotSpecified> {
#[inline]
pub fn list_before(self, id: &str) -> ListOptions<ListBefore> {
ListOptions::<ListBefore> {
list: std::marker::PhantomData,
limit: self.limit,
before_id: Some(id.to_string()),
after_id: None,
other: self.other,
}
}
#[inline]
pub fn list_after(self, id: &str) -> ListOptions<ListAfter> {
ListOptions::<ListAfter> {
list: std::marker::PhantomData,
limit: self.limit,
before_id: None,
after_id: Some(id.to_string()),
other: self.other,
}
}
}
#[must_use]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ListResponse<T> {
pub has_more: bool,
pub data: Vec<T>,
}
impl<T> Index<usize> for ListResponse<T> {
type Output = T;
fn index(&self, index: usize) -> &Self::Output {
#[allow(clippy::indexing_slicing)]
&self.data[index]
}
}
impl<T> ListResponse<T> {
#[inline]
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
#[inline]
pub fn len(&self) -> usize {
self.data.len()
}
}