mod ctx;
mod state;
use crate::RequestBuilder;
use std::{future::Future, sync::Arc};
pub use self::{ctx::Ctx, state::State};
pub type Update<T = serde_json::Value> = crate::responses::LongPollResponse<T>;
impl<T> Update<T> {
pub fn into_parts(self) -> Parts {
Parts {
vk: self.ts.unwrap(),
}
}
}
mod private {
#[derive(Debug, Clone, Copy)]
pub enum ViaParts {}
#[derive(Debug, Clone, Copy)]
pub enum ViaUpdate {}
}
pub trait FromUpdate<S, M = private::ViaUpdate>: Sized {
fn from_update(
update: Update,
state: &S,
request: Arc<RequestBuilder>,
) -> impl Future<Output = Result<Self, ()>> + Send;
}
#[derive(Clone)]
pub struct Parts {
pub vk: String,
}
pub trait FromUpdateParts<S>: Sized {
fn from_update_parts(
parts: &mut Parts,
state: &S,
request: Arc<RequestBuilder>,
) -> impl Future<Output = Result<Self, ()>> + Send;
}
impl<S, T> FromUpdate<S, private::ViaParts> for T
where
S: Send + Sync,
T: FromUpdateParts<S>,
{
async fn from_update(
update: Update,
state: &S,
request: Arc<RequestBuilder>,
) -> Result<Self, ()> {
let mut parts = update.into_parts();
Self::from_update_parts(&mut parts, state, request).await
}
}