use crate::types::FriendOption;
use puniyu_common::read_config;
use puniyu_path::config_dir;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::LazyLock;
static CONFIG_PATH: LazyLock<PathBuf> = LazyLock::new(|| config_dir().join("friend.toml"));
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct FriendConfig {
#[serde(default)]
global: FriendOption,
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
friend: HashMap<String, FriendOption>,
}
impl FriendConfig {
pub fn get() -> Self {
use crate::ConfigRegistry;
ConfigRegistry::get(CONFIG_PATH.as_path()).and_then(|v| v.try_into().ok()).unwrap_or_else(
|| read_config::<Self>(config_dir().as_path(), "friend").unwrap_or_default(),
)
}
pub fn global(&self) -> &FriendOption {
&self.global
}
pub fn friend(&self, user_id: &str) -> FriendOption {
self.friend
.get(user_id)
.map(|specific| crate::common::MergeWith::merge_with(specific, &self.global))
.unwrap_or_else(|| self.global.clone())
}
pub fn list(&self) -> Vec<FriendOption> {
self.friend
.values()
.map(|specific| crate::common::MergeWith::merge_with(specific, &self.global))
.collect()
}
}
impl crate::Config for FriendConfig {
fn name(&self) -> &str {
"friend"
}
fn to_value(&self) -> toml::Value {
crate::serialize_to_value(self)
}
}