Skip to main content

gqls/
paths.rs

1//! Shared on-disk locations. gqls keeps everything cacheable under one base
2//! dir (`$XDG_CACHE_HOME/gqls`, else `~/.cache/gqls`) — embedding vectors, the
3//! introspection response cache, and the background-warm lockfiles.
4
5use std::path::PathBuf;
6
7/// Base cache directory, or `None` if neither `XDG_CACHE_HOME` nor `HOME` is set.
8pub fn cache_dir() -> Option<PathBuf> {
9    let base = std::env::var_os("XDG_CACHE_HOME")
10        .map(PathBuf::from)
11        .or_else(|| std::env::var_os("HOME").map(|h| PathBuf::from(h).join(".cache")))?;
12    Some(base.join("gqls"))
13}
14
15/// Render a path for display, shortening the home directory to `~`.
16pub fn display(p: &std::path::Path) -> String {
17    if let Some(home) = std::env::var_os("HOME") {
18        if let Ok(rest) = p.strip_prefix(&home) {
19            return format!("~/{}", rest.display());
20        }
21    }
22    p.display().to_string()
23}
24
25/// Directory for ephemeral files (the background-warm single-flight lockfiles).
26/// The system temp dir so the OS reaps them — they never litter the cache. It's
27/// per-user on macOS (`$TMPDIR`) and `/tmp` on Linux, and stable within a login,
28/// so concurrent gqls processes still see the same lock.
29pub fn temp_dir() -> PathBuf {
30    std::env::temp_dir().join("gqls")
31}