Skip to content

Commit 3e9cce0

Browse files
authored
Merge pull request #107 from approvers/refactor
fun: ✨ STATICALLY DISPATCHED CLIENTS ✨ + other stuff
2 parents 7258731 + a01e21d commit 3e9cce0

30 files changed

+1189
-892
lines changed

Cargo.lock

+532-435
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ default = ["prod"]
2525

2626
[dependencies]
2727
anyhow = "1"
28-
async-trait = "0.1"
2928
chrono = { version = "0.4", features = ["serde"] }
3029
chrono-tz = "0.10"
3130
clap = { version = "4", features = ["derive"] }
@@ -34,7 +33,7 @@ dotenv = "0.15"
3433
hex = "0.4"
3534
humantime = "2"
3635
image = "0.25"
37-
libwebp-sys = "0.10"
36+
libwebp-sys = "0.11"
3837
once_cell = "1"
3938
ordered-float = { version = "4" }
4039
parking_lot = "0.12"
@@ -46,7 +45,7 @@ serde_json = "1"
4645
sha2 = "0.10"
4746
shellwords = "1"
4847
static_assertions = "1"
49-
thiserror = "1"
48+
thiserror = "2"
5049
tracing = "0.1"
5150
tracing-subscriber = "0.3"
5251
url = "2"
@@ -66,7 +65,7 @@ crossbeam = { version = "0.8", optional = true }
6665
bzip2-sys = { version = "0.1.11", features = ["static"] }
6766

6867
[dependencies.charming]
69-
version = "0.3"
68+
version = "0.4"
7069
optional = true
7170
default-features = false
7271
features = ["ssr"]

Justfile

-14
This file was deleted.

src/bot/alias/command.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ impl<D: MessageAliasDatabase> MessageAliasBot<D> {
5353

5454
pub(super) async fn make(
5555
&self,
56-
ctx: &dyn Context,
56+
ctx: &impl Context,
5757
key: &str,
5858
msg: Option<&str>,
59-
attachments: &[&dyn Attachment],
59+
attachments: &[impl Attachment],
6060
force: bool,
6161
) -> Result<()> {
6262
let key = key.trim();

src/bot/alias/mod.rs

+24-18
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
mod command;
2-
pub(crate) mod model;
2+
pub mod model;
33

44
use {
55
crate::bot::{
66
alias::model::MessageAlias, parse_command, ui, BotService, Context, IsUpdated, Message,
7-
SendAttachment, SendMessage,
7+
Runtime, SendAttachment, SendMessage,
88
},
99
anyhow::Result,
10-
async_trait::async_trait,
10+
std::future::Future,
1111
};
1212

1313
const NAME: &str = "rusty_ponyo::bot::alias";
@@ -50,27 +50,29 @@ enum Command {
5050
},
5151
}
5252

53-
#[async_trait]
54-
pub(crate) trait MessageAliasDatabase: Send + Sync {
55-
async fn save(&self, alias: MessageAlias) -> Result<()>;
56-
async fn get(&self, key: &str) -> Result<Option<MessageAlias>>;
57-
async fn get_and_increment_usage_count(&self, key: &str) -> Result<Option<MessageAlias>>;
58-
async fn delete(&self, key: &str) -> Result<IsUpdated>;
59-
async fn len(&self) -> Result<u32>;
60-
async fn usage_count_top_n(&self, n: usize) -> Result<Vec<MessageAlias>>;
53+
pub trait MessageAliasDatabase: Send + Sync {
54+
fn save(&self, alias: MessageAlias) -> impl Future<Output = Result<()>> + Send;
55+
fn get(&self, key: &str) -> impl Future<Output = Result<Option<MessageAlias>>> + Send;
56+
fn get_and_increment_usage_count(
57+
&self,
58+
key: &str,
59+
) -> impl Future<Output = Result<Option<MessageAlias>>> + Send;
60+
fn delete(&self, key: &str) -> impl Future<Output = Result<IsUpdated>> + Send;
61+
fn len(&self) -> impl Future<Output = Result<u32>> + Send;
62+
fn usage_count_top_n(&self, n: usize)
63+
-> impl Future<Output = Result<Vec<MessageAlias>>> + Send;
6164
}
6265

63-
pub(crate) struct MessageAliasBot<D: MessageAliasDatabase> {
66+
pub struct MessageAliasBot<D: MessageAliasDatabase> {
6467
db: D,
6568
}
6669

67-
#[async_trait]
68-
impl<D: MessageAliasDatabase> BotService for MessageAliasBot<D> {
70+
impl<R: Runtime, D: MessageAliasDatabase> BotService<R> for MessageAliasBot<D> {
6971
fn name(&self) -> &'static str {
7072
NAME
7173
}
7274

73-
async fn on_message(&self, msg: &dyn Message, ctx: &dyn Context) -> Result<()> {
75+
async fn on_message(&self, msg: &R::Message, ctx: &R::Context) -> Result<()> {
7476
if msg.content().starts_with(PREFIX) {
7577
if let Some(msg) = self.on_command(msg, ctx).await? {
7678
ctx.send_message(SendMessage {
@@ -90,11 +92,15 @@ impl<D: MessageAliasDatabase> BotService for MessageAliasBot<D> {
9092
}
9193

9294
impl<D: MessageAliasDatabase> MessageAliasBot<D> {
93-
pub(crate) fn new(db: D) -> Self {
95+
pub fn new(db: D) -> Self {
9496
Self { db }
9597
}
9698

97-
async fn on_command(&self, message: &dyn Message, ctx: &dyn Context) -> Result<Option<String>> {
99+
async fn on_command(
100+
&self,
101+
message: &impl Message,
102+
ctx: &impl Context,
103+
) -> Result<Option<String>> {
98104
let Some(parsed) = parse_command::<Ui>(message.content(), ctx).await? else {
99105
return Ok(None);
100106
};
@@ -117,7 +123,7 @@ impl<D: MessageAliasDatabase> MessageAliasBot<D> {
117123
}
118124
}
119125

120-
async fn send_alias(&self, ctx: &dyn Context, alias: &MessageAlias) -> Result<()> {
126+
async fn send_alias(&self, ctx: &impl Context, alias: &MessageAlias) -> Result<()> {
121127
ctx.send_message(SendMessage {
122128
content: &alias.message,
123129
attachments: &alias

src/bot/alias/model.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ use {
44
};
55

66
#[derive(Clone, Serialize)]
7-
pub(crate) struct MessageAlias {
8-
pub(crate) key: String,
9-
pub(crate) message: String,
10-
pub(crate) attachments: Vec<MessageAliasAttachment>,
11-
pub(crate) usage_count: u32,
12-
pub(crate) created_at: DateTime<Utc>,
7+
pub struct MessageAlias {
8+
pub key: String,
9+
pub message: String,
10+
pub attachments: Vec<MessageAliasAttachment>,
11+
pub usage_count: u32,
12+
pub created_at: DateTime<Utc>,
1313
}
1414

1515
#[derive(Clone, Serialize)]
16-
pub(crate) struct MessageAliasAttachment {
17-
pub(crate) name: String,
18-
pub(crate) data: Vec<u8>,
16+
pub struct MessageAliasAttachment {
17+
pub name: String,
18+
pub data: Vec<u8>,
1919
}

src/bot/auth/mod.rs

+21-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use {
2-
crate::bot::{parse_command, ui, BotService, Context, Message},
2+
crate::bot::{parse_command, ui, BotService, Context, Message, Runtime, User},
33
anyhow::{Context as _, Result},
4-
async_trait::async_trait,
54
rand::{prelude::StdRng, Rng, SeedableRng},
65
sequoia_openpgp::{
76
cert::CertParser,
@@ -11,7 +10,7 @@ use {
1110
Cert,
1211
},
1312
sha2::Digest,
14-
std::{io::Write, time::Duration},
13+
std::{future::Future, io::Write, time::Duration},
1514
url::{Host, Origin, Url},
1615
};
1716

@@ -50,28 +49,31 @@ enum SetCommand {
5049
},
5150
}
5251

53-
#[async_trait]
54-
pub(crate) trait GenkaiAuthDatabase: Send + Sync {
55-
async fn register_pgp_key(&self, user_id: u64, cert: &str) -> Result<()>;
56-
async fn get_pgp_key(&self, user_id: u64) -> Result<Option<String>>;
57-
58-
async fn register_token(&self, user_id: u64, hashed_token: &str) -> Result<()>;
59-
async fn revoke_token(&self, user_id: u64) -> Result<()>;
60-
async fn get_token(&self, user_id: u64) -> Result<Option<String>>;
52+
pub trait GenkaiAuthDatabase: Send + Sync {
53+
fn register_pgp_key(&self, user_id: u64, cert: &str)
54+
-> impl Future<Output = Result<()>> + Send;
55+
fn get_pgp_key(&self, user_id: u64) -> impl Future<Output = Result<Option<String>>> + Send;
56+
57+
fn register_token(
58+
&self,
59+
user_id: u64,
60+
hashed_token: &str,
61+
) -> impl Future<Output = Result<()>> + Send;
62+
fn revoke_token(&self, user_id: u64) -> impl Future<Output = Result<()>> + Send;
63+
fn get_token(&self, user_id: u64) -> impl Future<Output = Result<Option<String>>> + Send;
6164
}
6265

63-
pub(crate) struct GenkaiAuthBot<D> {
66+
pub struct GenkaiAuthBot<D> {
6467
db: D,
6568
pgp_pubkey_source_domain_whitelist: Vec<String>,
6669
}
6770

68-
#[async_trait]
69-
impl<D: GenkaiAuthDatabase> BotService for GenkaiAuthBot<D> {
71+
impl<R: Runtime, D: GenkaiAuthDatabase> BotService<R> for GenkaiAuthBot<D> {
7072
fn name(&self) -> &'static str {
7173
NAME
7274
}
7375

74-
async fn on_message(&self, msg: &dyn Message, ctx: &dyn Context) -> Result<()> {
76+
async fn on_message(&self, msg: &R::Message, ctx: &R::Context) -> Result<()> {
7577
if !msg.content().starts_with(PREFIX) {
7678
return Ok(());
7779
}
@@ -93,14 +95,14 @@ impl<D: GenkaiAuthDatabase> BotService for GenkaiAuthBot<D> {
9395
}
9496

9597
impl<D: GenkaiAuthDatabase> GenkaiAuthBot<D> {
96-
pub(crate) fn new(db: D, pubkey_whitelist: Vec<String>) -> Self {
98+
pub fn new(db: D, pubkey_whitelist: Vec<String>) -> Self {
9799
Self {
98100
db,
99101
pgp_pubkey_source_domain_whitelist: pubkey_whitelist,
100102
}
101103
}
102104

103-
async fn set_pgp(&self, msg: &dyn Message, ctx: &dyn Context, url: &str) -> Result<()> {
105+
async fn set_pgp(&self, msg: &impl Message, ctx: &impl Context, url: &str) -> Result<()> {
104106
let verify_result = match self.verify_url(url) {
105107
Ok(_) => download_gpg_key(url).await,
106108
Err(e) => Err(e),
@@ -127,7 +129,7 @@ impl<D: GenkaiAuthDatabase> GenkaiAuthBot<D> {
127129
Ok(())
128130
}
129131

130-
async fn token(&self, msg: &dyn Message, ctx: &dyn Context) -> Result<()> {
132+
async fn token(&self, msg: &impl Message, ctx: &impl Context) -> Result<()> {
131133
let author = msg.author();
132134

133135
if self.db.get_token(author.id()).await?.is_some() {
@@ -171,7 +173,7 @@ impl<D: GenkaiAuthDatabase> GenkaiAuthBot<D> {
171173
Ok(())
172174
}
173175

174-
async fn revoke(&self, msg: &dyn Message, ctx: &dyn Context) -> Result<()> {
176+
async fn revoke(&self, msg: &impl Message, ctx: &impl Context) -> Result<()> {
175177
self.db
176178
.revoke_token(msg.author().id())
177179
.await

src/bot/genkai_point/formula/mod.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
use {self::v3::FormulaV3, crate::bot::genkai_point::model::Session};
1+
use {crate::bot::genkai_point::model::Session, v3::FormulaV3};
22

33
pub mod v1;
44
pub mod v2;
55
pub mod v3;
66

7-
pub(crate) trait GenkaiPointFormula: Send + Sync + 'static {
7+
pub trait GenkaiPointFormula: Send + Sync + 'static {
88
fn name(&self) -> &'static str;
99
fn calc(&self, sessions: &[Session]) -> GenkaiPointFormulaOutput;
1010
}
1111

12-
pub(crate) struct GenkaiPointFormulaOutput {
13-
pub(crate) point: u64,
14-
pub(crate) efficiency: f64,
12+
pub struct GenkaiPointFormulaOutput {
13+
pub point: u64,
14+
pub efficiency: f64,
1515
}
1616

17-
pub(crate) fn default_formula() -> impl GenkaiPointFormula {
17+
pub fn default_formula() -> impl GenkaiPointFormula {
1818
FormulaV3
1919
}
2020

21-
pub(crate) struct DynGenkaiPointFormula(pub Box<dyn GenkaiPointFormula>);
21+
pub struct DynGenkaiPointFormula(pub Box<dyn GenkaiPointFormula>);
2222

2323
impl GenkaiPointFormula for DynGenkaiPointFormula {
2424
fn name(&self) -> &'static str {

src/bot/genkai_point/formula/v1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use {
77
chrono_tz::Asia::Tokyo,
88
};
99

10-
pub(crate) struct FormulaV1;
10+
pub struct FormulaV1;
1111

1212
impl GenkaiPointFormula for FormulaV1 {
1313
fn name(&self) -> &'static str {

src/bot/genkai_point/formula/v2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use {
77
chrono_tz::Asia::Tokyo,
88
};
99

10-
pub(crate) struct FormulaV2;
10+
pub struct FormulaV2;
1111

1212
impl GenkaiPointFormula for FormulaV2 {
1313
fn name(&self) -> &'static str {

src/bot/genkai_point/formula/v3.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use {
44
chrono_tz::Asia::Tokyo,
55
};
66

7-
pub(crate) struct FormulaV3;
7+
pub struct FormulaV3;
88

99
impl GenkaiPointFormula for FormulaV3 {
1010
fn name(&self) -> &'static str {

0 commit comments

Comments
 (0)