Struct KonfigManager

Source
pub struct KonfigManager { /* private fields */ }
Expand description

The main manager in konfig-rust, this is intended to be created near the start of your program, and destroyed by closing it

KonfigManager allows you to register sections, which then can be loaded and saved into a single file

§Keep in mind this uses raw pointers to store data section, hence why it is supposed to be used, as a single “source of truth” in your app, likely as a global instance

example:

use serde::{Deserialize, Serialize};
use konfig_rust::*;
use konfig_rust::format::*;

use konfig_rust_derive::KonfigSection;

#[derive(Serialize, Deserialize, KonfigSection)] // Aside from KonfigSection, you also have to use the Serialize and Deserialize macros
struct Config {
    name: String,
    age: u32,
}

let mut c = Config { name: "Bob".to_string(), age: 32 };

let mut manager = KonfigManager::new(KonfigOptions {
    format: Format::JSON,
    auto_save: true,
    use_callbacks: true,
    config_path: "config.json".to_string(),
});

manager.register_section(&mut c).unwrap();

manager.load().unwrap();

println!("Name: {}, Age: {}", c.name, c.age); // Notice how you just access the struct like normal in memory state storage

manager.save().unwrap();

Implementations§

Source§

impl KonfigManager

Source

pub fn new(opts: KonfigOptions) -> Self

Simply creates a new KonfigManager, with the passed in KonfigOptions

Source

pub fn load(&mut self) -> Result<(), KonfigError>

Loads the found config data from the specified file into the registered sections

Throws: KonfigError::LoadError

Source

pub fn save(&self) -> Result<(), KonfigError>

Saves the registered sections to the specified file

Throws: KonfigError::SaveError

Source

pub fn register_section<T>( &mut self, section: &mut T, ) -> Result<(), KonfigError>
where T: KonfigSection + 'static,

Registers a new section with the KonfigManager, the section must use the Serialize and Deserialize macros and implement the KonfigSection trait (or use the #[derive(KonfigSection)] macro to do it for you)

Throws: KonfigError::RegistrationError

Source

pub fn validate_all(&self) -> Vec<(String, Result<(), KonfigError>)>

Quick helper that runs the validation callback on all registered sections, useful if the config is modified a lot in memory but rarely saved or loaded

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.