Struct settingsfile::ShadowSettings
source · Expand description
Complex settings that pulls data from 2 locations
The more complex Settings
style struct that can be used,
designed as a wrapper around Settings
that allows for
configuration ‘Shadowing’, which means you can override
global settings with local settings.
ShadowSettings
looks for two locations for configuration.
First the configuration is defined with the ‘Configuration’
struct. Then ShadowSettings
looks at the global location
(the same location as Settings
would look) and then looks
for the same file in the current working directory.
ShadowSettings
works the same as Settings
except for the additional functions to get
and set
locally or
globally. See Settings or the
ron test
to see how its used.
Example
If you defined your configuration as a toml at
~/.config/app/settings.toml
then it will look for a local
file in the current working directory called settings.toml
.
You can use the Format
trait to
allow hidden files (i.e. “.settings.toml”) or even define an
alternate name for the local file (“.myapp”). See
Format for more information.
Implementations
sourceimpl<T> ShadowSettings<T>where
T: Format + Clone,
impl<T> ShadowSettings<T>where
T: Format + Clone,
sourcepub fn new(config: T) -> ShadowSettings<T>
pub fn new(config: T) -> ShadowSettings<T>
Creates a new ShadowSetting
with both global and local Settings
empty.
sourcepub fn new_and_load(config: T) -> ShadowSettings<T>
pub fn new_and_load(config: T) -> ShadowSettings<T>
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.
sourcepub fn create_from(file: &File, config: T) -> Result<ShadowSettings<T>, Error>
pub fn create_from(file: &File, config: T) -> Result<ShadowSettings<T>, Error>
Creates a new ShadowSetting
and loads the file buffer into the global Setting
.
sourcepub fn load_global(&mut self) -> Result<(), Error>
pub fn load_global(&mut self) -> Result<(), Error>
Loads the global file, or errors
Usually not a breaking error, since it mostly errors because there isn’t a file.
sourcepub fn load_local(&mut self) -> Result<(), Error>
pub fn load_local(&mut self) -> Result<(), Error>
Loads the local file, or errors
Usually not a breaking error, since it mostly errors because there isn’t a file.
sourcepub fn load_global_from(&mut self, file: &mut File) -> Result<(), Error>
pub fn load_global_from(&mut self, file: &mut File) -> Result<(), Error>
Loads the file buffer over the existing global Settings
, replacing it.
Will fail if it cannot read the buffer.
sourcepub fn load_local_from(&mut self, file: &mut File) -> Result<(), Error>
pub fn load_local_from(&mut self, file: &mut File) -> Result<(), Error>
Loads the file buffer over the existing local Settings
, replacing it.
Will fail if it cannot read the buffer.
sourcepub fn save(&self) -> Result<(), Error>
pub fn save(&self) -> Result<(), Error>
saves the setting to a file, uses the save_to
buffer function
sourcepub fn save_global_to(&self, file: &File) -> Result<(), Error>
pub fn save_global_to(&self, file: &File) -> Result<(), Error>
Saves the global Setting
to a file buffer.
sourcepub fn save_local_to(&self, file: &File) -> Result<(), Error>
pub fn save_local_to(&self, file: &File) -> Result<(), Error>
Saves the global Setting
to a file buffer.
Will return Ok even if there isn’t any local settings. I.E. if
the Settings
object never had any data in the local Settings
it will return an Ok
but it will not have written anything to
the disk.
sourcepub fn get_value(&self, key_path: &str) -> Option<Type>
pub fn get_value(&self, key_path: &str) -> Option<Type>
Gets the most accurate value from the key_path
.
Since this is ‘shadowing’, Settings
will look in both
the local and global Settings
to determine what value to
return. The local value will always override the global value.
pub fn get_value_or<A: ?Sized>(&self, key_path: &str, default_value: &A) -> Typewhere
A: SupportedType,
pub fn get_value_local(&self, key_path: &str) -> Option<Type>
pub fn get_value_global(&self, key_path: &str) -> Option<Type>
pub fn set_value_local<A: ?Sized>(
&mut self,
key_path: &str,
value: &A
) -> Result<(), Error>where
A: SupportedType,
pub fn set_value_global<A: ?Sized>(
&mut self,
key_path: &str,
value: &A
) -> Result<(), Error>where
A: SupportedType,
pub fn delete_key_local(&mut self, key_path: &str) -> Option<Type>
pub fn delete_key_global(&mut self, key_path: &str) -> Option<Type>
pub fn delete_file_global(&self) -> bool
pub fn delete_file_local(&self) -> bool
pub fn keys_global(&self) -> Vec<String>
pub fn keys_local(&self) -> Vec<String>
Trait Implementations
sourceimpl<T: Clone> Clone for ShadowSettings<T>where
T: Format + Clone,
impl<T: Clone> Clone for ShadowSettings<T>where
T: Format + Clone,
sourcefn clone(&self) -> ShadowSettings<T>
fn clone(&self) -> ShadowSettings<T>
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more