use std::future::Future;
use crate::action::ChatActionGuard;
use crate::context::Context;
#[cfg(not(target_arch = "wasm32"))]
pub trait FromContextBounds: Send {}
#[cfg(not(target_arch = "wasm32"))]
impl<T: Send> FromContextBounds for T {}
#[cfg(target_arch = "wasm32")]
pub trait FromContextBounds {}
#[cfg(target_arch = "wasm32")]
impl<T> FromContextBounds for T {}
#[cfg(not(target_arch = "wasm32"))]
pub trait ContextFutureBounds: Future + Send {}
#[cfg(not(target_arch = "wasm32"))]
impl<T: Future + Send + ?Sized> ContextFutureBounds for T {}
#[cfg(target_arch = "wasm32")]
pub trait ContextFutureBounds: Future {}
#[cfg(target_arch = "wasm32")]
impl<T: Future + ?Sized> ContextFutureBounds for T {}
pub trait FromContext: Sized + FromContextBounds {
fn from_context(ctx: &Context) -> impl ContextFutureBounds<Output = Self>;
}
impl FromContext for Context {
async fn from_context(ctx: &Context) -> Self {
ctx.clone()
}
}
#[derive(Debug, Clone)]
pub struct User {
pub id: String,
pub name: String,
}
impl FromContext for User {
async fn from_context(ctx: &Context) -> Self {
Self {
id: ctx.user_id().to_string(),
name: ctx.user_name().to_string(),
}
}
}
#[derive(Debug, Clone)]
pub struct Channel {
pub id: String,
}
impl FromContext for Channel {
async fn from_context(ctx: &Context) -> Self {
Self {
id: ctx.channel_id().to_string(),
}
}
}
#[derive(Debug, Clone)]
pub struct CommandName(pub String);
impl FromContext for CommandName {
async fn from_context(ctx: &Context) -> Self {
Self(ctx.command_name().unwrap_or_default().to_string())
}
}
#[derive(Debug, Clone)]
pub struct CommandArgs(pub String);
impl FromContext for CommandArgs {
async fn from_context(ctx: &Context) -> Self {
Self(ctx.command_args().unwrap_or_default().to_string())
}
}
#[derive(Debug, Clone)]
pub struct ButtonId(pub String);
impl FromContext for ButtonId {
async fn from_context(ctx: &Context) -> Self {
Self(ctx.button_id().unwrap_or_default().to_string())
}
}
#[derive(Debug, Clone)]
pub struct MessageContent(pub String);
impl FromContext for MessageContent {
async fn from_context(ctx: &Context) -> Self {
Self(ctx.message_content().unwrap_or_default().to_string())
}
}
impl FromContext for () {
async fn from_context(_ctx: &Context) -> Self {}
}
impl<T1: FromContext> FromContext for (T1,) {
async fn from_context(ctx: &Context) -> Self {
(T1::from_context(ctx).await,)
}
}
impl<T1: FromContext, T2: FromContext> FromContext for (T1, T2) {
async fn from_context(ctx: &Context) -> Self {
let t1 = T1::from_context(ctx).await;
let t2 = T2::from_context(ctx).await;
(t1, t2)
}
}
impl<T1: FromContext, T2: FromContext, T3: FromContext> FromContext for (T1, T2, T3) {
async fn from_context(ctx: &Context) -> Self {
let t1 = T1::from_context(ctx).await;
let t2 = T2::from_context(ctx).await;
let t3 = T3::from_context(ctx).await;
(t1, t2, t3)
}
}
impl<T1: FromContext, T2: FromContext, T3: FromContext, T4: FromContext> FromContext
for (T1, T2, T3, T4)
{
async fn from_context(ctx: &Context) -> Self {
let t1 = T1::from_context(ctx).await;
let t2 = T2::from_context(ctx).await;
let t3 = T3::from_context(ctx).await;
let t4 = T4::from_context(ctx).await;
(t1, t2, t3, t4)
}
}
pub struct Typing(pub Option<ChatActionGuard>);
impl FromContext for Typing {
async fn from_context(ctx: &Context) -> Self {
Self(ctx.typing())
}
}