use crate::rate_limit::{RateLimit, WallClockTimestamp};
use crate::retry::MonotonicDuration;
use super::{ProgressChange, ProgressObservation};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PollControl {
Continue,
Cancel,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct ProviderTimeObservation {
observed_at: Option<WallClockTimestamp>,
expires_at: Option<WallClockTimestamp>,
}
impl ProviderTimeObservation {
pub const fn new(
observed_at: Option<WallClockTimestamp>,
expires_at: Option<WallClockTimestamp>,
) -> Result<Self, ProviderTimeError> {
if let (Some(observed), Some(expires)) = (observed_at, expires_at)
&& expires.get() < observed.get()
{
return Err(ProviderTimeError::ExpiryBeforeObservation);
}
Ok(Self {
observed_at,
expires_at,
})
}
#[must_use]
pub const fn observed_at(self) -> Option<WallClockTimestamp> {
self.observed_at
}
#[must_use]
pub const fn expires_at(self) -> Option<WallClockTimestamp> {
self.expires_at
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ProviderTimeError {
ExpiryBeforeObservation,
}
impl_static_error!(ProviderTimeError,
Self::ExpiryBeforeObservation => "provider expiry precedes its observation timestamp",
);
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct PollContext {
pub(crate) observation: u32,
pub(crate) progress: ProgressObservation,
pub(crate) progress_change: ProgressChange,
pub(crate) rate_limit: Option<RateLimit>,
pub(crate) provider_time: ProviderTimeObservation,
}
impl PollContext {
#[must_use]
pub const fn observation(self) -> u32 {
self.observation
}
#[must_use]
pub const fn progress(self) -> ProgressObservation {
self.progress
}
#[must_use]
pub const fn progress_change(self) -> ProgressChange {
self.progress_change
}
#[must_use]
pub const fn rate_limit(self) -> Option<RateLimit> {
self.rate_limit
}
#[must_use]
pub const fn provider_time(self) -> ProviderTimeObservation {
self.provider_time
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PollRequestStep {
Request,
Delay(MonotonicDuration),
Cancelled,
TimedOut,
}