Skip to content

Commit

Permalink
refactor: 调整lib结构
Browse files Browse the repository at this point in the history
  • Loading branch information
龚亦航 committed Nov 14, 2023
1 parent 0a53ba8 commit 1e274e9
Show file tree
Hide file tree
Showing 17 changed files with 213 additions and 334 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ path = "src/lib.rs"
[package]
authors = ["AnselYuki"]
name = "maimai-search"
version = "0.4.1"
version = "0.4.2"
edition = "2021"
repository = "https://github.com/Anselyuki/maimai-search-rs"

Expand Down
34 changes: 19 additions & 15 deletions src/clients/song_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub fn search_songs_by_title(param: &str, count: usize) -> Vec<Song> {
}

pub mod entity {
use std::fmt;
use std::io::Error;
use std::process::exit;

Expand Down Expand Up @@ -145,20 +146,23 @@ pub mod entity {
BasicInfo,
}

impl SongField {
/// 各个字段对应在 Tantivy Schema 里的 Field 名称
pub fn to_string(&self) -> &str {
match self {
SongField::Id => "id",
SongField::Keyword => "keyword",
SongField::Title => "title",
SongField::SongType => "song_type",
SongField::Ds => "ds",
SongField::Level => "level",
SongField::Cids => "cids",
SongField::Charts => "charts",
SongField::BasicInfo => "basic_info",
}
impl fmt::Display for SongField {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}",
match self {
SongField::Id => "id",
SongField::Keyword => "keyword",
SongField::Title => "title",
SongField::SongType => "song_type",
SongField::Ds => "ds",
SongField::Level => "level",
SongField::Cids => "cids",
SongField::Charts => "charts",
SongField::BasicInfo => "basic_info",
}
)
}
}

Expand Down Expand Up @@ -202,7 +206,7 @@ pub mod entity {

/// 单独获取字段(静态方法)
pub fn field(song_field: SongField) -> Field {
match SONG_SCHEMA.get_field(song_field.to_string()) {
match SONG_SCHEMA.get_field(&*song_field.to_string()) {
Ok(field) => field,
Err(error) => {
error!("获取 Field 失败\n[Cause]:{:?}", error);
Expand Down
43 changes: 14 additions & 29 deletions src/clients/user_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn get_b50_data(username: &str) -> Result<B50Response, Box<dyn Error>> {
.body(payload.to_string());
let response = request.send()?;
let status = response.status();
let response = match status.as_u16() {
Ok(match status.as_u16() {
200 => {
let resp_text: B50Response = response.json().unwrap();
resp_text
Expand All @@ -40,8 +40,7 @@ pub fn get_b50_data(username: &str) -> Result<B50Response, Box<dyn Error>> {
error!("[{}] <-- http 请求错误", status);
exit(exitcode::NOHOST);
}
};
Ok(response)
})
}

pub mod entity {
Expand All @@ -52,7 +51,7 @@ pub mod entity {
use serde::{Deserialize, Serialize};

/// 查分器返回的数据
#[derive(Debug, Serialize, Deserialize)]
#[derive(Serialize, Deserialize)]
pub struct B50Response {
/// 查分器用户名
pub username: String,
Expand All @@ -70,13 +69,13 @@ pub mod entity {
pub user_general_data: Option<String>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Serialize, Deserialize)]
pub struct Charts {
pub dx: Vec<ChartInfoResponse>,
pub sd: Vec<ChartInfoResponse>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Serialize, Deserialize, PartialOrd)]
pub struct ChartInfoResponse {
/// 达成率
pub achievements: f32,
Expand All @@ -91,14 +90,6 @@ pub mod entity {
pub fs: String,
/// 等级
pub level: String,
/// 标记是第几个难度的谱面(感觉跟下面的重复了)
///
/// - `0`: Basic
/// - `1`: Advanced
/// - `2`: Expert
/// - `3`: Master
/// - `4`: Re:Master
pub level_index: i32,
/// 难度标签
pub level_label: LevelLabel,
/// 难度分
Expand All @@ -114,14 +105,14 @@ pub mod entity {
pub song_type: String,
}

#[derive(Clone, Debug, Serialize, Deserialize, Ord, PartialOrd, Eq, PartialEq)]
#[derive(Serialize, Deserialize, Ord, PartialOrd, Eq, PartialEq, Clone)]
pub enum LevelLabel {
Basic,
Advanced,
Expert,
Master,
Basic = 0,
Advanced = 1,
Expert = 2,
Master = 3,
#[serde(rename = "Re:MASTER")]
ReMaster,
ReMaster = 4,
}

impl Display for LevelLabel {
Expand All @@ -138,6 +129,7 @@ pub mod entity {
}

impl LevelLabel {
/// 获取难度等级对应的颜色
pub fn label_color(&self) -> Rgba<u8> {
match self {
LevelLabel::Basic => Rgba([69, 193, 36, 255]),
Expand All @@ -149,9 +141,8 @@ pub mod entity {
}
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(PartialEq, PartialOrd, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[derive(PartialEq, PartialOrd)]
pub enum ChartRate {
D,
C,
Expand Down Expand Up @@ -187,7 +178,7 @@ pub mod entity {
ChartRate::SSS => "SSS",
ChartRate::SSSP => "SSS+",
};
write!(f, "{}", rate_str)
write!(f, "{}", &rate_str)
}
}

Expand Down Expand Up @@ -221,12 +212,6 @@ pub mod entity {
}
}

impl PartialOrd for ChartInfoResponse {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Eq for ChartInfoResponse {}

impl Ord for ChartInfoResponse {
Expand Down
22 changes: 5 additions & 17 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,11 @@ pub mod command {
/// 谱面等级
#[derive(ValueEnum, Clone, Debug)]
pub enum ChartLevel {
BSC,
ADV,
EXP,
MST,
REM,
}

impl ChartLevel {
pub fn get_index(&self) -> usize {
match self {
ChartLevel::BSC => 0,
ChartLevel::ADV => 1,
ChartLevel::EXP => 2,
ChartLevel::MST => 3,
ChartLevel::REM => 4,
}
}
BSC = 0,
ADV = 1,
EXP = 2,
MST = 3,
REM = 4,
}

#[derive(Subcommand, Debug)]
Expand Down
27 changes: 7 additions & 20 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod database {
pub(crate) mod database {
use std::fs;
use std::process::exit;

Expand Down Expand Up @@ -45,14 +45,6 @@ pub mod database {
/// > 解耦合主要是为了方便之后重建索引的步骤
///
/// 这个方法返回的索引注册了 Jieba 分词器
///
/// ```rust
/// let tokenizer_manager = index.tokenizers();
/// let tokenizer = tokenizer_manager.get("jieba");
/// ```
///
/// 可以使用如上的方式获取注册的 tokenizer
///
fn get_index() -> Index {
let tokenizer = tantivy_jieba::JiebaTokenizer {};
let index_path = &CONFIG_PATH.join("data");
Expand Down Expand Up @@ -83,8 +75,12 @@ pub mod database {
.template("{bar:50.green/white} 歌曲数量: {pos}/{len} [{elapsed_precise}]")
.unwrap(),
);

Self::re_create_index();
// 删除原有的索引重建
if CONFIG_PATH.join("data").exists() {
info!("删除原有的索引");
FileUtils::delete_folder_contents(&CONFIG_PATH.join("data")).unwrap();
fs::remove_dir(&CONFIG_PATH.join("data")).unwrap();
}
let mut writer = Self::get_writer();
for song in songs {
let document = match song.document() {
Expand All @@ -105,15 +101,6 @@ pub mod database {
progress_bar.finish();
}

/// 删除原有的索引重新建立
fn re_create_index() {
if CONFIG_PATH.join("data").exists() {
info!("删除原有的索引");
FileUtils::delete_folder_contents(&CONFIG_PATH.join("data")).unwrap();
fs::remove_dir(&CONFIG_PATH.join("data")).unwrap();
}
}

/// 按照传入的 ID 查询歌曲,精确查询
pub fn search_song_by_id(id: usize) -> Option<Song> {
let searcher = Self::get_searcher(&Self::get_index());
Expand Down
1 change: 0 additions & 1 deletion src/image/mod.rs

This file was deleted.

Loading

0 comments on commit 1e274e9

Please sign in to comment.