andybarron / preferences-rs Goto Github PK
View Code? Open in Web Editor NEWRead and write user-specific application data in (stable!) Rust
Home Page: https://docs.rs/preferences
License: MIT License
Read and write user-specific application data in (stable!) Rust
Home Page: https://docs.rs/preferences
License: MIT License
So users can actually match on PreferencesError::Directory
's data.
Not all user-specific preferences should go to XDG_DATA_HOME
, configuration for instance could go to XDG_CONFIG_HOME
. Much of the structure is the same, though (serializable key-value pairs), so it may make sens to include this in this crate.
Among the few differences between preference and config, I'd say more option in the serialization format would be helpful (some people will really like toml
for configuration, but not necessarily for preferences).
This lib should focus exclusively on config/preferences. Changes:
$XDG_CONFIG_HOME
with default to ~/.config
on *nix platforms..cfg
(or .cfg.json
, or something else entirely) to avoid naming conflicts on systems that don't specify different locations for e.g. config and dataRight now, there are two options for a preference file name:
<key>.prefs.json
prefs.json
It would be great to be able to override this, so that any file name can be chosen (some linux utilities use a .config/app_name/config
pattern, which is currently impossible). At very least, if full control over the extension is undesirable, being able to avoid the .prefs
extension prefix when using a non-empty key would be great!
Use Travis or Appveyor for cross-platform CI. Should test Linux, OS X, and Windows.
Add new versions of Preferences
trait methods (e.g. save(..)
-> save_type(..)
) that let users specify the type of data to be saved/loaded (e.g. UserCache
, UserData
). Would expand support for non-config data such as cache and logs. (They can already be stored, but don't usually belong in the same place as config data.)
Am I doing something wrong or is there a problem with the newest Rust compiler?
Cargo.toml:
[package]
name = "prefs"
version = "0.1.0"
authors = ["Sehny <[email protected]>"]
[dependencies]
serde = "1.0"
serde_derive = "1.0"
preferences = "^1.1.0"
Source code:
#[macro_use]
extern crate serde_derive;
extern crate preferences;
use preferences::{AppInfo, Preferences};
const APP_INFO: AppInfo = AppInfo{name: "preferences", author: "Rust language community"};
// Deriving `Serialize` and `Deserialize` on a struct/enum automatically implements
// the `Preferences` trait.
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct PlayerData {
level: u32,
health: f32,
}
fn main() {
let player = PlayerData{level: 2, health: 0.75};
let prefs_key = "tests/docs/custom-types";
let save_result = player.save(&APP_INFO, prefs_key);
assert!(save_result.is_ok());
// Method `load` is from trait `Preferences`.
let load_result = PlayerData::load(&APP_INFO, prefs_key);
assert!(load_result.is_ok());
assert_eq!(load_result.unwrap(), player);
}
Build log:
$ rustc --version
rustc 1.23.0 (766bd11c8 2018-01-01)
$ cargo build
Compiling quote v0.3.15
Compiling dtoa v0.4.2
Compiling num-traits v0.1.42
Compiling serde v1.0.27
Compiling itoa v0.3.4
Compiling serde v0.9.15
Compiling unicode-xid v0.0.4
Compiling xdg v2.1.0
Compiling synom v0.11.3
Compiling syn v0.11.11
Compiling app_dirs v1.1.1
Compiling serde_derive_internals v0.19.0
Compiling serde_derive v1.0.27
Compiling serde_json v0.9.10
Compiling preferences v1.1.0
Compiling prefs v0.1.0 (file:///home/sehny/tmp/prefs)
error[E0599]: no method named `save` found for type `PlayerData` in the current scope
--> src/main.rs:21:30
|
21 | let save_result = player.save(&APP_INFO, prefs_key);
| ^^^^
|
= note: the method `save` exists but the following trait bounds were not satisfied:
`PlayerData : preferences::Preferences`
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `save`, perhaps you need to implement it:
candidate #1: `preferences::Preferences`
error[E0599]: no function or associated item named `load` found for type `PlayerData` in the current scope
--> src/main.rs:25:23
|
25 | let load_result = PlayerData::load(&APP_INFO, prefs_key);
| ^^^^^^^^^^^^^^^^
|
= note: the method `load` exists but the following trait bounds were not satisfied:
`PlayerData : preferences::Preferences`
`&PlayerData : preferences::Preferences`
`&mut PlayerData : preferences::Preferences`
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `load`, perhaps you need to implement it:
candidate #1: `preferences::Preferences`
error: aborting due to 2 previous errors
error: Could not compile `prefs`.
To learn more, run the command again with --verbose.
Now that Rust 1.15 has been released and custom derive functionality has stabilized, preferences
can use serde
, rather than rustc-serialize
.
Put all Windows-specific dependencies under [target.'cfg(windows)'.dependencies]
. (This syntax does not exists in the current stable version of Rust, 1.7.0.)
Consistent error handling FTW.
Version 1.1.0 of app_dirs renames an enum variant that we wrap, so we want to update ASAP.
Is there a way to clear / remove the saved preferences?
https://andybarron.github.io/docs/preferences-rs/preferences/index.html
Is out of date and does not include new changes to APP_INFO. As this was an API change, more than a little bit of time was wasted until digging into the library to figure out what was wrong.
Additionally, having docs on https://docs.rs/preferences-rs would be nice so crates.io can find the documentation.
Update to serde and serde_json v1.0, please :)
Was going to use File
, but maybe Read
/Write
would be better; hmm.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.