Skip to content

Commit 68640b5

Browse files
committed
Updated how api is structured in codora-framework-bot
1 parent 581d265 commit 68640b5

File tree

5 files changed

+71
-43
lines changed

5 files changed

+71
-43
lines changed

crates/codora-framework-bot/src/adapter/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
pub mod discord;
33

44
#[cfg(feature = "codora-framework-bot-telegram")]
5+
/// Telegram
6+
///
7+
///
58
pub mod telegram;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
}
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
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 {}

crates/codora-framework-bot/src/lib.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,21 @@ pub mod api;
1818
pub mod bot;
1919
pub mod handler;
2020
mod listener;
21-
mod serve;
22-
pub use serve::{Error as ServeError, serve};
2321

2422
#[cfg(feature = "codora-framework-bot-telegram")]
2523
use codora_framework_macro::Command;
2624

2725
#[cfg(test)]
2826
mod 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
}

crates/codora-framework-bot/src/serve.rs

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)