From 76c572d4bf9156149974d3becbe23df31ad944cf Mon Sep 17 00:00:00 2001 From: explosion-mental Date: Mon, 6 Mar 2023 19:48:20 -0500 Subject: [PATCH 1/3] use `cfg!` for (runtime) platform check instead of `whoami` crate Also just use a const for SeParator &str --- src/io_functions.rs | 61 ++++++++++++++++----------------------------- 1 file changed, 21 insertions(+), 40 deletions(-) diff --git a/src/io_functions.rs b/src/io_functions.rs index 12e015e..633a5c4 100644 --- a/src/io_functions.rs +++ b/src/io_functions.rs @@ -3,7 +3,9 @@ use std::{env, fs, io::Write, path::Path}; use anyhow::{bail, Result}; use chrono::prelude::Local; use glob::glob; -use whoami::{platform, Platform}; + +/// SeParator - Only here to handle windows properly +const SP: &str = if cfg!(windows) { "\\" } else { "/" }; pub fn fetch(query: String, lang: String) -> Result { let x = ureq::get("https://google.com/search") @@ -20,24 +22,21 @@ pub fn fetch(query: String, lang: String) -> Result { } pub fn cached_html() -> Result { - let os_type = platform(); - let files = get_file_list(os_type)?; + let files = get_file_list()?; let html = fs::read_to_string(&files[(files.len() - 1)])?; Ok(html) } pub fn save_html(query: &[&str], html: &str) -> Result { - let os_type = platform(); - let cache_path = get_cache_path(&os_type)?; + let cache_path = get_cache_path()?; let file_date = Local::now().format("%s").to_string(); let file_query = query.join("_"); - let sep = sep_type(&os_type); - let mut x: Vec<&str> = vec![&cache_path, sep, "oi"]; + let mut x: Vec<&str> = vec![&cache_path, SP, "oi"]; if !Path::new(&x.join("")).is_dir() { fs::create_dir(&x.join(""))? } - x.push(sep); + x.push(SP); x.push(&file_date); x.push("-"); x.push(&file_query); @@ -50,45 +49,27 @@ pub fn save_html(query: &[&str], html: &str) -> Result { } pub fn clean_cache() -> Result { - let os_type = platform(); - let sep = sep_type(&os_type); - let target = [&get_cache_path(&os_type)?, sep, "oi"].join(""); + let target = [&get_cache_path()?, SP, "oi"].join(""); fs::remove_dir_all(&target)?; Ok(target) } -// this isn't strictly necessary but the mix of slashes on Windows looks messy -fn sep_type(os_type: &Platform) -> &str { - match os_type { - Platform::Windows => "\\", - _ => "/" - } -} - -fn get_cache_path(os_type: &Platform) -> Result { - let cache_path: String = match os_type { - Platform::Bsd | Platform::Linux => match env::var("XDG_CACHE_HOME") { - Ok(x) => x, - Err(_) => { - let home_path = env::var("HOME")?; - let x = [home_path, "/.cache".to_string()]; - x.join("") - } - }, - Platform::MacOS => { - let home_path = env::var("HOME")?; - let x = [home_path, "/Library/Application Support".to_string()]; - x.join("") - } - Platform::Windows => env::var("LOCALAPPDATA")?, - _ => bail!("This feature is not supported on your platform, sorry!") - }; +fn get_cache_path() -> Result { + let cache_path: String = + if cfg!(freebsd) || cfg!(linux) { + env::var("XDG_CACHE_HOME").unwrap_or(env::var("HOME")? + "/.cache") + } else if cfg!(macos) { + env::var("HOME")? + "/Library/Application Support" + } else if cfg!(windows) { + env::var("LOCALAPPDATA")? + } else { + bail!("This feature is not supported on your platform, sorry!") + }; Ok(cache_path) } -fn get_file_list(os_type: Platform) -> Result> { - let sep = sep_type(&os_type); - let cache_path = [&get_cache_path(&os_type)?, sep, "oi", sep, "*.html"].join(""); +fn get_file_list() -> Result> { + let cache_path = [&get_cache_path()?, SP, "oi", SP, "*.html"].join(""); let mut files: Vec = vec![]; for x in glob(&cache_path).unwrap() { From fabf6ef86679b319ab9d4d91a8ad87f305a700ca Mon Sep 17 00:00:00 2001 From: explosion-mental Date: Mon, 6 Mar 2023 19:58:38 -0500 Subject: [PATCH 2/3] fix `cfg!`, specify `target_os` --- src/io_functions.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/io_functions.rs b/src/io_functions.rs index 633a5c4..ba78e2a 100644 --- a/src/io_functions.rs +++ b/src/io_functions.rs @@ -56,11 +56,11 @@ pub fn clean_cache() -> Result { fn get_cache_path() -> Result { let cache_path: String = - if cfg!(freebsd) || cfg!(linux) { + if cfg!(target_os = "freebsd") || cfg!(target_os = "linux") { env::var("XDG_CACHE_HOME").unwrap_or(env::var("HOME")? + "/.cache") - } else if cfg!(macos) { + } else if cfg!(target_os = "macos") { env::var("HOME")? + "/Library/Application Support" - } else if cfg!(windows) { + } else if cfg!(target_os = "windows") { env::var("LOCALAPPDATA")? } else { bail!("This feature is not supported on your platform, sorry!") From 339008e79fb2bb6a2571e367f090c25b3796aa02 Mon Sep 17 00:00:00 2001 From: explosion-mental Date: Mon, 6 Mar 2023 20:00:35 -0500 Subject: [PATCH 3/3] fix `cfg!` on SP as well --- src/io_functions.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/io_functions.rs b/src/io_functions.rs index ba78e2a..0feb5bb 100644 --- a/src/io_functions.rs +++ b/src/io_functions.rs @@ -5,7 +5,7 @@ use chrono::prelude::Local; use glob::glob; /// SeParator - Only here to handle windows properly -const SP: &str = if cfg!(windows) { "\\" } else { "/" }; +const SP: &str = if cfg!(target_os = "windows") { "\\" } else { "/" }; pub fn fetch(query: String, lang: String) -> Result { let x = ureq::get("https://google.com/search")