wx_core/helpers/
mod.rs

1#![doc = "辅助函数"]
2pub use self::lazy_xml::LazyXML;
3use crate::{WxError, WxResult};
4use std::{
5    collections::BTreeMap,
6    fs::read_dir,
7    path::{Path, PathBuf},
8};
9use url::Url;
10
11mod lazy_xml;
12
13/// 获取微信主目录
14pub fn get_wechat_path(given: &Option<String>) -> WxResult<PathBuf> {
15    let path = match given {
16        Some(wechat_path) => PathBuf::from(wechat_path),
17        None => match dirs::document_dir() {
18            Some(s) => s.join("WeChat Files"),
19            None => Err(WxError::custom("fail to get document directory"))?,
20        },
21    };
22    if path.exists() {
23        if !path.is_dir() {
24            Err(WxError::custom("指定的微信主目录不是文件夹,请检查。"))?;
25        }
26    }
27    else {
28        Err(WxError::custom(format!("指定的微信主目录不存在,请检查。{:?}", path.display())))?;
29    }
30    Ok(path)
31}
32/// 读取数据库
33pub async fn read_database(wechat_path: &Option<String>) -> WxResult<BTreeMap<String, PathBuf>> {
34    let mut map = BTreeMap::new();
35    let wechat_path_buf = get_wechat_path(wechat_path)?;
36
37    for entity in read_dir(wechat_path_buf)? {
38        let entity = entity?;
39        if entity.file_name() == "All Users" || entity.file_name() == "Applet" || entity.file_name() == "WMPF" {
40            continue;
41        }
42        if entity.file_type()?.is_dir() {
43            match entity.file_name().into_string() {
44                Ok(o) => {
45                    map.insert(o, entity.path().join("Msg"));
46                }
47                Err(_) => {}
48            }
49        }
50    }
51    Ok(map)
52}
53/// 显示 url 路径
54pub fn url_display(path: &Path, fmt: fn(Url)) {
55    match Url::from_file_path(path) {
56        Ok(o) => fmt(o),
57        Err(_) => {}
58    }
59}