use std::ops::Index;
use serde::{Deserialize, Serialize};
#[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>,
}
impl Default for ListOptions {
fn default() -> Self {
Self {
list: std::marker::PhantomData::<TimeNotSpecified>,
limit: None,
before_id: None,
after_id: None,
}
}
}
impl<T> ListOptions<T> {
#[inline]
pub const fn with_limit(mut self, limit: u8) -> Self {
self.limit = Some(limit);
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,
}
}
#[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()),
}
}
}
#[must_use]
#[derive(Debug, Clone, 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()
}
}