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
impl KonfigManager
Sourcepub fn new(opts: KonfigOptions) -> Self
pub fn new(opts: KonfigOptions) -> Self
Simply creates a new KonfigManager
, with the passed in KonfigOptions
Sourcepub fn load(&mut self) -> Result<(), KonfigError>
pub fn load(&mut self) -> Result<(), KonfigError>
Loads the found config data from the specified file into the registered sections
Throws: KonfigError::LoadError
Sourcepub fn save(&self) -> Result<(), KonfigError>
pub fn save(&self) -> Result<(), KonfigError>
Saves the registered sections to the specified file
Throws: KonfigError::SaveError
Sourcepub fn register_section<T>(
&mut self,
section: &mut T,
) -> Result<(), KonfigError>where
T: KonfigSection + 'static,
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
Sourcepub fn validate_all(&self) -> Vec<(String, Result<(), KonfigError>)>
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