use chrono::naive::NaiveDateTime;
use futures::future::BoxFuture;
use std::sync::Arc;
mod retries;
pub use retries::{retry_future, Retryable};
pub mod observe;
const DATE_FMT: &str = "%Y%m%d%H%M%S";
#[derive(Clone)]
pub struct Pacer {
cdx: Arc<dyn Fn() -> BoxFuture<'static, ()> + Send + Sync>,
content: Arc<dyn Fn() -> BoxFuture<'static, ()> + Send + Sync>,
}
impl Pacer {
pub fn new<CF, CFFut, DF, DFFut>(cdx: CF, content: DF) -> Self
where
CF: Fn() -> CFFut + Send + Sync + 'static,
CFFut: futures::Future<Output = ()> + Send + 'static,
DF: Fn() -> DFFut + Send + Sync + 'static,
DFFut: futures::Future<Output = ()> + Send + 'static,
{
Self {
cdx: Arc::new(move || Box::pin(cdx())),
content: Arc::new(move || Box::pin(content())),
}
}
pub fn noop() -> Self {
Self::new(|| async {}, || async {})
}
pub async fn pace_cdx(&self) {
(self.cdx)().await
}
pub async fn pace_content(&self) {
(self.content)().await
}
}
pub fn parse_timestamp(input: &str) -> Option<NaiveDateTime> {
NaiveDateTime::parse_from_str(input, DATE_FMT).ok()
}
pub fn to_timestamp(input: &NaiveDateTime) -> String {
input.format(DATE_FMT).to_string()
}
pub mod redirect {
pub fn guess_redirect_content(url: &str) -> String {
format!(
"<html><body>You are being <a href=\"{}\">redirected</a>.</body></html>",
url
)
}
}