src/global/functions/find_library.rs

30 lines · 1005 bytes

1 symbol in this file
 1use std::{fs, path::PathBuf};
 2
 3use crate::config::MainConfig;
 4
 5use super::paths;
 6
 7pub fn find_library_item(id: &str, mainconfig: &MainConfig) -> Option<PathBuf> {
 8    let save_path_str = mainconfig.env.get("save-path")?;
 9
10    // Resolve save-path: handle tilde expansion for backward compatibility
11    let save_path = if save_path_str.starts_with("~/") || save_path_str.starts_with("~\\") {
12        dirs::home_dir()
13            .unwrap()
14            .join(&save_path_str[2..])
15    } else {
16        // If it's an absolute path (e.g., from paths::default_save_path()), use directly
17        let p = PathBuf::from(save_path_str);
18        if p.is_absolute() {
19            p
20        } else {
21            // Relative path: treat as relative to data_dir for safety
22            paths::data_dir().join(save_path_str)
23        }
24    };
25
26    fs::read_dir(save_path)
27        .ok()?
28        .filter_map(|entry| Some(entry.ok()?.path()))
29        .find(|stem| stem.as_os_str().to_str().unwrap_or_default().contains(id))
30}