Commit 84ee3188 authored by Chunchi Che's avatar Chunchi Che

add strings_conf_reader

parent a0b01b0c
Pipeline #18769 failed with stages
in 2 minutes and 55 seconds
use std::{collections::HashMap, net::SocketAddr, str::FromStr}; use std::{net::SocketAddr, str::FromStr};
use warp::Filter; use warp::Filter;
mod infra; mod infra;
...@@ -11,7 +11,10 @@ const SERVE_ADDR: &str = "127.0.0.1:3030"; ...@@ -11,7 +11,10 @@ const SERVE_ADDR: &str = "127.0.0.1:3030";
async fn main() -> anyhow::Result<()> { async fn main() -> anyhow::Result<()> {
pretty_env_logger::try_init()?; pretty_env_logger::try_init()?;
let strings_manager = HashMap::new(); let workspace = env!("CARGO_MANIFEST_DIR");
let strings_manager = service::strings_conf_reader(format!(
"{workspace}/ygopro-database/locales/zh-CN/strings.conf"
))?;
// TODO: use CORS correctly // TODO: use CORS correctly
let cors = warp::cors() let cors = warp::cors()
......
...@@ -4,4 +4,4 @@ mod strings; ...@@ -4,4 +4,4 @@ mod strings;
pub use cards::service as cards_service; pub use cards::service as cards_service;
pub use deck::service as deck_service; pub use deck::service as deck_service;
pub use strings::service_maker as strings_service; pub use strings::{service_maker as strings_service, strings_conf_reader};
use std::collections::HashMap; use std::{
collections::HashMap,
fs::File,
io::{BufRead, BufReader},
path::Path,
};
type StringsManager = HashMap<String, HashMap<i64, String>>;
// TODO: `strings_manager`应该做好持久化存储 // TODO: `strings_manager`应该做好持久化存储
pub fn service_maker( pub fn service_maker(strings_manager: StringsManager) -> impl Fn(String) -> String + Clone {
strings_manager: HashMap<String, HashMap<i64, String>>,
) -> impl Fn(String) -> String + Clone {
move |param| { move |param| {
// TODO:应该做好错误处理 // TODO:应该做好错误处理
let (r#type, code) = param.split_once('_').unwrap_or_default(); let (r#type, code) = param.split_once('_').unwrap_or_default();
...@@ -15,3 +20,34 @@ pub fn service_maker( ...@@ -15,3 +20,34 @@ pub fn service_maker(
.unwrap_or_default() .unwrap_or_default()
} }
} }
pub fn strings_conf_reader(path: impl AsRef<Path>) -> anyhow::Result<StringsManager> {
let f = File::open(path)?;
let reader = BufReader::new(f);
let mut results: StringsManager = HashMap::new();
for line in reader.lines() {
let line = line?;
if !line.starts_with("#") {
if let Some((r#type, left)) = line.split_once(' ') {
if let Some((code, s)) = left.split_once(' ') {
results
.entry(r#type.to_string())
.or_default()
.insert(parse_code(code)?, s.to_string());
}
}
}
}
Ok(results)
}
fn parse_code(code: &str) -> anyhow::Result<i64> {
if code.starts_with("0x") {
let without_prefix = code.trim_start_matches("0x");
Ok(i64::from_str_radix(without_prefix, 16)?)
} else {
Ok(code.parse()?)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment