Skip to content

visnkmr/prefstore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

prefstore

Crates.io Crates.io Crates.io

A rust crate to Easily store and retrieve preferences in rust.

Installation

Install the crate as a dependency in your app's Cargo.toml file:

[dependencies]
prefstore = "0.5.0"

Then you can use it in your code like this:

// Import prefstore
use prefstore::*;

fn main() {
    // Save value to disk using savepreference
    savepreference("MyApp", "name", "Alice");

    // Save value to disk using savecustom
    savecustom("MyApp", "age.txt", 25);

    // Save value to disk using appendcustom
    appendcustom("MyApp", "hobbies.txt", "reading");

    // Load value from disk using getpreference
    let name = getpreference("MyApp", "name", "Bob");
    println!("Name: {}", name);

    // Load value from disk using getcustom
    let age = getcustom("MyApp", "age.txt", 0);
    println!("Age: {}", age);

    // Load value from disk using getcustomwithnodefault
    let hobbies = getcustomwithnodefault("MyApp", "hobbies.txt");
    println!("Hobbies: {}", hobbies);

    // Delete preference file from disk using clearpreference
    clearpreference("MyApp", "name");

     // Delete preference file from disk using clearcustom
     clearcustom("MyApp", "age.txt");
 }

 // For safe handling of fast writes, use CachedPrefStore
 use prefstore::CachedPrefStore;

 fn main() {
     let store = CachedPrefStore::new(5); // Flush every 5 seconds

     // Save preferences (cached and queued)
     store.savepreference("MyApp", "name", "Alice");
     store.savepreference("MyApp", "age", 25);

     // Get preferences (from cache first)
     let name = store.getpreference("MyApp", "name", "Bob");
     println!("Name: {}", name);

     // Force flush to disk
     store.flush().unwrap();
 }

Features

  • Supports any type that implements [Display] for values
  • Stores each preference in separate file, ensuring quick access and minimum disk read/write operations.
  • Provides methods for setting, getting, removing preferebces.
  • Provides methods for loading and saving preference from and to a file.
  • Provides methods for clearing.
  • CachedPrefStore for preventing corruption from fast writes: uses in-memory cache with queued writes and periodic flushing to disk.

License

This project is licensed under the MIT license. See the LICENSE file for more details.

About

Easily store and retrieve preferences when using rust.

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages