Struct settingsfile::Settings

source ·
pub struct Settings<T>where
    T: Format + Clone,
{ /* private fields */ }
Expand description

Basic one file settings

The main guts of Settingsfile-rs. The Settings struct interacts with the file system to serialize / deserialize configurations / settings and then allow easy navigation and maniulation.

Settings only reads data from one source and doesn’t do any ‘shadowing’, so if you want to override settings based on a local user configuration, use the ShadowSettings struct instead.

Examples

General Use

Say a file looked like this, and was defined in the Format Config

{
    "user" : {
        "name" : "user's name",
        "email" : "user's email"
    }
}

We could access the data like this.

use settingsfile::Settings;
use settingsfile::EmptyConfig; // dumb config for examples and testing
 
let app_settings = Settings::new(EmptyConfig{});
if let Some(value) = app_settings.get_value("user.name") {
    // prints "username is user's name"
    println!("username is {}",value); 
}

And if we weren’t sure if the user would set a certain value, but would need something during running we can define a default value.

use settingsfile::Settings;
use settingsfile::EmptyConfig; // dumb config for examples and testing
 
let app_settings = Settings::new(EmptyConfig{});
let font_size = app_settings.get_value_or("font.size",&12);
println!("Font size is {}",font_size); // will either be 12 or whatever the user sets.

Data Validation

Rust is heavy on type safe, and so is settingsfile. By default a settings file can only contain the following types (defined in the Type enum)

  • Text
  • Int
  • Float
  • Switch
  • Array (of any combination of above)
  • HashMap (of any combination of above)

Every result of a get will return a Type that you will need to match in order to use the data. The only except is when you print / format it, as Type implements display.

Using the fontsize example above.

use settingsfile::Settings;
use settingsfile::Type;
use settingsfile::EmptyConfig; // dumb config for examples and testing
 
let app_settings = Settings::new(EmptyConfig{});
if let Type::Int(size) = app_settings.get_value_or("font.size",&12) {
    println!("Setting font size to {}",size);
} else {
    println!("Setting font.size must be an int!");
}

Implementations

Creates an empty Settings from a configuration.

Initally the settings doesn’t have any data and needs to have data inserted, set or loaded, ::load().

A onliner to create a Settings and load from the config location.

Basically the same thing as .new() and .load(). The main difference is you don’t need to give your Settings mutability if you don’t need it.

Will return an empty setting if it fails loading the file for some reason. A warn!() (from the log crate) will be used if the loading fails.

Loads the content of a File using the configuration, but doesn’t use a path or doesn’t infer the path from the config.

Primariy made for testing serializing and deserializing the struture, but can also because to force the setting to load from a file buffer

Wrapper around load_from so the return value isn’t an result. loads a new setting object from a path, or creates an empty object if file doesn’t exist or errors in any way.

Loads Setting data from the file defined in the configuration.

If nothing exists it throws an error. This shouldn’t be used for initalizing a new Settings, look at create and create_from for that.

Will override the existing data of a Setting

Loads into the current Setting from a file.

Will override the existing data of a Setting

Saves the setting to a file defined in the configuraton.

saves the setting to a file buffer.

Get the saved value inside of a Setting

Looks for a key_path in dot notation and returns an Option containing the value if it exists.

Wraps get_value so instead of an Option the result will always be a type.

The default value can be any type that implements SupportedType

sets the value of a key, uses a generic that must implement the SupportedType trait

Deletes the key and returns the current value, returns none if the key didn’t exist.

Deletes the physical file from the disk

Trait Implementations

implementing add so you should be able to use ‘+’ on two settings, useful because you may want to combine settings from different locations.

Adding to Settings works by overlaying the two Settings on top of each other, if the same “key” has multiple “values”, the “self” is overwritten with the “other”. So Adding a Settings means you are overlaying it ontop of the existing data.

The resulting type after applying the + operator.

AddAssign follows the same logic as Add, and allows you to use += with Settings

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Deserialize this value from the given Serde deserializer. Read more
Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.