From 08438264352c4c1709ed3cf853fd5632d59b72c3 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Wed, 13 Mar 2024 01:20:41 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E4=B8=80=E4=B8=AA=20clicppy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ica-rs/src/config.rs | 10 +++++----- ica-rs/src/data_struct/ica/online_data.rs | 10 +++++----- ica-rs/src/ica.rs | 2 +- ica-rs/src/ica/client.rs | 6 +++++- ica-rs/src/ica/events.rs | 7 +++---- ica-rs/src/main.rs | 2 +- ica-rs/src/py/call.rs | 14 ++++++-------- ica-rs/src/py/class.rs | 4 ++++ ica-rs/src/py/mod.rs | 16 ++++++++-------- 9 files changed, 38 insertions(+), 33 deletions(-) diff --git a/ica-rs/src/config.rs b/ica-rs/src/config.rs index d707001..14b0f46 100644 --- a/ica-rs/src/config.rs +++ b/ica-rs/src/config.rs @@ -61,7 +61,7 @@ impl BotConfig { // try read config from file let config = fs::read_to_string(&config_file_path).expect("Failed to read config file"); let ret: Self = from_str(&config) - .expect(format!("Failed to parse config file {}", &config_file_path).as_str()); + .unwrap_or_else(|_| panic!("Failed to parse config file {}", &config_file_path)); ret } pub fn new_from_cli() -> Self { @@ -74,7 +74,7 @@ impl BotConfig { match self.enable_ica { Some(enable) => { if enable { - if let None = self.ica { + if self.ica.is_none() { warn!("enable_ica 为 true 但未填写 [ica] 配置\n将不启用 ica"); false } else { @@ -85,7 +85,7 @@ impl BotConfig { } } None => { - if let Some(_) = self.ica { + if self.ica.is_some() { warn!("未填写 enable_ica 但填写了 [ica] 配置\n将不启用 ica"); } false @@ -98,7 +98,7 @@ impl BotConfig { match self.enable_matrix { Some(enable) => { if enable { - if let None = self.matrix { + if self.matrix.is_none() { warn!("enable_matrix 为 true 但未填写 [matrix] 配置\n将不启用 Matrix"); false } else { @@ -109,7 +109,7 @@ impl BotConfig { } } None => { - if let Some(_) = self.matrix { + if self.matrix.is_some() { warn!("未填写 enable_matrix 但填写了 [matrix] 配置\n将不启用 Matrix"); } false diff --git a/ica-rs/src/data_struct/ica/online_data.rs b/ica-rs/src/data_struct/ica/online_data.rs index 8f9ef62..c832fd9 100644 --- a/ica-rs/src/data_struct/ica/online_data.rs +++ b/ica-rs/src/data_struct/ica/online_data.rs @@ -24,7 +24,7 @@ impl IcalinguaInfo { let mut load = None; let mut server_node = None; let mut client_count = None; - let info_list = s.split("\n").collect::>(); + let info_list = s.split('\n').collect::>(); for info in info_list { if info.starts_with("icalingua-bridge-oicq") { ica_version = Some(info.split_at(22).1.to_string()); @@ -40,9 +40,9 @@ impl IcalinguaInfo { server_node = Some(info.split_at(12).1.to_string()); } else if info.ends_with("clients connected") { client_count = Some( - info.split(" ") + info.split(' ') .collect::>() - .get(0) + .first() .unwrap_or(&"1") .parse::() .unwrap_or_else(|e| { @@ -194,7 +194,7 @@ mod tests { })); assert_eq!(online_data.bkn, 123); assert_eq!(online_data.nick, "test"); - assert_eq!(online_data.online, true); + assert!(online_data.online); assert_eq!(online_data.qqid, 123456); assert_eq!(online_data.icalingua_info.ica_version, "2.11.1"); assert_eq!(online_data.icalingua_info.os_info, "Linux c038fad79f13 4.4.302+"); @@ -210,7 +210,7 @@ mod tests { let online_data = OnlineData::new_from_json(&serde_json::json!({})); assert_eq!(online_data.bkn, -1); assert_eq!(online_data.nick, "UNKNOWN"); - assert_eq!(online_data.online, false); + assert!(!online_data.online); assert_eq!(online_data.qqid, -1); assert_eq!(online_data.icalingua_info.ica_version, "UNKNOWN"); assert_eq!(online_data.icalingua_info.os_info, "UNKNOWN"); diff --git a/ica-rs/src/ica.rs b/ica-rs/src/ica.rs index 3933e5d..43afaea 100644 --- a/ica-rs/src/ica.rs +++ b/ica-rs/src/ica.rs @@ -34,7 +34,7 @@ pub async fn start_ica(config: &IcaConfig, stop_reciver: tokio::sync::oneshot::R for room in config.notice_room.iter() { let startup_msg = crate::data_struct::ica::messages::SendMessage::new( format!("ica-async-rs bot v{}", crate::VERSION), - room.clone(), + *room, None, ); tokio::time::sleep(std::time::Duration::from_secs(1)).await; diff --git a/ica-rs/src/ica/client.rs b/ica-rs/src/ica/client.rs index 0334de0..789f9d5 100644 --- a/ica-rs/src/ica/client.rs +++ b/ica-rs/src/ica/client.rs @@ -56,6 +56,10 @@ pub struct BotStatus { pub config: Option, } +impl Default for BotStatus { + fn default() -> Self { Self::new() } +} + impl BotStatus { pub fn new() -> Self { Self { @@ -139,7 +143,7 @@ pub async fn sign_callback(payload: Payload, client: Client) { let (auth_key, version) = (&require_data[0], &require_data[1]); debug!("auth_key: {:?}, version: {:?}", auth_key, version); - let auth_key = match &require_data.get(0) { + let auth_key = match &require_data.first() { Some(Value::String(auth_key)) => Some(auth_key), _ => None, } diff --git a/ica-rs/src/ica/events.rs b/ica-rs/src/ica/events.rs index 3e972df..4c1391f 100644 --- a/ica-rs/src/ica/events.rs +++ b/ica-rs/src/ica/events.rs @@ -62,7 +62,7 @@ pub async fn delete_message(payload: Payload, client: Client) { // 消息 id if let Some(value) = values.first() { if let Some(msg_id) = value.as_str() { - info!("delete_message {}", format!("{}", msg_id).yellow()); + info!("delete_message {}", msg_id.to_string().yellow()); py::call::delete_message_py(msg_id.to_string(), &client).await; } @@ -74,8 +74,7 @@ pub async fn update_all_room(payload: Payload, _client: Client) { if let Payload::Text(values) = payload { if let Some(value) = values.first() { if let Some(raw_rooms) = value.as_array() { - let rooms: Vec = - raw_rooms.iter().map(|room| Room::new_from_json(room)).collect(); + let rooms: Vec = raw_rooms.iter().map(Room::new_from_json).collect(); BotStatus::update_rooms(rooms.clone()); info!("update_all_room {}", rooms.len()); } @@ -151,7 +150,7 @@ pub async fn any_event(event: Event, payload: Payload, _client: Client) { Payload::Text(ref data) => { print!("event: {}", event.as_str().purple()); for value in data { - println!("|{}", value.to_string()); + println!("|{}", value); } } _ => (), diff --git a/ica-rs/src/main.rs b/ica-rs/src/main.rs index 2905cc7..e3dd512 100644 --- a/ica-rs/src/main.rs +++ b/ica-rs/src/main.rs @@ -8,7 +8,7 @@ mod ica; mod matrix; mod py; -use config::{BotConfig, IcaConfig}; +use config::BotConfig; use tracing::info; #[allow(non_upper_case_globals)] diff --git a/ica-rs/src/py/call.rs b/ica-rs/src/py/call.rs index 9e524b5..236a7f2 100644 --- a/ica-rs/src/py/call.rs +++ b/ica-rs/src/py/call.rs @@ -46,19 +46,17 @@ pub fn get_func<'py>(py_module: &'py PyAny, path: &PathBuf, name: &'py str) -> O pub fn verify_plugins() { let mut need_reload_files: Vec = Vec::new(); let plugin_path = BotStatus::get_config().py_plugin_path.as_ref(); - if let None = plugin_path { + if plugin_path.is_none() { warn!("未配置 Python 插件路径"); return; } let plugin_path = plugin_path.unwrap(); - for entry in std::fs::read_dir(&plugin_path).unwrap() { + for entry in std::fs::read_dir(plugin_path).unwrap() { if let Ok(entry) = entry { let path = entry.path(); if let Some(ext) = path.extension() { - if ext == "py" { - if !PyStatus::verify_file(&path) { - need_reload_files.push(path); - } + if ext == "py" && !PyStatus::verify_file(&path) { + need_reload_files.push(path); } } } @@ -94,7 +92,7 @@ pub async fn new_message_py(message: &NewMessage, client: &Client) { // 甚至实际上压根不需要await这个spawn, 直接让他自己跑就好了(离谱) tokio::spawn(async move { Python::with_gil(|py| { - if let Some(py_func) = get_func(plugin.py_module.as_ref(py), &path, "on_message") { + if let Some(py_func) = get_func(plugin.py_module.as_ref(py), path, "on_message") { if let Err(e) = py_func.call1(args) { warn!("failed to call function: {:?}", e); } @@ -115,7 +113,7 @@ pub async fn delete_message_py(msg_id: MessageId, client: &Client) { tokio::spawn(async move { Python::with_gil(|py| { if let Some(py_func) = - get_func(plugin.py_module.as_ref(py), &path, "on_delete_message") + get_func(plugin.py_module.as_ref(py), path, "on_delete_message") { if let Err(e) = py_func.call1(args) { warn!("failed to call function: {:?}", e); diff --git a/ica-rs/src/py/class.rs b/ica-rs/src/py/class.rs index 648e420..cb96ed7 100644 --- a/ica-rs/src/py/class.rs +++ b/ica-rs/src/py/class.rs @@ -53,6 +53,10 @@ impl IcaStatusPy { pub fn get_load(&self) -> String { BotStatus::get_online_data().icalingua_info.load.clone() } } +impl Default for IcaStatusPy { + fn default() -> Self { Self::new() } +} + impl IcaStatusPy { pub fn new() -> Self { Self {} } } diff --git a/ica-rs/src/py/mod.rs b/ica-rs/src/py/mod.rs index 8eb4aa9..24707f1 100644 --- a/ica-rs/src/py/mod.rs +++ b/ica-rs/src/py/mod.rs @@ -8,7 +8,7 @@ use pyo3::prelude::*; use pyo3::types::PyTuple; use tracing::{debug, info, warn}; -use crate::config::{BotConfig, IcaConfig}; +use crate::config::BotConfig; use crate::ica::client::BotStatus; #[derive(Debug, Clone)] @@ -28,7 +28,7 @@ pub struct PyPlugin { impl PyPlugin { pub fn new_from_path(path: &PathBuf) -> Option { - let raw_file = load_py_file(&path); + let raw_file = load_py_file(path); match raw_file { Ok(raw_file) => match Self::try_from(raw_file) { Ok(plugin) => Some(plugin), @@ -122,9 +122,9 @@ impl TryFrom for PyPlugin { "加载 Python 插件 {:?} 的配置文件信息时失败:返回的不是 [str, str]", path ); - Err(PyErr::new::(format!( - "返回的不是 [str, str]" - ))) + Err(PyErr::new::( + "返回的不是 [str, str]".to_string(), + )) } } Err(e) => { @@ -186,7 +186,7 @@ impl PyStatus { pub static mut PYSTATUS: PyStatus = PyStatus { files: None }; -pub fn load_py_plugins(path: &PathBuf) -> () { +pub fn load_py_plugins(path: &PathBuf) { if path.exists() { info!("finding plugins in: {:?}", path); // 搜索所有的 py 文件 和 文件夹单层下面的 py 文件 @@ -226,7 +226,7 @@ pub fn py_module_from_code(content: &str, path: &PathBuf) -> PyResult> Python::with_gil(|py| -> PyResult> { let module: PyResult> = PyModule::from_code( py, - &content, + content, &path.to_string_lossy(), &path.file_name().unwrap().to_string_lossy(), // !!!! 请注意, 一定要给他一个名字, cpython 会自动把后面的重名模块覆盖掉前面的 @@ -239,7 +239,7 @@ pub fn py_module_from_code(content: &str, path: &PathBuf) -> PyResult> /// 传入文件路径 /// 返回 hash 和 文件内容 pub fn load_py_file(path: &PathBuf) -> std::io::Result { - let changed_time = get_change_time(&path); + let changed_time = get_change_time(path); let content = std::fs::read_to_string(path)?; Ok((path.clone(), changed_time, content)) }