refactor: use anyhow to handle error

This commit is contained in:
GyDi
2022-02-28 01:34:25 +08:00
parent ade34f5217
commit dbf380a0d1
9 changed files with 224 additions and 193 deletions

View File

@@ -1,3 +1,4 @@
use anyhow::{Context, Result};
use serde::{de::DeserializeOwned, Serialize};
use std::{fs, path::PathBuf};
@@ -7,26 +8,16 @@ pub fn read_yaml<T: DeserializeOwned + Default>(path: PathBuf) -> T {
serde_yaml::from_str::<T>(&yaml_str).unwrap_or(T::default())
}
/// - save the data to the file
/// - can set `prefix` string to add some comments
pub fn save_yaml<T: Serialize>(
path: PathBuf,
data: &T,
prefix: Option<&str>,
) -> Result<(), String> {
match serde_yaml::to_string(data) {
Ok(data_str) => {
let yaml_str = match prefix {
Some(prefix) => format!("{}{}", prefix, data_str),
None => data_str,
};
/// save the data to the file
/// can set `prefix` string to add some comments
pub fn save_yaml<T: Serialize>(path: PathBuf, data: &T, prefix: Option<&str>) -> Result<()> {
let data_str = serde_yaml::to_string(data)?;
let path_str = path.as_os_str().to_string_lossy().to_string();
match fs::write(path, yaml_str.as_bytes()) {
Ok(_) => Ok(()),
Err(_) => Err(format!("can not save file `{}`", path_str)),
}
}
Err(_) => Err("can not convert the data to yaml".into()),
}
let yaml_str = match prefix {
Some(prefix) => format!("{prefix}{data_str}"),
None => data_str,
};
let path_str = path.as_os_str().to_string_lossy().to_string();
fs::write(path, yaml_str.as_bytes()).context(format!("failed to save file \"{path_str}\""))
}

View File

@@ -1,3 +1,4 @@
use std::env::temp_dir;
use std::path::{Path, PathBuf};
use tauri::{
api::path::{home_dir, resource_dir},
@@ -18,3 +19,34 @@ pub fn app_resources_dir(package_info: &PackageInfo) -> PathBuf {
.unwrap()
.join("resources")
}
/// profiles dir
pub fn app_profiles_dir() -> PathBuf {
app_home_dir().join("profiles")
}
/// logs dir
pub fn app_logs_dir() -> PathBuf {
app_home_dir().join("logs")
}
static CLASH_CONFIG: &str = "config.yaml";
static VERGE_CONFIG: &str = "verge.yaml";
static PROFILE_YAML: &str = "profiles.yaml";
static PROFILE_TEMP: &str = "clash-verge-runtime.yaml";
pub fn clash_path() -> PathBuf {
app_home_dir().join(CLASH_CONFIG)
}
pub fn verge_path() -> PathBuf {
app_home_dir().join(VERGE_CONFIG)
}
pub fn profiles_path() -> PathBuf {
app_home_dir().join(PROFILE_YAML)
}
pub fn profiles_temp_path() -> PathBuf {
temp_dir().join(PROFILE_TEMP)
}

View File

@@ -1,5 +1,3 @@
extern crate serde_yaml;
use crate::utils::{dirs, tmpl};
use chrono::Local;
use log::LevelFilter;
@@ -7,7 +5,7 @@ use log4rs::append::console::ConsoleAppender;
use log4rs::append::file::FileAppender;
use log4rs::config::{Appender, Config, Root};
use log4rs::encode::pattern::PatternEncoder;
use std::fs::{self, File};
use std::fs;
use std::io::Write;
use std::path::PathBuf;
use tauri::PackageInfo;
@@ -48,13 +46,13 @@ fn init_config(app_dir: &PathBuf) -> std::io::Result<()> {
let profile_path = app_dir.join("profiles.yaml");
if !clash_path.exists() {
File::create(clash_path)?.write(tmpl::CLASH_CONFIG)?;
fs::File::create(clash_path)?.write(tmpl::CLASH_CONFIG)?;
}
if !verge_path.exists() {
File::create(verge_path)?.write(tmpl::VERGE_CONFIG)?;
fs::File::create(verge_path)?.write(tmpl::VERGE_CONFIG)?;
}
if !profile_path.exists() {
File::create(profile_path)?.write(tmpl::PROFILES_CONFIG)?;
fs::File::create(profile_path)?.write(tmpl::PROFILES_CONFIG)?;
}
Ok(())
}
@@ -63,8 +61,8 @@ fn init_config(app_dir: &PathBuf) -> std::io::Result<()> {
pub fn init_app(package_info: &PackageInfo) {
// create app dir
let app_dir = dirs::app_home_dir();
let log_dir = app_dir.join("logs");
let profiles_dir = app_dir.join("profiles");
let log_dir = dirs::app_logs_dir();
let profiles_dir = dirs::app_profiles_dir();
let res_dir = dirs::app_resources_dir(package_info);