Coder Social home page Coder Social logo

preferences-rs's Introduction

preferences

Read and write user-specific application data in Rust

crates.io: preferences Linux & OS X build status Windows build status

Documentation

https://docs.rs/preferences

Installation

Add the following to your Cargo.toml:

preferences = "^1.1.0"

preferences-rs's People

Contributors

andybarron avatar eh2406 avatar nsmaciej avatar zsiciarz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

preferences-rs's Issues

Focus on config/preferences only

This lib should focus exclusively on config/preferences. Changes:

  • Switch to $XDG_CONFIG_HOME with default to ~/.config on *nix platforms.
  • Auto-suffix config files with .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 data
  • Copy path logic to a new crate modeled after appdirs
  • Remove path logic from preferences-rs and depend on the new crate

Add a `config` equivalent

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).

Allow specifying data type other than `app_dirs::AppDataType::UserConfig`

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.)

Set up CI

Use Travis or Appveyor for cross-platform CI. Should test Linux, OS X, and Windows.

Allow full file name override

Right 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!

Custom struct example does not work

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.