Skip to content

Commit a7b160e

Browse files
committed
Remove lazy_static
1 parent 1eb3549 commit a7b160e

File tree

3 files changed

+45
-48
lines changed

3 files changed

+45
-48
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ anyhow = "1.0"
1313
thiserror = "1.0"
1414
async-trait = "0.1"
1515
bytes = "*"
16+
once_cell = "1"
1617

1718
reqwest = { version = "0.11", features = ["json", "rustls-tls"], default-features = false }
1819
tokio = { version = "1", features = ["full"] }
@@ -29,7 +30,6 @@ hmac = "0.12"
2930
sha2 = "0.10"
3031

3132
futures = "0.3"
32-
lazy_static = "1.4.0"
3333
rand = "0.8"
3434
redis = "0.21"
3535

src/modules/mhw.rs

+40-43
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,56 @@
11
use crate::slack;
2-
use lazy_static::lazy_static;
32
use rand::{thread_rng, Rng};
43

54
struct MonsterHunterData<'a> {
6-
keywords: Vec<&'a str>,
5+
keywords: &'a [&'a str],
76
text: &'a str,
87
image_url: &'a str,
98
}
109

11-
lazy_static! {
12-
static ref MHW_DATA: Vec<MonsterHunterData<'static>> = vec![
13-
MonsterHunterData {
14-
keywords: vec!["ㄷㄷ", "ㄷㄷ가마루", "도도가마루"],
15-
text: "도도가마루",
16-
image_url:
17-
"https://raw.githubusercontent.com/skyser2003/ditto_bot_rust/master/images/Dodogama.png"
18-
},
19-
MonsterHunterData {
20-
keywords: vec!["ㅊㅊ", "추천"],
21-
text: "치치야크",
22-
image_url:
23-
"https://raw.githubusercontent.com/skyser2003/ditto_bot_rust/master/images/Tzitzi_Ya_Ku.png"
24-
},
25-
MonsterHunterData {
26-
keywords: vec!["ㅈㄹ", "지랄"],
27-
text: "조라마그다라오스",
28-
image_url:
29-
"https://raw.githubusercontent.com/skyser2003/ditto_bot_rust/master/images/Zorah_Magdaros.png"
30-
},
31-
MonsterHunterData {
32-
keywords: vec!["ㄹㅇ", "리얼"],
33-
text: "로아루드로스",
34-
image_url:
35-
"https://raw.githubusercontent.com/skyser2003/ditto_bot_rust/master/images/Royal_Ludroth.png"
36-
},
37-
MonsterHunterData {
38-
keywords: vec!["ㅇㄷ"],
39-
text: "오도가론",
40-
image_url:
41-
"https://raw.githubusercontent.com/skyser2003/ditto_bot_rust/master/images/Odogaron.png"
42-
},
43-
MonsterHunterData {
44-
keywords: vec!["이불", "졸려", "잘래", "잠와", "이블조"],
45-
text: "이블조",
46-
image_url:
47-
"https://raw.githubusercontent.com/skyser2003/ditto_bot_rust/master/images/Evil_Jaw.png"
48-
},
49-
];
10+
macro_rules! url_prefix {
11+
() => {
12+
"https://raw.githubusercontent.com/skyser2003/ditto_bot_rust/master/images/"
13+
};
5014
}
5115

16+
const MHW_DATA: &'static [MonsterHunterData<'static>] = &[
17+
MonsterHunterData {
18+
keywords: &["ㄷㄷ", "ㄷㄷ가마루", "도도가마루"],
19+
text: "도도가마루",
20+
image_url: concat!(url_prefix!(), "Dodogama.png"),
21+
},
22+
MonsterHunterData {
23+
keywords: &["ㅊㅊ", "추천"],
24+
text: "치치야크",
25+
image_url: concat!(url_prefix!(), "Tzitzi_Ya_Ku.png"),
26+
},
27+
MonsterHunterData {
28+
keywords: &["ㅈㄹ", "지랄"],
29+
text: "조라마그다라오스",
30+
image_url: concat!(url_prefix!(), "Zorah_Magdaros.png"),
31+
},
32+
MonsterHunterData {
33+
keywords: &["ㄹㅇ", "리얼"],
34+
text: "로아루드로스",
35+
image_url: concat!(url_prefix!(), "Royal_Ludroth.png"),
36+
},
37+
MonsterHunterData {
38+
keywords: &["ㅇㄷ"],
39+
text: "오도가론",
40+
image_url: concat!(url_prefix!(), "Odogaron.png"),
41+
},
42+
MonsterHunterData {
43+
keywords: &["이불", "졸려", "잘래", "잠와", "이블조"],
44+
text: "이블조",
45+
image_url: concat!(url_prefix!(), "Evil_Jaw.png"),
46+
},
47+
];
48+
5249
pub async fn handle<B: crate::Bot>(bot: &B, msg: &crate::MessageEvent) -> anyhow::Result<()> {
5350
// TODO: Remove hard coded value
5451
if thread_rng().gen_range(0..100) < 35 {
55-
for data in &*MHW_DATA {
56-
for keyword in &data.keywords {
52+
for data in MHW_DATA {
53+
for keyword in data.keywords {
5754
if msg.text.contains(keyword) {
5855
bot.send_message(
5956
&msg.channel,

src/modules/namuwiki.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use crate::slack;
2-
use lazy_static::lazy_static;
2+
3+
use once_cell::sync::OnceCell;
34
use regex::Regex;
45
use reqwest::Url;
56

6-
lazy_static! {
7-
static ref TITLE_REGEX: Regex = Regex::new(r"<title>(.+) - 나무위키</title>").unwrap();
8-
}
7+
const TITLE_REGEX: OnceCell<Regex> = OnceCell::new();
98

109
pub async fn handle<B: crate::Bot>(bot: &B, msg: &crate::MessageEvent) -> anyhow::Result<()> {
1110
if let Some(link) = &msg.link {
@@ -24,6 +23,7 @@ pub async fn handle<B: crate::Bot>(bot: &B, msg: &crate::MessageEvent) -> anyhow
2423
let body = res.text().await?;
2524

2625
let title_opt = TITLE_REGEX
26+
.get_or_try_init(|| Regex::new(r"<title>(.+) - 나무위키</title>"))?
2727
.captures(&body)
2828
.and_then(|captures| captures.get(1).map(|match_title| match_title.as_str()));
2929

0 commit comments

Comments
 (0)