完成了基本命令部分,matrix登陆在测试中
This commit is contained in:
1 parent
bbbdd9eac5
commit
ad69931573
6 files changed
+310
-29
No files matched your search
+44
-2
@@ -1,8 +1,9 @@
|
||||
use matrix_sdk::ruma::{OwnedRoomId, OwnedUserId, UserId};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{path::Path, sync::OnceLock};
|
||||
|
||||
static CONFIG: OnceLock<Config> = OnceLock::new();
|
||||
pub fn get_config() -> &'static Config {
|
||||
fn get_config() -> &'static Config {
|
||||
CONFIG.get_or_init(|| {
|
||||
toml::from_str(&std::fs::read_to_string(Path::new("config.toml")).expect("无法读配置文件"))
|
||||
.expect("解析配置文件时发生错误")
|
||||
@@ -14,9 +15,50 @@ pub struct Config {
|
||||
pub matrix: Option<MatrixBot>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct MatrixBot {
|
||||
pub home_server: String,
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
pub rooms: Vec<Room>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct Room {
|
||||
pub room_id: OwnedRoomId,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub kick_member_if_they_didnot_checkin_for_long_time: Option<bool>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub kick_long_time: Option<isize>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub white_list: Option<Vec<OwnedUserId>>,
|
||||
}
|
||||
use thiserror::Error;
|
||||
#[derive(Debug, Error)]
|
||||
enum ConfigError {
|
||||
#[error("{0} is not exist")]
|
||||
NonExixtent(String),
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn get_all() -> &'static Self {
|
||||
get_config()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::config::Room;
|
||||
|
||||
#[test]
|
||||
fn room_test() {
|
||||
let cfg = r#"room_id="!example:foo.bar"
|
||||
kick_member_if_they_didnot_checkin_for_long_time=true
|
||||
kick_long_time=1
|
||||
white_list=["@example:foo.bar"]
|
||||
"#;
|
||||
dbg!(&cfg);
|
||||
let a: Room = toml::from_str::<Room>(cfg).unwrap();
|
||||
dbg!(&a);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user