Overview of xdgdir
Here's what xdgdir
is, why you'd use it, and the reason I made it.
I built a small Rust crate called xdgdir
. It’s a simple tool that helps you
find the correct directories for your application’s config, data, and cache
files based on the
XDG Base Directory Specification.
I needed a library like this for my own projects, and I had a few key goals in mind when I wrote it.
- Zero I/O. I wanted a library that only works with paths and never touches
the filesystem. This makes it super fast and predictable. It just gives you
the
PathBuf
you need, and that’s it. - Follow the Spec. It had to correctly check for environment variables like
$XDG_CONFIG_HOME
and fall back to the right defaults (like$HOME/.config
) if they aren’t set. - Simple API. I didn’t want to mess with complex builders or configuration. The API should be small and easy to use.
How it Works
The most common thing you’ll do is get a set of paths for your application. You
just give xdgdir
your app’s name, and it figures out the rest.
Here’s a quick example.
Rust
use xdgdir::BaseDir;
fn main() {
let dirs = BaseDir::new("my-app").unwrap();
// Prints something like /home/user/.config/my-app
println!("Config files go here: {}", dirs.config.display());
// Prints something like /home/user/.local/share/my-app
println!("Data files go here: {}", dirs.data.display());
// Prints something like /home/user/.cache/my-app
println!("Cache files go here: {}", dirs.cache.display());
}
It’s important to know that xdgdir
does not create these directories for you.
It only tells you where they should be. It’s up to you to create them if they
don’t exist.