From 45af4a251e99ad435e354ae55bdbeab2327292ec Mon Sep 17 00:00:00 2001 From: Dan Bluhm Hansen <00.pavers_dither@icloud.com> Date: Thu, 4 Apr 2024 19:35:40 +0200 Subject: [PATCH] get `.crates2.json` from `$CARGO_HOME` if present --- crates/pacdef_core/src/backend/actual/rust.rs | 3 +-- crates/pacdef_core/src/path.rs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/crates/pacdef_core/src/backend/actual/rust.rs b/crates/pacdef_core/src/backend/actual/rust.rs index b168008..d5a2cdd 100644 --- a/crates/pacdef_core/src/backend/actual/rust.rs +++ b/crates/pacdef_core/src/backend/actual/rust.rs @@ -86,8 +86,7 @@ impl Rust { } fn get_crates_file() -> Result { - let mut result = crate::path::get_home_dir().context("getting home dir")?; - result.push(".cargo"); + let mut result = crate::path::get_cargo_home().context("getting cargo home dir")?; result.push(".crates2.json"); Ok(result) } diff --git a/crates/pacdef_core/src/path.rs b/crates/pacdef_core/src/path.rs index 0c69299..03bcada 100644 --- a/crates/pacdef_core/src/path.rs +++ b/crates/pacdef_core/src/path.rs @@ -34,6 +34,22 @@ pub(crate) fn get_pacdef_base_dir() -> Result { Ok(dir) } +/// Get the path to the cargo home directory. +/// +/// # Errors +/// +/// This function will return an error if neither the `$CARGO_HOME` nor +/// the `$HOME` environment variables are set. +pub(crate) fn get_cargo_home() -> Result { + if let Ok(config) = env::var("CARGO_HOME") { + Ok(config.into()) + } else { + let mut config = get_home_dir().context("falling back to $HOME/.cargo")?; + config.push(".cargo"); + Ok(config) + } +} + /// Get the path to the XDG config directory. /// /// # Errors