Skip to content

Commit 45e68ce

Browse files
committed
Update file locations to better follow standards
1 parent 296d174 commit 45e68ce

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "preferences"
3-
version = "0.2.1"
3+
version = "0.3.0"
44
authors = ["Andy Barron <AndrewLBarron@gmail.com>"]
55

66
description = "Read and write user-specific application data (in stable Rust)"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ _Read and write user-specific application data in Rust_
77
## Installation
88
Add the following to your `Cargo.toml`:
99

10-
`preferences = "^0.2.1"`
10+
`preferences = "^0.3.0"`

src/lib.rs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@
145145
//! Data is written to flat files under the active user's home directory in a location specific to
146146
//! the operating system.
147147
//!
148-
//! * Mac OS X: `~/Library/Preferences`
149-
//! * Other Unix/Linux: `~/.config`
150-
//! * Windows: `%USERPROFILE%\AppData\Roaming` (a.k.a. `%APPDATA%`)
148+
//! * Mac OS X: `~/Library/Application Support`
149+
//! * Other Unix/Linux: `$XDG_DATA_HOME`, defaulting to `~/.local/share` if not set
150+
//! * Windows: `%APPDATA%`, defaulting to `<std::env::home_dir()>\AppData\Roaming` if not set
151151
//!
152152
//! The data is stored in JSON format. This has several advantages:
153153
//!
@@ -171,6 +171,7 @@ extern crate rustc_serialize;
171171
use rustc_serialize::{Encodable, Decodable};
172172
use rustc_serialize::json::{self, EncoderError, DecoderError};
173173
use std::collections::HashMap;
174+
use std::env;
174175
use std::fs::{File, create_dir_all};
175176
use std::io::{ErrorKind, Read, Write};
176177
use std::path::{Path, PathBuf};
@@ -179,11 +180,39 @@ use std::string::FromUtf8Error;
179180
type IoError = std::io::Error;
180181

181182
#[cfg(target_os="macos")]
182-
static PREFS_DIR_PATH: &'static str = "Library/Preferences";
183+
fn get_prefs_base_path() -> Option<PathBuf> {
184+
env::home_dir().map(|mut dir| {
185+
dir.push("Library/Application Support");
186+
dir
187+
})
188+
}
189+
183190
#[cfg(all(unix, not(target_os="macos")))]
184-
static PREFS_DIR_PATH: &'static str = ".config";
191+
fn get_prefs_base_path() -> Option<PathBuf> {
192+
match env::var("XDG_DATA_HOME") {
193+
Ok(path_str) => Some(path_str.into()),
194+
Err(..) => {
195+
env::home_dir().map(|mut dir| {
196+
dir.push(".local/share");
197+
dir
198+
})
199+
}
200+
}
201+
}
202+
185203
#[cfg(windows)]
186-
static PREFS_DIR_PATH: &'static str = "AppData/Roaming";
204+
fn get_prefs_base_path() -> Option<PathBuf> {
205+
match env::var("APPDATA") {
206+
Ok(path_str) => Some(path_str.into()),
207+
Err(..) => {
208+
env::home_dir().map(|mut dir| {
209+
dir.push("AppData");
210+
dir.push("Roaming");
211+
dir
212+
})
213+
}
214+
}
215+
}
187216

188217
/// Generic key-value store for user data.
189218
///
@@ -298,13 +327,6 @@ impl<T> PreferencesTrait for T
298327
}
299328
}
300329

301-
fn get_prefs_base_path() -> Option<PathBuf> {
302-
std::env::home_dir().map(|mut dir| {
303-
dir.push(PREFS_DIR_PATH);
304-
dir
305-
})
306-
}
307-
308330
fn path_buf_from_name(name: &str) -> Result<PathBuf, IoError> {
309331

310332
let msg_not_found = "Could not find home directory for user data storage";

0 commit comments

Comments
 (0)