allow custom path from config.toml

This commit is contained in:
yell0wsuit 2024-07-05 13:08:38 +07:00
commit 494e4fddde
No known key found for this signature in database
GPG key ID: 5B4F198A9800F6F4
2 changed files with 27 additions and 5 deletions

View file

@ -9,3 +9,4 @@ license = "MIT"
miniz_oxide = { version = "0.7", default-features = false }
miniserde = "0.1"
xmlparser = "0.13.5"
toml = "0.5"

View file

@ -4,6 +4,7 @@ use std::{
fs,
path::{Path, PathBuf},
};
use toml::Value;
use crate::{media::Media, key::Keys, pages::Pages, Error};
@ -33,11 +34,31 @@ pub struct Paths {
contents_dir: String,
}
impl AsRef<Path> for Paths {
fn as_ref(&self) -> &Path {
&self.base_path
}
}
impl Paths {
fn std_list_path() -> PathBuf {
PathBuf::from(
"/Library/Application Support/AppStoreContent/jp.monokakido.Dictionaries/Products/",
)
fn read_config() -> Result<String, Box<dyn std::error::Error>> {
let config_str = fs::read_to_string("config.toml")?;
let config: Value = toml::from_str(&config_str)?;
let dict_path = config["dict_path"].as_str()
.ok_or("dict_path not found or not a string")?
.to_string();
Ok(dict_path)
}
fn list_path() -> PathBuf {
read_config()
.map(PathBuf::from)
.unwrap_or_else(|_| {
// Fallback to default path if config reading fails
PathBuf::from("/Library/Application Support/AppStoreContent/jp.monokakido.Dictionaries/Products/")
})
}
fn std_dict_path(name: &str) -> PathBuf {
@ -134,7 +155,7 @@ impl MonokakidoDict {
let pages = Pages::new(&paths)?;
let audio = Media::new(&paths)?;
let graphics = Media::new(&paths)?;
let keys = Keys::new(&paths)?;
let keys = Keys::new(paths.contents_path())?;
Ok(MonokakidoDict {
paths,