Skip to content

Commit

Permalink
refactor: change conf to figment
Browse files Browse the repository at this point in the history
  • Loading branch information
Itsusinn committed Feb 13, 2024
1 parent 1762d5b commit c0827bf
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/commands/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl BindCommand {
if is_admin {
match CONFIG
.bindings
.insert(chat_id.0, ArcStr::from(address.clone()))
.insert(chat_id.0.to_string(), ArcStr::from(address.clone()))
{
Some(before) => {
bot
Expand Down Expand Up @@ -82,7 +82,7 @@ impl BindCommand {
}
}
if is_admin {
match CONFIG.bindings.remove(&chat_id.0) {
match CONFIG.bindings.remove(&chat_id.0.to_string()) {
Some(before) => {
bot
.send_message(msg.chat.id, "成功解绑当前群组的信使地址".to_string())
Expand Down
2 changes: 1 addition & 1 deletion src/commands/manage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl ManageCommand {
.send_message(msg.chat.id, ManageCommand::descriptions().to_string())
.await?;
}
ManageCommand::NewProfile { name, address } => {}
ManageCommand::NewProfile { name: _, address: _ } => {}
}
Ok(())
}
Expand Down
44 changes: 19 additions & 25 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,46 @@ use std::sync::Arc;
use arcstr::ArcStr;
use color_eyre::eyre::{Error, Result};
use dashmap::DashMap;
use figment_wrapper::{figment_derive, FigmentWrapper};
use mesagisto_client::server::SERVER;
use uuid::Uuid;

#[config_derive]
#[derive(AutomaticConfig)]
#[location = "config/tg.yml"]
#[figment_derive]
#[derive(FigmentWrapper)]
#[location = "config/tg.toml"]
pub struct Config {
#[educe(Default = false)]
pub enable: bool,
#[educe(Default = "")]
pub locale: ArcStr,
// A-z order
pub bindings: DashMap<i64, ArcStr>,
pub bindings: DashMap<String, ArcStr>,
pub cipher: CipherConfig,
pub proxy: ProxyConfig,
pub telegram: TelegramConfig,
pub auto_update: AutoUpdateConfig,
pub tls: TlsConfig,
pub centers: Arc<DashMap<ArcStr, ArcStr>>,
}

impl Config {
pub fn room_address(&self, target: &i64) -> Option<ArcStr> {
self.bindings.get(target).map(|v| v.clone())
self.bindings.get(&target.to_string()).map(|v| v.clone())
}

pub fn room_id(&self, target: i64) -> Option<Arc<Uuid>> {
let room_address = self.room_address(&target)?;
Some(SERVER.room_id(room_address))
}
// pub fn room_id(&self, target: i64) -> Option<Uuid> {
// let room_address = self.room_address(&target)?;
// Some(SERVER.room_id(room_address))
// }

pub fn target_id(&self, room_id: Arc<Uuid>) -> Option<Vec<i64>> {
pub fn target_id(&self, room_id: Uuid) -> Option<Vec<i64>> {
let entry = SERVER.room_map.iter().find(|v| v.value() == &room_id)?;
let room_address = entry.key();
let targets = self
.bindings
.iter()
.filter_map(|v| {
if v.value() == room_address {
Some(v.key().to_owned())
Some(v.key().parse::<i64>().expect("Failed to decode channel id"))
} else {
None
}
Expand All @@ -58,15 +58,15 @@ impl Config {
}

pub fn migrate_chat(&self, old_chat_id: &i64, new_chat_id: &i64) -> bool {
if let Some((_, address)) = self.bindings.remove(old_chat_id) {
self.bindings.insert(*new_chat_id, address);
if let Some((_, address)) = self.bindings.remove(&old_chat_id.to_string()) {
self.bindings.insert(new_chat_id.to_string(), address);
return true;
};
false
}
}

#[config_derive]
#[figment_derive]
pub struct ProxyConfig {
#[educe(Default = false)]
pub enable: bool,
Expand All @@ -75,24 +75,24 @@ pub struct ProxyConfig {
pub address: ArcStr,
}

#[config_derive]
#[figment_derive]
pub struct CipherConfig {
#[educe(Default = "default")]
pub key: ArcStr,
}

#[config_derive]
#[figment_derive]
pub struct TelegramConfig {
#[educe(Default = "BOT_TOKEN")]
pub token: String,
}

#[config_derive]
#[figment_derive]
pub struct FormatConfig {
pub msg: ArcStr,
}

#[config_derive]
#[figment_derive]
pub struct AutoUpdateConfig {
#[educe(Default = true)]
pub enable: bool,
Expand All @@ -101,9 +101,3 @@ pub struct AutoUpdateConfig {
#[educe(Default = false)]
pub no_confirm: bool,
}
#[config_derive]
pub struct TlsConfig {
#[educe(Default = false)]
pub skip_verify: bool,
pub custom_cert: ArcStr,
}
5 changes: 3 additions & 2 deletions src/handlers/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ use crate::{bot::TG_BOT, config::CONFIG, ext::db::DbExt};

pub async fn answer_common(msg: Message) -> Result<()> {
let target = msg.chat.id.0;
if !CONFIG.bindings.contains_key(&target) {
let target_str = msg.chat.id.0.to_string();
if !CONFIG.bindings.contains_key(&target_str) {
return Ok(());
}
let room_address = CONFIG.bindings.get(&target).unwrap().clone();
let room_address = CONFIG.bindings.get(&target_str).unwrap().clone();

let sender = match msg.from() {
Some(v) => v,
Expand Down
2 changes: 0 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ use crate::{
#[macro_use]
extern crate educe;
#[macro_use]
extern crate automatic_config;
#[macro_use]
extern crate singleton;

mod bot;
Expand Down

0 comments on commit c0827bf

Please sign in to comment.