Inline cache_dir into assets module

This commit is contained in:
David Tolnay 2025-03-02 22:36:57 -08:00
parent a58bd1a6ca
commit d46967c745
No known key found for this signature in database
GPG key ID: F9BA143B95FF6D82
3 changed files with 103 additions and 109 deletions

View file

@ -1,5 +1,4 @@
use crate::error::Result;
use crate::etcetera;
use crate::error::{Error, Result};
use std::env;
use std::path::PathBuf;
use std::str;
@ -21,6 +20,107 @@ pub fn cache_dir() -> Result<PathBuf> {
return Ok(PathBuf::from(cache_dir));
}
let cache_dir = etcetera::cache_dir()?;
let home_dir = home::home_dir().ok_or(Error::HomeDir)?;
let cache_dir = if cfg!(windows) {
windows::cache_dir(&home_dir)
} else {
xdg::cache_dir(&home_dir)
};
Ok(cache_dir.join("bat"))
}
mod windows {
use std::path::{Path, PathBuf};
fn dir_inner(env: &'static str) -> Option<PathBuf> {
std::env::var_os(env)
.filter(|s| !s.is_empty())
.map(PathBuf::from)
.or_else(|| dir_crt(env))
}
// Ref: https://github.com/rust-lang/cargo/blob/home-0.5.11/crates/home/src/windows.rs
// We should keep this code in sync with the above.
#[cfg(all(windows, not(target_vendor = "uwp")))]
fn dir_crt(env: &'static str) -> Option<PathBuf> {
use std::ffi::OsString;
use std::os::windows::ffi::OsStringExt;
use std::ptr;
use std::slice;
use windows_sys::Win32::Foundation::S_OK;
use windows_sys::Win32::System::Com::CoTaskMemFree;
use windows_sys::Win32::UI::Shell::{
FOLDERID_LocalAppData, FOLDERID_RoamingAppData, SHGetKnownFolderPath,
KF_FLAG_DONT_VERIFY,
};
extern "C" {
fn wcslen(buf: *const u16) -> usize;
}
let folder_id = match env {
"APPDATA" => FOLDERID_RoamingAppData,
"LOCALAPPDATA" => FOLDERID_LocalAppData,
_ => return None,
};
unsafe {
let mut path = ptr::null_mut();
match SHGetKnownFolderPath(
&folder_id,
KF_FLAG_DONT_VERIFY as u32,
std::ptr::null_mut(),
&mut path,
) {
S_OK => {
let path_slice = slice::from_raw_parts(path, wcslen(path));
let s = OsString::from_wide(path_slice);
CoTaskMemFree(path.cast());
Some(PathBuf::from(s))
}
_ => {
// Free any allocated memory even on failure. A null ptr is a no-op for `CoTaskMemFree`.
CoTaskMemFree(path.cast());
None
}
}
}
}
#[cfg(not(all(windows, not(target_vendor = "uwp"))))]
fn dir_crt(_env: &'static str) -> Option<PathBuf> {
None
}
pub fn cache_dir(home_dir: &Path) -> PathBuf {
dir_inner("LOCALAPPDATA").unwrap_or_else(|| home_dir.join("AppData").join("Local"))
}
}
mod xdg {
use std::path::{Path, PathBuf};
fn env_var_or_none(env_var: &str) -> Option<PathBuf> {
std::env::var(env_var).ok().and_then(|path| {
let path = PathBuf::from(path);
// Return None if the path obtained from the environment variable isn’t absolute.
if path.is_absolute() {
Some(path)
} else {
None
}
})
}
fn env_var_or_default(home_dir: &Path, env_var: &str, default: impl AsRef<Path>) -> PathBuf {
env_var_or_none(env_var).unwrap_or_else(|| home_dir.join(default))
}
pub fn cache_dir(home_dir: &Path) -> PathBuf {
env_var_or_default(home_dir, "XDG_CACHE_HOME", ".cache/")
}
}

View file

@ -1,105 +0,0 @@
use crate::error::{Error, Result};
use std::path::PathBuf;
pub fn cache_dir() -> Result<PathBuf> {
let home_dir = home::home_dir().ok_or(Error::HomeDir)?;
if cfg!(windows) {
Ok(windows::cache_dir(&home_dir))
} else {
Ok(xdg::cache_dir(&home_dir))
}
}
mod windows {
use std::path::{Path, PathBuf};
fn dir_inner(env: &'static str) -> Option<PathBuf> {
std::env::var_os(env)
.filter(|s| !s.is_empty())
.map(PathBuf::from)
.or_else(|| dir_crt(env))
}
// Ref: https://github.com/rust-lang/cargo/blob/home-0.5.11/crates/home/src/windows.rs
// We should keep this code in sync with the above.
#[cfg(all(windows, not(target_vendor = "uwp")))]
fn dir_crt(env: &'static str) -> Option<PathBuf> {
use std::ffi::OsString;
use std::os::windows::ffi::OsStringExt;
use std::ptr;
use std::slice;
use windows_sys::Win32::Foundation::S_OK;
use windows_sys::Win32::System::Com::CoTaskMemFree;
use windows_sys::Win32::UI::Shell::{
FOLDERID_LocalAppData, FOLDERID_RoamingAppData, SHGetKnownFolderPath,
KF_FLAG_DONT_VERIFY,
};
extern "C" {
fn wcslen(buf: *const u16) -> usize;
}
let folder_id = match env {
"APPDATA" => FOLDERID_RoamingAppData,
"LOCALAPPDATA" => FOLDERID_LocalAppData,
_ => return None,
};
unsafe {
let mut path = ptr::null_mut();
match SHGetKnownFolderPath(
&folder_id,
KF_FLAG_DONT_VERIFY as u32,
std::ptr::null_mut(),
&mut path,
) {
S_OK => {
let path_slice = slice::from_raw_parts(path, wcslen(path));
let s = OsString::from_wide(path_slice);
CoTaskMemFree(path.cast());
Some(PathBuf::from(s))
}
_ => {
// Free any allocated memory even on failure. A null ptr is a no-op for `CoTaskMemFree`.
CoTaskMemFree(path.cast());
None
}
}
}
}
#[cfg(not(all(windows, not(target_vendor = "uwp"))))]
fn dir_crt(_env: &'static str) -> Option<PathBuf> {
None
}
pub fn cache_dir(home_dir: &Path) -> PathBuf {
dir_inner("LOCALAPPDATA").unwrap_or_else(|| home_dir.join("AppData").join("Local"))
}
}
mod xdg {
use std::path::{Path, PathBuf};
fn env_var_or_none(env_var: &str) -> Option<PathBuf> {
std::env::var(env_var).ok().and_then(|path| {
let path = PathBuf::from(path);
// Return None if the path obtained from the environment variable isn’t absolute.
if path.is_absolute() {
Some(path)
} else {
None
}
})
}
fn env_var_or_default(home_dir: &Path, env_var: &str, default: impl AsRef<Path>) -> PathBuf {
env_var_or_none(env_var).unwrap_or_else(|| home_dir.join(default))
}
pub fn cache_dir(home_dir: &Path) -> PathBuf {
env_var_or_default(home_dir, "XDG_CACHE_HOME", ".cache/")
}
}

View file

@ -21,7 +21,6 @@ mod cmd;
mod config;
mod edit;
mod error;
mod etcetera;
mod fmt;
mod manifest;
mod opts;