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 =
34    Arc<dyn Fn(RuneContext, Bytes, StreamSender) -> BoxFuture<'static, SdkResult<()>> + Send + Sync>;
35
36/// A stream handler that also accepts files: `(ctx, input, files, stream) -> Result<()>`.
37pub type StreamRuneHandlerWithFilesFn = Arc<
38    dyn Fn(RuneContext, Bytes, Vec<FileAttachment>, StreamSender) -> BoxFuture<'static, SdkResult<()>>
39        + Send
40        + Sync,
41>;
42
43// ---------------------------------------------------------------------------
44// Handler enum
45// ---------------------------------------------------------------------------
46
47/// An erased handler, either unary or stream, with or without files.
48#[derive(Clone)]
49pub enum HandlerKind {
50    Unary(RuneHandlerFn),
51    UnaryWithFiles(RuneHandlerWithFilesFn),
52    Stream(StreamRuneHandlerFn),
53    StreamWithFiles(StreamRuneHandlerWithFilesFn),
54}
55
56impl std::fmt::Debug for HandlerKind {
57    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58        match self {
59            Self::Unary(_) => write!(f, "HandlerKind::Unary(...)"),
60            Self::UnaryWithFiles(_) => write!(f, "HandlerKind::UnaryWithFiles(...)"),
61            Self::Stream(_) => write!(f, "HandlerKind::Stream(...)"),
62            Self::StreamWithFiles(_) => write!(f, "HandlerKind::StreamWithFiles(...)"),
63        }
64    }
65}
66
67impl HandlerKind {
68    /// Whether the handler is a stream handler.
69    pub fn is_stream(&self) -> bool {
70        matches!(self, Self::Stream(_) | Self::StreamWithFiles(_))
71    }
72
73    /// Whether the handler accepts file attachments.
74    pub fn accepts_files(&self) -> bool {
75        matches!(self, Self::UnaryWithFiles(_) | Self::StreamWithFiles(_))
76    }
77}
78
79// ---------------------------------------------------------------------------
80// Registered rune
81// ---------------------------------------------------------------------------
82
83/// A registered rune with its config and handler.
84#[derive(Debug, Clone)]
85pub struct RegisteredRune {
86    /// Rune configuration.
87    pub config: RuneConfig,
88    /// The handler to execute.
89    pub handler: HandlerKind,
90}