File tree Expand file tree Collapse file tree 5 files changed +71
-43
lines changed
crates/codora-framework-bot/src Expand file tree Collapse file tree 5 files changed +71
-43
lines changed Original file line number Diff line number Diff line change 22pub mod discord;
33
44#[ cfg( feature = "codora-framework-bot-telegram" ) ]
5+ /// Telegram
6+ ///
7+ ///
58pub mod telegram;
Original file line number Diff line number Diff line change 1+ use crate :: { bot:: Bot , listener:: Listener } ;
2+ use std:: {
3+ pin:: Pin ,
4+ task:: { Context , Poll } ,
5+ } ;
6+
7+ #[ derive( Debug , thiserror:: Error ) ]
8+ #[ error( "{0}" ) ]
9+ pub enum Error {
10+ #[ error( "[Error]: Server error happened" ) ]
11+ ServeError ,
12+ }
13+
14+ pub type Result < T , E = Error > = core:: result:: Result < T , E > ;
15+
16+ #[ derive( Debug ) ]
17+ pub struct BotContext < T > {
18+ bot_handler : T ,
19+ // extension
20+ }
21+
22+ impl < T > BotContext < T > {
23+ pub fn builder ( bot_handler : T ) -> BotContextBuilder < T > {
24+ BotContextBuilder { bot_handler }
25+ }
26+
27+ pub async fn run_with_listener < L > ( self , listener : L ) -> Result < ( ) >
28+ where
29+ L : Listener ,
30+ {
31+ todo ! ( )
32+ }
33+ }
34+
35+ #[ derive( Debug ) ]
36+ pub struct BotContextBuilder < T > {
37+ bot_handler : T ,
38+ }
39+
40+ impl < T > BotContextBuilder < T > {
41+ pub fn with_graceful_shutdown < F > ( self , shutdown_handler : F ) -> BotContextBuilder < T >
42+ where
43+ F : Future < Output = ( ) > + Send + ' static ,
44+ {
45+ todo ! ( )
46+ }
47+
48+ pub fn build ( self ) -> BotContext < T > {
49+ let BotContextBuilder { bot_handler } = self ;
50+ BotContext { bot_handler }
51+ }
52+ }
Original file line number Diff line number Diff line change 1+ pub mod context;
2+
13/// Bot contract
24///
35///
@@ -11,11 +13,5 @@ pub trait Bot<Request> {
1113 fn handle ( & self , request : Request ) -> impl Future < Output = Result < ( ) , Self :: Error > > ;
1214}
1315
14- #[ derive( Debug , new) ]
15- pub struct Context < T > {
16- bot_handler : T ,
17- // extension
18- }
19-
2016// Request would be Telegram or Discord Updates
2117// impl<T> Bot<T> where T: BotHandler {}
Original file line number Diff line number Diff line change @@ -18,16 +18,21 @@ pub mod api;
1818pub mod bot;
1919pub mod handler;
2020mod listener;
21- mod serve;
22- pub use serve:: { Error as ServeError , serve} ;
2321
2422#[ cfg( feature = "codora-framework-bot-telegram" ) ]
2523use codora_framework_macro:: Command ;
2624
2725#[ cfg( test) ]
2826mod tests {
2927 use super :: * ;
30- use crate :: adapter:: telegram:: { TGBot , TGBotOption , TGWebhook } ;
28+ use crate :: {
29+ adapter:: telegram:: { TGBot , TGBotOption , TGWebhook } ,
30+ bot:: context:: BotContext ,
31+ } ;
32+
33+ async fn handler ( ) {
34+ // implement the graceful shutdown here
35+ }
3136
3237 #[ tokio:: test]
3338 async fn codora_framework_bot_test ( ) -> anyhow:: Result < ( ) > {
@@ -40,7 +45,12 @@ mod tests {
4045 let bot = TGBot :: with ( TGBotOption { } ) . command ( Command :: Start ) ;
4146
4247 // Identical api with axum yes
43- let res = serve ( listener, bot) . await ?;
48+ let res = BotContext :: builder ( bot)
49+ . with_graceful_shutdown ( handler ( ) )
50+ . build ( )
51+ // .run()
52+ . run_with_listener ( listener)
53+ . await ?;
4454 Ok ( ( ) )
4555 }
4656}
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments