fix: reduce unwrap

This commit is contained in:
GyDi 2022-07-30 23:32:52 +08:00
parent a9bf32919e
commit 60f6587169
No known key found for this signature in database
GPG Key ID: 1C95E0D3467B3084

View File

@ -1,4 +1,5 @@
use crate::utils::{dirs, tmpl};
use anyhow::Result;
use chrono::Local;
use log::LevelFilter;
use log4rs::append::console::ConsoleAppender;
@ -11,7 +12,7 @@ use std::path::PathBuf;
use tauri::PackageInfo;
/// initialize this instance's log file
fn init_log(log_dir: &PathBuf) {
fn init_log(log_dir: &PathBuf) -> Result<()> {
let local_time = Local::now().format("%Y-%m-%d-%H%M%S").to_string();
let log_file = format!("{}.log", local_time);
let log_file = log_dir.join(log_file);
@ -22,8 +23,7 @@ fn init_log(log_dir: &PathBuf) {
.build();
let tofile = FileAppender::builder()
.encoder(Box::new(PatternEncoder::new(time_format)))
.build(log_file)
.unwrap();
.build(log_file)?;
let config = Config::builder()
.appender(Appender::builder().build("stdout", Box::new(stdout)))
@ -34,14 +34,15 @@ fn init_log(log_dir: &PathBuf) {
.additive(false)
.build("app", LevelFilter::Info),
)
.build(Root::builder().appender("stdout").build(LevelFilter::Info))
.unwrap();
.build(Root::builder().appender("stdout").build(LevelFilter::Info))?;
log4rs::init_config(config).unwrap();
log4rs::init_config(config)?;
Ok(())
}
/// Initialize all the files from resources
fn init_config(app_dir: &PathBuf) -> std::io::Result<()> {
fn init_config(app_dir: &PathBuf) -> Result<()> {
// target path
let clash_path = app_dir.join("config.yaml");
let verge_path = app_dir.join("verge.yaml");
@ -69,25 +70,24 @@ pub fn init_app(package_info: &PackageInfo) {
let res_dir = dirs::app_resources_dir(package_info);
if !app_dir.exists() {
fs::create_dir_all(&app_dir).unwrap();
let _ = fs::create_dir_all(&app_dir);
}
if !log_dir.exists() {
fs::create_dir_all(&log_dir).unwrap();
let _ = fs::create_dir_all(&log_dir);
}
if !profiles_dir.exists() {
fs::create_dir_all(&profiles_dir).unwrap();
let _ = fs::create_dir_all(&profiles_dir);
}
init_log(&log_dir);
if let Err(err) = init_config(&app_dir) {
log::error!(target: "app", "{err}");
}
crate::log_if_err!(init_log(&log_dir));
crate::log_if_err!(init_config(&app_dir));
// copy the resource file
let mmdb_path = app_dir.join("Country.mmdb");
let mmdb_tmpl = res_dir.join("Country.mmdb");
if !mmdb_path.exists() && mmdb_tmpl.exists() {
fs::copy(mmdb_tmpl, mmdb_path).unwrap();
let _ = fs::copy(mmdb_tmpl, mmdb_path);
}
// copy the wintun.dll
@ -96,7 +96,7 @@ pub fn init_app(package_info: &PackageInfo) {
let wintun_path = app_dir.join("wintun.dll");
let wintun_tmpl = res_dir.join("wintun.dll");
if !wintun_path.exists() && wintun_tmpl.exists() {
fs::copy(wintun_tmpl, wintun_path).unwrap();
let _ = fs::copy(wintun_tmpl, wintun_path);
}
}
}