Struct lib_config::Config

source ·
pub struct Config { /* private fields */ }
Expand description

Object representing an open config file, use lib_config::open_from_path or lib_config::open_from_home to get an instance

Usage

A Config may contain primitive values (such as strings or numbers), arrays or Sections. A Section is a JSON object that can contain the same values as a Config, useful to logically split the config file. To save the contents of the config call the save() associated function.

Implementations§

source§

impl Config

source

pub fn write_value<K: Into<String>, V: Serialize>( &mut self, key: K, value: V ) -> Result<(), Box<dyn Error>>

Writes a valute to the given key, if it doesn’t exist, inserts the key - value pair

source

pub fn read_value<K: Into<String>, V: DeserializeOwned>( &self, key: K ) -> Result<V, Box<dyn Error>>

Reads a value from the given key, if the key does not exist returns Err

source

pub fn read_or_insert<K: Into<String>, V: DeserializeOwned + Serialize + Clone>( &mut self, key: K, value: V ) -> Result<V, Box<dyn Error>>

Reads a value from the given key, if the key does not exists, inserts it with the given value

source

pub fn update_value<K, V, Out, F>( &mut self, key: &K, f_upd: F ) -> Result<Out, Box<dyn Error>>where K: ?Sized + Ord + Eq + Hash, String: Borrow<K>, V: DeserializeOwned, Out: Serialize, F: FnOnce(&V) -> Out,

Updates a value with the given key using the provided function, returns the final value of the key, if the key does not exist returns Err

source

pub fn get_section<K>( &self, key: &K ) -> Result<Section<&JObject>, Box<dyn Error>>where K: ?Sized + Ord + Eq + Hash, String: Borrow<K>,

Gets an immutable reference to Section at the given key

source

pub fn get_section_mut<K>( &mut self, key: &K ) -> Result<Section<&mut JObject>, Box<dyn Error>>where K: ?Sized + Ord + Eq + Hash, String: Borrow<K>,

Gets a mutable reference to Section at the given key

Remarks

Changing the Section’s value will also change the Config data

source

pub fn save(&mut self) -> Result<String, Box<dyn Error>>

Writes the Config object to the file

source

pub fn clone_data(&self) -> JObject

Clones the Config data, the result does not have any reference to the original Config

source

pub fn copy_from(&mut self, data: JObject)

Replaces Config data with the provided data

Trait Implementations§

source§

impl Debug for Config

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.