use crate::{Error, Result, meta};
use std::time::Duration;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Reference {
Name(String),
Uid(String),
}
impl Reference {
pub fn name(value: impl Into<String>) -> Self {
Self::Name(value.into())
}
pub fn uid(value: impl Into<String>) -> Self {
Self::Uid(value.into())
}
pub(crate) fn object(&self) -> Result<meta::ObjectReference> {
let mut r = meta::ObjectReference::default();
match self {
Self::Name(v) => {
crate::error::nonempty(v, "resource name")?;
r.name = v.clone();
}
Self::Uid(v) => {
crate::error::nonempty(v, "resource UID")?;
r.uid = v.clone();
}
}
Ok(r)
}
pub(crate) fn get(&self) -> Result<meta::GetOptions> {
let r = self.object()?;
Ok(meta::GetOptions {
name: r.name,
uid: r.uid,
})
}
pub(crate) fn delete(&self) -> Result<meta::DeleteOptions> {
let r = self.object()?;
Ok(meta::DeleteOptions {
name: r.name,
uid: r.uid,
})
}
}
impl From<String> for Reference {
fn from(v: String) -> Self {
Self::Name(v)
}
}
impl From<&str> for Reference {
fn from(v: &str) -> Self {
Self::Name(v.into())
}
}
impl From<&String> for Reference {
fn from(v: &String) -> Self {
Self::Name(v.clone())
}
}
impl From<&Reference> for Reference {
fn from(v: &Reference) -> Self {
v.clone()
}
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct Page<T> {
pub items: Vec<T>,
pub info: meta::ListResponseMeta,
}
impl<T> Page<T> {
pub(crate) fn new(items: Vec<T>, info: Option<meta::ListResponseMeta>) -> Self {
Self {
items,
info: info.unwrap_or_default(),
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum OrderBy {
#[default]
Default,
Name,
CreatedAt,
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum ListFilter {
Space(Reference),
Template(Reference),
Workspace(Reference),
}
#[derive(Clone, Copy, Debug, Default)]
pub enum SpaceMode {
#[default]
CreatedBy,
Member,
}
#[derive(Clone, Debug, Default)]
#[non_exhaustive]
pub struct ListOptions {
pub page: u32,
pub page_size: u32,
pub order_by: OrderBy,
pub descending: bool,
pub filter: Option<ListFilter>,
pub space_mode: SpaceMode,
}
impl ListOptions {
pub fn new() -> Self {
Self::default()
}
pub fn page_size(mut self, n: u32) -> Self {
self.page_size = n;
self
}
pub fn page(mut self, n: u32) -> Self {
self.page = n;
self
}
pub fn in_space(mut self, r: impl Into<Reference>) -> Self {
self.filter = Some(ListFilter::Space(r.into()));
self
}
pub fn of_template(mut self, r: impl Into<Reference>) -> Self {
self.filter = Some(ListFilter::Template(r.into()));
self
}
pub fn of_workspace(mut self, r: impl Into<Reference>) -> Self {
self.filter = Some(ListFilter::Workspace(r.into()));
self
}
pub fn order(mut self, key: OrderBy, descending: bool) -> Self {
self.order_by = key;
self.descending = descending;
self
}
pub fn member_spaces(mut self) -> Self {
self.space_mode = SpaceMode::Member;
self
}
pub(crate) fn common(&self) -> meta::CommonListOptions {
meta::CommonListOptions {
page: self.page,
items_per_page: self.page_size,
order_by: if self.order_by == OrderBy::Default && !self.descending {
None
} else {
Some(meta::common_list_options::OrderBy {
r#type: match self.order_by {
OrderBy::Default => 0,
OrderBy::Name => 1,
OrderBy::CreatedAt => 2,
},
mode: if self.descending { 2 } else { 1 },
})
},
}
}
pub(crate) fn space(&self) -> Result<Option<meta::ObjectReference>> {
match &self.filter {
None => Ok(None),
Some(ListFilter::Space(r)) => Ok(Some(r.object()?)),
_ => Err(Error::InvalidArgument(
"this collection only supports Space filters".into(),
)),
}
}
pub(crate) fn unfiltered(&self) -> Result<()> {
if self.filter.is_some() {
return Err(Error::InvalidArgument(
"this collection does not support filters".into(),
));
}
Ok(())
}
pub(crate) fn next(&mut self, info: meta::ListResponseMeta, empty: bool) -> Result<bool> {
if !info.has_more {
return Ok(false);
}
if empty || info.page < self.page {
return Err(Error::Protocol("pagination did not advance".into()));
}
self.page = info
.page
.checked_add(1)
.ok_or_else(|| Error::Protocol("page index overflow".into()))?;
Ok(true)
}
}
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct WaitOptions {
pub timeout: Option<Duration>,
pub poll_interval: Duration,
}
impl Default for WaitOptions {
fn default() -> Self {
Self {
timeout: Some(Duration::from_secs(300)),
poll_interval: Duration::from_millis(500),
}
}
}
impl WaitOptions {
pub fn timeout(mut self, t: Option<Duration>) -> Self {
self.timeout = t;
self
}
pub fn poll_interval(mut self, t: Duration) -> Self {
self.poll_interval = t;
self
}
pub(crate) fn validate(self) -> Result<Self> {
if self.poll_interval.is_zero() || self.timeout.is_some_and(|t| t.is_zero()) {
return Err(Error::InvalidArgument(
"timeouts and polling intervals must be positive".into(),
));
}
Ok(self)
}
}