Merge pull request #241 from dtolnay/etcetera

Vendor cache_dir logic from etcetera
This commit is contained in:
David Tolnay 2025-03-03 02:19:12 -05:00 committed by GitHub
commit 89d75db8e2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 100 additions and 104 deletions

103
Cargo.lock generated
View file

@ -200,7 +200,6 @@ dependencies = [
"clap",
"clap-cargo",
"console",
"etcetera",
"fs-err",
"home",
"prettyplease",
@ -215,6 +214,7 @@ dependencies = [
"termcolor",
"toml",
"toolchain_find",
"windows-sys 0.59.0",
]
[[package]]
@ -411,17 +411,6 @@ dependencies = [
"windows-sys 0.59.0",
]
[[package]]
name = "etcetera"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "136d1b5283a1ab77bd9257427ffd09d8667ced0570b6f938942bc7568ed5b943"
dependencies = [
"cfg-if",
"home",
"windows-sys 0.48.0",
]
[[package]]
name = "fancy-regex"
version = "0.11.0"
@ -1112,7 +1101,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1de69df01bdf1ead2f4ac895dc77c9351aefff65b2f3db429a343f9cbf05e132"
dependencies = [
"windows-core",
"windows-targets 0.52.6",
"windows-targets",
]
[[package]]
@ -1124,7 +1113,7 @@ dependencies = [
"windows-implement",
"windows-interface",
"windows-result",
"windows-targets 0.52.6",
"windows-targets",
]
[[package]]
@ -1155,16 +1144,7 @@ version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e383302e8ec8515204254685643de10811af0ed97ea37210dc26fb0032647f8"
dependencies = [
"windows-targets 0.52.6",
]
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets 0.48.5",
"windows-targets",
]
[[package]]
@ -1173,7 +1153,7 @@ version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets 0.52.6",
"windows-targets",
]
[[package]]
@ -1182,22 +1162,7 @@ version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [
"windows-targets 0.52.6",
]
[[package]]
name = "windows-targets"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [
"windows_aarch64_gnullvm 0.48.5",
"windows_aarch64_msvc 0.48.5",
"windows_i686_gnu 0.48.5",
"windows_i686_msvc 0.48.5",
"windows_x86_64_gnu 0.48.5",
"windows_x86_64_gnullvm 0.48.5",
"windows_x86_64_msvc 0.48.5",
"windows-targets",
]
[[package]]
@ -1206,46 +1171,28 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm 0.52.6",
"windows_aarch64_msvc 0.52.6",
"windows_i686_gnu 0.52.6",
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc 0.52.6",
"windows_x86_64_gnu 0.52.6",
"windows_x86_64_gnullvm 0.52.6",
"windows_x86_64_msvc 0.52.6",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
@ -1258,48 +1205,24 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"

View file

@ -22,7 +22,6 @@ cargo-subcommand-metadata = "0.1"
clap = { version = "4", features = ["deprecated", "derive"] }
clap-cargo = "0.15"
console = "0.15"
etcetera = "0.8"
fs-err = "3"
home = "0.5"
prettyplease = { version = "0.2.29", features = ["verbatim"] }
@ -38,6 +37,9 @@ termcolor = "1.0"
toml = "0.8"
toolchain_find = "0.4"
[target.'cfg(all(windows, not(target_vendor = "uwp")))'.dependencies]
windows-sys = { version = "0.59", features = ["Win32_Foundation", "Win32_System_Com", "Win32_UI_Shell"] }
[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

View file

@ -1,5 +1,4 @@
use crate::error::Result;
use etcetera::BaseStrategy as _;
use crate::error::{Error, Result};
use std::env;
use std::path::PathBuf;
use std::str;
@ -21,6 +20,84 @@ pub fn cache_dir() -> Result<PathBuf> {
return Ok(PathBuf::from(cache_dir));
}
let basedirs = etcetera::choose_base_strategy()?;
Ok(basedirs.cache_dir().join("bat"))
let home_dir = home::home_dir().ok_or(Error::HomeDir)?;
#[cfg(windows)]
let cache_dir = windows::cache_dir(&home_dir);
#[cfg(not(windows))]
let cache_dir = xdg::cache_dir(&home_dir);
Ok(cache_dir.join("bat"))
}
// Based on etcetera v0.9.0
#[cfg(windows)]
mod windows {
use std::env;
use std::path::{Path, PathBuf};
pub fn cache_dir(home_dir: &Path) -> PathBuf {
env::var_os("LOCALAPPDATA")
.filter(|s| !s.is_empty())
.map(PathBuf::from)
.or_else(dir_crt)
.unwrap_or_else(|| home_dir.join("AppData").join("Local"))
}
// 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(not(target_vendor = "uwp"))]
fn dir_crt() -> 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, SHGetKnownFolderPath, KF_FLAG_DONT_VERIFY,
};
extern "C" {
fn wcslen(buf: *const u16) -> usize;
}
let mut path = ptr::null_mut();
let S_OK = (unsafe {
SHGetKnownFolderPath(
&FOLDERID_LocalAppData,
KF_FLAG_DONT_VERIFY as u32,
ptr::null_mut(),
&mut path,
)
}) else {
// Free any allocated memory even on failure. A null ptr is a no-op for `CoTaskMemFree`.
unsafe { CoTaskMemFree(path.cast()) };
return None;
};
let path_slice = unsafe { slice::from_raw_parts(path, wcslen(path)) };
let s = OsString::from_wide(path_slice);
unsafe { CoTaskMemFree(path.cast()) };
Some(PathBuf::from(s))
}
#[cfg(target_vendor = "uwp")]
fn dir_crt() -> Option<PathBuf> {
None
}
}
// Based on etcetera v0.9.0
#[cfg(not(windows))]
mod xdg {
use std::env;
use std::path::{Path, PathBuf};
pub fn cache_dir(home_dir: &Path) -> PathBuf {
env::var_os("XDG_CACHE_HOME")
.map(PathBuf::from)
.filter(|path| path.is_absolute())
.unwrap_or_else(|| home_dir.join(".cache/"))
}
}

View file

@ -8,7 +8,7 @@ pub enum Error {
TomlSer(toml::ser::Error),
TomlDe(toml::de::Error),
Quote(shlex::QuoteError),
HomeDir(etcetera::HomeDirError),
HomeDir,
Bat(bat::error::Error),
}
@ -38,12 +38,6 @@ impl From<shlex::QuoteError> for Error {
}
}
impl From<etcetera::HomeDirError> for Error {
fn from(error: etcetera::HomeDirError) -> Self {
Error::HomeDir(error)
}
}
impl From<bat::error::Error> for Error {
fn from(error: bat::error::Error) -> Self {
Error::Bat(error)
@ -57,7 +51,7 @@ impl Display for Error {
Error::TomlSer(e) => e.fmt(formatter),
Error::TomlDe(e) => e.fmt(formatter),
Error::Quote(e) => e.fmt(formatter),
Error::HomeDir(e) => e.fmt(formatter),
Error::HomeDir => formatter.write_str("could not locate home directory"),
Error::Bat(e) => e.fmt(formatter),
}
}
@ -70,7 +64,7 @@ impl StdError for Error {
Error::TomlSer(e) => e.source(),
Error::TomlDe(e) => e.source(),
Error::Quote(e) => e.source(),
Error::HomeDir(e) => e.source(),
Error::HomeDir => None,
Error::Bat(e) => e.source(),
}
}