Skip to main content

rune_framework/
handler.rs

1//! Handler traits and registration types.
2
3use std::future::Future;
4use std::pin::Pin;
5use std::sync::Arc;
6
7use bytes::Bytes;
8
9use crate::config::{FileAttachment, RuneConfig};
10use crate::context::RuneContext;
11use crate::error::SdkResult;
12use crate::stream::StreamSender;
13
14// ---------------------------------------------------------------------------
15// Handler type aliases
16// ---------------------------------------------------------------------------
17
18/// Boxed future returned by handlers.
19pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
20
21/// A unary handler function: `(ctx, input) -> Result<Bytes>`.
22pub type RuneHandlerFn =
23    Arc<dyn Fn(RuneContext, Bytes) -> BoxFuture<'static, SdkResult<Bytes>> + Send + Sync>;
24
25/// A unary handler that also accepts files: `(ctx, input, files) -> Result<Bytes>`.
26pub type RuneHandlerWithFilesFn = Arc<
27    dyn Fn(RuneContext, Bytes, Vec<FileAttachment>) -> BoxFuture<'static, SdkResult<Bytes>>
28        + Send
29        + Sync,
30>;
31
32/// A stream handler function: `(ctx, input, stream) -> Result<()>`.
33pub type StreamRuneHandlerFn = Arc<
34    dyn Fn(RuneContext, Bytes, StreamSender) -> BoxFuture<'static, SdkResult<()>> + Send + Sync,
35>;
36
37/// A stream handler that also accepts files: `(ctx, input, files, stream) -> Result<()>`.
38pub type StreamRuneHandlerWithFilesFn = Arc<
39    dyn Fn(
40            RuneContext,
41            Bytes,
42            Vec<FileAttachment>,
43            StreamSender,
44        ) -> BoxFuture<'static, SdkResult<()>>
45        + Send
46        + Sync,
47>;
48
49// ---------------------------------------------------------------------------
50// Handler enum
51// ---------------------------------------------------------------------------
52
53/// An erased handler, either unary or stream, with or without files.
54#[derive(Clone)]
55pub enum HandlerKind {
56    Unary(RuneHandlerFn),
57    UnaryWithFiles(RuneHandlerWithFilesFn),
58    Stream(StreamRuneHandlerFn),
59    StreamWithFiles(StreamRuneHandlerWithFilesFn),
60}
61
62impl std::fmt::Debug for HandlerKind {
63    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64        match self {
65            Self::Unary(_) => write!(f, "HandlerKind::Unary(...)"),
66            Self::UnaryWithFiles(_) => write!(f, "HandlerKind::UnaryWithFiles(...)"),
67            Self::Stream(_) => write!(f, "HandlerKind::Stream(...)"),
68            Self::StreamWithFiles(_) => write!(f, "HandlerKind::StreamWithFiles(...)"),
69        }
70    }
71}
72
73impl HandlerKind {
74    /// Whether the handler is a stream handler.
75    pub fn is_stream(&self) -> bool {
76        matches!(self, Self::Stream(_) | Self::StreamWithFiles(_))
77    }
78
79    /// Whether the handler accepts file attachments.
80    pub fn accepts_files(&self) -> bool {
81        matches!(self, Self::UnaryWithFiles(_) | Self::StreamWithFiles(_))
82    }
83}
84
85// ---------------------------------------------------------------------------
86// Registered rune
87// ---------------------------------------------------------------------------
88
89/// A registered rune with its config and handler.
90#[derive(Debug, Clone)]
91pub struct RegisteredRune {
92    /// Rune configuration.
93    pub config: RuneConfig,
94    /// The handler to execute.
95    pub handler: HandlerKind,
96}