Skip to content

Commit

Permalink
Update file locations to better follow standards
Browse files Browse the repository at this point in the history
  • Loading branch information
andybarron committed Mar 7, 2016
1 parent 296d174 commit 45e68ce
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "preferences"
version = "0.2.1"
version = "0.3.0"
authors = ["Andy Barron <AndrewLBarron@gmail.com>"]

description = "Read and write user-specific application data (in stable Rust)"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ _Read and write user-specific application data in Rust_
## Installation
Add the following to your `Cargo.toml`:

`preferences = "^0.2.1"`
`preferences = "^0.3.0"`
48 changes: 35 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@
//! Data is written to flat files under the active user's home directory in a location specific to
//! the operating system.
//!
//! * Mac OS X: `~/Library/Preferences`
//! * Other Unix/Linux: `~/.config`
//! * Windows: `%USERPROFILE%\AppData\Roaming` (a.k.a. `%APPDATA%`)
//! * Mac OS X: `~/Library/Application Support`
//! * Other Unix/Linux: `$XDG_DATA_HOME`, defaulting to `~/.local/share` if not set
//! * Windows: `%APPDATA%`, defaulting to `<std::env::home_dir()>\AppData\Roaming` if not set
//!
//! The data is stored in JSON format. This has several advantages:
//!
Expand All @@ -171,6 +171,7 @@ extern crate rustc_serialize;
use rustc_serialize::{Encodable, Decodable};
use rustc_serialize::json::{self, EncoderError, DecoderError};
use std::collections::HashMap;
use std::env;
use std::fs::{File, create_dir_all};
use std::io::{ErrorKind, Read, Write};
use std::path::{Path, PathBuf};
Expand All @@ -179,11 +180,39 @@ use std::string::FromUtf8Error;
type IoError = std::io::Error;

#[cfg(target_os="macos")]
static PREFS_DIR_PATH: &'static str = "Library/Preferences";
fn get_prefs_base_path() -> Option<PathBuf> {
env::home_dir().map(|mut dir| {
dir.push("Library/Application Support");
dir
})
}

#[cfg(all(unix, not(target_os="macos")))]
static PREFS_DIR_PATH: &'static str = ".config";
fn get_prefs_base_path() -> Option<PathBuf> {
match env::var("XDG_DATA_HOME") {
Ok(path_str) => Some(path_str.into()),
Err(..) => {
env::home_dir().map(|mut dir| {
dir.push(".local/share");
dir
})
}
}
}

#[cfg(windows)]
static PREFS_DIR_PATH: &'static str = "AppData/Roaming";
fn get_prefs_base_path() -> Option<PathBuf> {
match env::var("APPDATA") {
Ok(path_str) => Some(path_str.into()),
Err(..) => {
env::home_dir().map(|mut dir| {
dir.push("AppData");
dir.push("Roaming");
dir
})
}
}
}

/// Generic key-value store for user data.
///
Expand Down Expand Up @@ -298,13 +327,6 @@ impl<T> PreferencesTrait for T
}
}

fn get_prefs_base_path() -> Option<PathBuf> {
std::env::home_dir().map(|mut dir| {
dir.push(PREFS_DIR_PATH);
dir
})
}

fn path_buf_from_name(name: &str) -> Result<PathBuf, IoError> {

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

0 comments on commit 45e68ce

Please sign in to comment.