Path: blob/main/crates/bevy_platform/src/dirs/linux.rs
30636 views
use std::{1env::{self, home_dir},2path::PathBuf,3};45/// The path if it's absolute or [`None`]. Empty paths are not absolute.6///7/// [XDG Base Directory Specification] requires that the path specified in environment variables must be absolute. If it's not, we should ignore it and fallback to the default path.8///9/// [XDG Base Directory Specification]: https://specifications.freedesktop.org/basedir/latest/10fn is_absolute_path(path: impl Into<PathBuf>) -> Option<PathBuf> {11let path = path.into();12if path.is_absolute() {13Some(path)14} else {15None16}17}1819/// Returns the path to the directory used for application settings.20pub fn preferences_dir() -> Option<PathBuf> {21// default value for XDG_CONFIG_HOME when unset, empty, or invalid is ~/.config/22env::var_os("XDG_CONFIG_HOME")23.and_then(is_absolute_path)24.or_else(|| home_dir().map(|home| home.join(".config")))25}2627#[cfg(test)]28mod tests {29use super::*;3031#[test]32fn empty_is_not_absolute() {33// preferences_dir() depends on is_absolute_path() returning None for empty paths, so we test that here.34assert!(is_absolute_path("").is_none());35}36}373839