This commit is contained in:
2026-01-28 23:57:04 +08:00
parent 1bb2ca0f40
commit 8caed3c8b4
6 changed files with 416 additions and 27 deletions

View File

@@ -1,9 +1,12 @@
use ::log::{debug, error, info, trace, warn};
use encoding_rs::Encoding;
use tokio::runtime::Runtime;
use crate::utils::cxx_string_to_string;
mod log;
mod network;
mod config;
pub mod utils;
#[cxx::bridge]
mod ffi {
@@ -76,30 +79,4 @@ fn http_get(url: &cxx::CxxString) -> Result<String, Box<dyn std::error::Error>>
rt.block_on(network::http_get(&url))
}
fn cxx_string_to_string(s: &cxx::CxxString) -> String {
match s.to_str() {
Ok(s) => return s.to_string(),
Err(_) => {}
};
// 不是UTF-8尝试转换
let candidates = [
"gb18030", // 覆盖 GBK/GB2312 的常见场景
"windows-1252", // 西欧常见
"shift_jis", // 日文常见
"big5", // 繁体中文常见
];
let bytes = s.as_bytes();
for label in candidates {
let enc = Encoding::for_label(label.as_bytes()).unwrap();
let (cow, _actual_used, had_errors) = enc.decode(bytes);
if !had_errors {
return cow.into_owned();
}
}
/// 编码全部没有命中的话,则丢弃无法解析的部分
s.to_string_lossy().into_owned()
}