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 =
34 Arc<dyn Fn(RuneContext, Bytes, StreamSender) -> BoxFuture<'static, SdkResult<()>> + Send + Sync>;
35
36pub type StreamRuneHandlerWithFilesFn = Arc<
38 dyn Fn(RuneContext, Bytes, Vec<FileAttachment>, StreamSender) -> BoxFuture<'static, SdkResult<()>>
39 + Send
40 + Sync,
41>;
42
43#[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 pub fn is_stream(&self) -> bool {
70 matches!(self, Self::Stream(_) | Self::StreamWithFiles(_))
71 }
72
73 pub fn accepts_files(&self) -> bool {
75 matches!(self, Self::UnaryWithFiles(_) | Self::StreamWithFiles(_))
76 }
77}
78
79#[derive(Debug, Clone)]
85pub struct RegisteredRune {
86 pub config: RuneConfig,
88 pub handler: HandlerKind,
90}