Skip to content

Commit dfc11f6

Browse files
authored
Refactor Devil Fruit Scraper (#5)
* update tokio * wip refactor scraper * refactored scraper code
1 parent b58b0b1 commit dfc11f6

File tree

11 files changed

+592
-372
lines changed

11 files changed

+592
-372
lines changed

Cargo.lock

Lines changed: 16 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ repository = "https://github.com/muhrifqii/opdb-rust.git"
1313
readme = "README.md"
1414

1515
[workspace.dependencies]
16-
tokio = { version = "1.44.1", features = ["full"] }
16+
tokio = { version = "1.44.2", features = ["full"] }
1717
serde = { version = "1.0.219", features = ["derive"] }
1818
serde_json = "1.0.140"
1919
strum = { version = "0.27.1", features = ["derive"] }
@@ -26,3 +26,5 @@ reqwest = "0.12.15"
2626
strsim = "0.11.1"
2727
clap = { version = "4.5.35", features = ["derive"] }
2828
scraper = "0.23.1"
29+
lazy_static = "1.5.0"
30+
async-trait = "0.1.88"

scraper/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "opdb-scrapper"
2+
name = "opdb_scrapper"
33
version.workspace = true
44
edition.workspace = true
55
authors.workspace = true
@@ -24,6 +24,8 @@ reqwest = { workspace = true }
2424
strsim = { workspace = true }
2525
clap = { workspace = true }
2626
scraper = { workspace = true }
27+
lazy_static = { workspace = true }
28+
async-trait = { workspace = true }
2729

2830
[dev-dependencies]
2931
mockall = "0.13.1"

scraper/src/df/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub mod models;
2+
pub mod parser;
23
pub mod scraper;
34
pub mod types;

scraper/src/df/models.rs

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use serde::Serialize;
22
use std::cmp::Ordering;
33

4-
use super::types::{DfSubType, DfType};
4+
use super::types::{DfSubType, DfType, HasDevilFruit};
55
use crate::types::UrlTyped;
66

77
#[derive(Debug, Serialize, Default)]
@@ -22,6 +22,23 @@ impl std::fmt::Display for DfTypeInfo {
2222
}
2323
}
2424

25+
#[derive(Debug)]
26+
pub struct DevilFruitName {
27+
name: String,
28+
en_name: String,
29+
description: String,
30+
}
31+
32+
impl DevilFruitName {
33+
pub fn new(name: String, en_name: String, description: String) -> Self {
34+
Self {
35+
name,
36+
en_name,
37+
description,
38+
}
39+
}
40+
}
41+
2542
#[derive(Debug, Serialize)]
2643
pub struct DevilFruit {
2744
pub df_type: DfType,
@@ -33,6 +50,48 @@ pub struct DevilFruit {
3350
pub df_url: String,
3451
}
3552

53+
impl DevilFruit {
54+
pub fn zoan(
55+
sub_type: Option<DfSubType>,
56+
name_detail: DevilFruitName,
57+
pic_url: String,
58+
df_url: String,
59+
) -> Self {
60+
Self {
61+
df_type: DfType::Zoan,
62+
df_sub_type: sub_type,
63+
name: name_detail.name,
64+
en_name: name_detail.en_name,
65+
description: name_detail.description,
66+
pic_url,
67+
df_url,
68+
}
69+
}
70+
71+
pub fn non_zoan(
72+
df_type: DfType,
73+
name_detail: DevilFruitName,
74+
pic_url: String,
75+
df_url: String,
76+
) -> Self {
77+
Self {
78+
df_type,
79+
df_sub_type: None,
80+
name: name_detail.name,
81+
en_name: name_detail.en_name,
82+
description: name_detail.description,
83+
pic_url,
84+
df_url,
85+
}
86+
}
87+
}
88+
89+
impl HasDevilFruit for DevilFruit {
90+
fn df_type(&self) -> DfType {
91+
self.df_type
92+
}
93+
}
94+
3695
impl std::fmt::Display for DevilFruit {
3796
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
3897
write!(
@@ -92,12 +151,12 @@ mod tests {
92151
use crate::{
93152
df::{
94153
models::DfTypeInfo,
95-
types::{DfSubType, DfType},
154+
types::{DfSubType, DfType, HasDevilFruit},
96155
},
97156
types::UrlTyped,
98157
};
99158

100-
use super::{Character, DevilFruit};
159+
use super::{Character, DevilFruit, DevilFruitName};
101160

102161
#[test]
103162
fn df_has_valid_traits() {
@@ -146,6 +205,7 @@ mod tests {
146205
format!("(df_type: {}, df_sub_type: {:?}, name: {}, english name: {}, pic: {}, url: {}, description: {})",
147206
df1.df_type, df1.df_sub_type, df1.name, df1.en_name, df1.pic_url, df1.df_url, df1.description
148207
));
208+
assert_eq!(DfType::Zoan, df3.df_type());
149209
}
150210

151211
#[test]
@@ -157,4 +217,30 @@ mod tests {
157217
};
158218
assert_eq!(character.get_path(), "/wiki/Character:1234");
159219
}
220+
221+
#[test]
222+
fn df_constructor_tests() {
223+
let df = DevilFruit::zoan(
224+
Some(DfSubType::MythicalZoan),
225+
DevilFruitName::new(
226+
"fruito".to_string(),
227+
"fruit".to_string(),
228+
"lorem".to_string(),
229+
),
230+
"".to_string(),
231+
"".to_string(),
232+
);
233+
assert_eq!("fruit", df.en_name);
234+
let df = DevilFruit::non_zoan(
235+
DfType::Logia,
236+
DevilFruitName::new(
237+
"ananas".to_string(),
238+
"pinaple".to_string(),
239+
"lorem".to_string(),
240+
),
241+
"".to_string(),
242+
"".to_string(),
243+
);
244+
assert_eq!("ananas", df.name);
245+
}
160246
}

0 commit comments

Comments
 (0)