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;
171171use rustc_serialize:: { Encodable , Decodable } ;
172172use rustc_serialize:: json:: { self , EncoderError , DecoderError } ;
173173use std:: collections:: HashMap ;
174+ use std:: env;
174175use std:: fs:: { File , create_dir_all} ;
175176use std:: io:: { ErrorKind , Read , Write } ;
176177use std:: path:: { Path , PathBuf } ;
@@ -179,11 +180,39 @@ use std::string::FromUtf8Error;
179180type 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-
308330fn 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