Skip to main content

kovi/
utils.rs

1use serde::Serialize;
2use serde::de::DeserializeOwned;
3use std::fs::{self, File};
4use std::io::{Read, Write};
5use std::path::{Path, PathBuf};
6
7fn save_data(data: &[u8], file_path: &Path) -> Result<(), Box<dyn std::error::Error>> {
8    if let Some(parent) = file_path.parent()
9        && !parent.exists()
10    {
11        fs::create_dir_all(parent)?;
12    }
13
14    let mut file = File::create(file_path)?;
15    file.write_all(data)?;
16
17    Ok(())
18}
19
20/// 加载本地json数据,如果没有则保存传入的数据进指定路径
21pub fn load_json_data<T, P>(data: T, file_path: P) -> Result<T, Box<dyn std::error::Error>>
22where
23    T: Serialize + DeserializeOwned,
24    P: AsRef<Path>,
25{
26    if !file_path.as_ref().exists() {
27        let serialized_data = serde_json::to_string(&data)?;
28        save_data(serialized_data.as_bytes(), file_path.as_ref())?;
29        return Ok(data);
30    }
31
32    let mut file = File::open(&file_path)?;
33    let mut contents = String::new();
34    file.read_to_string(&mut contents)?;
35    let deserialized_data = serde_json::from_str(&contents)?;
36
37    Ok(deserialized_data)
38}
39
40/// 加载本地toml数据,如果没有则保存传入的数据进指定路径
41pub fn load_toml_data<T, P>(data: T, file_path: P) -> Result<T, Box<dyn std::error::Error>>
42where
43    T: Serialize + DeserializeOwned,
44    P: AsRef<Path>,
45{
46    if !file_path.as_ref().exists() {
47        let serialized_data = toml::to_string(&data)?;
48        save_data(serialized_data.as_bytes(), file_path.as_ref())?;
49        return Ok(data);
50    }
51
52    let mut file = File::open(file_path)?;
53    let mut contents = String::new();
54    file.read_to_string(&mut contents)?;
55    let deserialized_data = toml::from_str(&contents)?;
56
57    Ok(deserialized_data)
58}
59
60/// 将json数据保存在传入的地址
61pub fn save_json_data<T, P>(data: &T, file_path: P) -> Result<(), Box<dyn std::error::Error>>
62where
63    T: Serialize,
64    P: AsRef<Path>,
65{
66    let serialized_data = serde_json::to_string(data)?;
67    save_data(serialized_data.as_bytes(), file_path.as_ref())?;
68    Ok(())
69}
70
71/// 将toml数据保存在传入的地址
72pub fn save_toml_data<T, P>(data: &T, file_path: P) -> Result<(), Box<dyn std::error::Error>>
73where
74    T: Serialize,
75    P: AsRef<Path>,
76{
77    let serialized_data = toml::to_string(data)?;
78    save_data(serialized_data.as_bytes(), file_path.as_ref())?;
79    Ok(())
80}
81
82/// 获取插件数据根目录
83///
84/// 如:`bot.get_data_path()` 是 `/path/data/plugin_name/`
85///
86/// 那么这个插件的数据目录就是 `/path/data/`
87pub fn get_data_root_path() -> PathBuf {
88    let mut current_dir = std::env::current_dir().expect("Get current directory failed");
89    current_dir.push("data");
90    current_dir
91}
92
93// /// 计算pskey值
94// pub fn calculate_pskey(skey: &str) -> u32 {
95//     let mut hash: u32 = 5381;
96//     for character in skey.chars() {
97//         hash = (hash << 5)
98//             .wrapping_add(hash)
99//             .wrapping_add(character as u32);
100//     }
101//     hash & 0x7fffffff
102// }