serenity_rich_interaction/menu/
page.rs

1use crate::error::Result;
2use serenity::builder::CreateMessage;
3use std::future::Future;
4use std::pin::Pin;
5use std::sync::Arc;
6
7pub type MessageBuildOutput<'b> =
8    Pin<Box<dyn Future<Output = Result<CreateMessage<'b>>> + Send + 'b>>;
9pub type MessageBuilderFn<'b> = Arc<dyn Fn() -> MessageBuildOutput<'b> + Send + Sync>;
10
11#[derive(Clone)]
12/// A page that stores a builder function for message pages
13/// or static pages
14pub enum Page<'b> {
15    Builder(MessageBuilderFn<'b>),
16    Static(CreateMessage<'b>),
17}
18
19impl<'b> Page<'b> {
20    /// Creates a new page with the given builder function that creates a page
21    /// each time it is accessed
22    pub fn new_builder<F: 'static>(builder_fn: F) -> Self
23    where
24        F: Fn() -> MessageBuildOutput<'b> + Send + Sync,
25    {
26        Self::Builder(Arc::new(builder_fn))
27    }
28
29    /// Creates a new page with a static message
30    pub fn new_static(page: CreateMessage<'b>) -> Self {
31        Self::Static(page)
32    }
33
34    /// Returns the CreateMessage of the page
35    pub async fn get(&self) -> Result<CreateMessage<'b>> {
36        match self {
37            Page::Builder(b) => b().await,
38            Page::Static(inner) => Ok(inner.clone()),
39        }
40    }
41}