rune_framework/
handler.rs1use 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
14pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
20
21pub type RuneHandlerFn =
23 Arc<dyn Fn(RuneContext, Bytes) -> BoxFuture<'static, SdkResult<Bytes>> + Send + Sync>;
24
25pub type RuneHandlerWithFilesFn = Arc<
27 dyn Fn(RuneContext, Bytes, Vec<FileAttachment>) -> BoxFuture<'static, SdkResult<Bytes>>
28 + Send
29 + Sync,
30>;
31
32pub type StreamRuneHandlerFn = Arc<
34 dyn Fn(RuneContext, Bytes, StreamSender) -> BoxFuture<'static, SdkResult<()>> + Send + Sync,
35>;
36
37pub 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#[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 pub fn is_stream(&self) -> bool {
76 matches!(self, Self::Stream(_) | Self::StreamWithFiles(_))
77 }
78
79 pub fn accepts_files(&self) -> bool {
81 matches!(self, Self::UnaryWithFiles(_) | Self::StreamWithFiles(_))
82 }
83}
84
85#[derive(Debug, Clone)]
91pub struct RegisteredRune {
92 pub config: RuneConfig,
94 pub handler: HandlerKind,
96}