pub struct Settings { /* private fields */ }Expand description
Setting structure It is composed by 3 private attributes ‘path’ contains the path of the loaded settings file ‘sections’ is a vector containing Section structures inside the settings file ‘messages_table’ is a vector of strings representing all error generated by Settings
Implementations§
Source§impl Settings
impl Settings
Sourcepub fn new() -> Self
pub fn new() -> Self
Associated function to create a Settings structure This function initialize the messages table with the default english language Returns an empty Setting structure
§Examples
use rssettings::Settings;
fn main() {
let mut settings = Settings::new();
}Sourcepub fn new_locale_messages(settings_messages: &[&str; 12]) -> Self
pub fn new_locale_messages(settings_messages: &[&str; 12]) -> Self
Associated function to create a Settings structure This function initialize the messages table with the the user language messages ‘settings_messages’ messages vector passed has reference Returns an empty Setting structure
§Examples
use rssettings::{Settings,MESSAGES_NUMBER};
const IT_SETTINGS_MESSAGES: [&str; MESSAGES_NUMBER] = [
"Errore apertura file di settings: '{}': '{}'",
"Manca il tag di inizio sezione '{}' alla linea '{}' del file di settings: '{}'",
"Manca il tag di fine sezione '{}' alla linea '{}' del file di settings: '{}'",
"Manca il tag di assegnazione '{}' alla linea '{}' del file di settings: '{}'",
"Manca la chiave alla linea '{}' del file di settings: '{}'",
"Chiave duplicata '{}' alla linea '{}' precedentemente definita alla linea '{}' del file di settings: '{}'",
"Sezione '{}' non trovata",
"Sezione '{}' chiave '{}' non trovata",
"Sezione '{}' chiave '{}', Errore di analisi: '{}'",
"Errore scrittura file: '{}': '{}'",
"Errore lettura file: '{}' alla line {}: '{}'",
"Settings già inizializzato utilizzando il file: '{}'"
];
fn main() {
let mut settings = Settings::new_locale_messages(&IT_SETTINGS_MESSAGES);
match settings.load("settings.ini") {
Result::Ok(()) => {
},
Result::Err(error) => {
eprintln!("{}", error);
}
}
}settings_messages- A string slice vector that holds the setting error messages
Sourcepub fn load<P>(&mut self, path: P) -> StdResult<(), String>
pub fn load<P>(&mut self, path: P) -> StdResult<(), String>
Load a settings file Returns std::result::Result::Ok(()) if settings file correctly loaded or std::result::Result::Err(error: String) if something has gone wrong error contains a message (in english or user language according to user created Setting, Settings::new or Settings::new_locale_messages) describing the problem
§Examples
use rssettings::Settings;
fn main() {
let mut settings = Settings::new();
match settings.load("test_files/settings.ini") {
Result::Ok(()) => {
},
Result::Err(error) => {
eprintln!("{}", error);
}
}
}self- An mutable reference to Setting structpath- A Path as reference to the settings file to load
Sourcepub fn save(&self) -> StdResult<(), String>
pub fn save(&self) -> StdResult<(), String>
Save Settings in the file used to load it User can save Settings every time it changes one of its section/key_value pair or let the Settings save itself when it is dropped see trait Drop implementation Returns std::result::Result::Ok(()) when saving is successfuly done or std::result::Result::Err(error: String) when a problem occured error message language depends on new associated function used to crete Settings
§Examples
use rssettings::Settings;
fn main() {
let mut settings = Settings::new();
match settings.load("test_files/settings.ini") {
Result::Ok(()) => {
if let Result::Ok(()) = settings.set("GLOBAL", "bool_value", true) {
if let Result::Err(error) = settings.save() {
eprintln!("{}", error);
}
}
},
Result::Err(error) => {
eprintln!("{}", error);
}
}
}self- A mutable reference to Setting struct
Sourcepub fn get<T: FromStr + Display>(
&self,
section_name: &str,
key: &str,
default_value: T,
) -> SettingsValue<T>
pub fn get<T: FromStr + Display>( &self, section_name: &str, key: &str, default_value: T, ) -> SettingsValue<T>
Generic method use to get section/key value
Generic type parameter has to implement FromStr & Display traits
Returns a SettingsValue structure containing the value associated with the section
and the key if both exist or a default value if not ad SettingsValue.error set with the
relative error message
error message language depends on new associated function used to crete Settings
§Examples
use rssettings::Settings;
fn main() {
let mut settings = Settings::new();
match settings.load("test_files/settings.ini") {
Result::Ok(()) => {
let bool_value = settings.get("GLOBAL", "bool_value", false);
if bool_value.error.len() == 0 {
assert_eq!(true, bool_value.value);
} else {
eprintln!("{}", bool_value.error);
}
},
Result::Err(error) => {
eprintln!("{}", error);
}
}
}self- An immutable reference to Setting structsection_name- A string slice that holds the name of the Sectionkey- A string slice that holds the name of the key inside the SectionT- A default generic value returned in case an error occurs
Sourcepub fn set<T: Display>(
&mut self,
section_name: &str,
key: &str,
value: T,
) -> StdResult<(), String>
pub fn set<T: Display>( &mut self, section_name: &str, key: &str, value: T, ) -> StdResult<(), String>
§Examples
use rssettings::Settings;
fn main() {
let mut settings = Settings::new();
match settings.load("test_files/settings.ini") {
Result::Ok(()) => {
let orig_bool_value = settings.get("GLOBAL", "bool_value", false);
if orig_bool_value.error.len() == 0 {
settings.set("GLOBAL", "bool_value", false).unwrap_or_else(|error| {
eprintln!("{}", error);
});
let new_bool_value = settings.get("GLOBAL", "bool_value", true);
assert_ne!(orig_bool_value.value, new_bool_value.value);
settings.set("GLOBAL", "bool_value", true).unwrap_or_else(|error| {
eprintln!("{}", error);
});
} else {
eprintln!("{}", orig_bool_value.error);
}
},
Result::Err(error) => {
eprintln!("{}", error);
}
}
}self- A mutable reference to Setting structsection_name- A string slice that holds the name of the Sectionkey- A string slice that holds the name of the key inside the SectionT- A new value for the key
Sourcepub fn section_exists(&self, section_name: &str) -> bool
pub fn section_exists(&self, section_name: &str) -> bool
Returns if a section exists
§Examples
use rssettings::Settings;
fn main() {
let mut settings = Settings::new();
match settings.load("test_files/settings.ini") {
Result::Ok(()) => {
if settings.section_exists("GOOFY") {
println!("[GOOFY] exists");
} else {
eprintln!("[GOOFY] does not exist");
}
},
Result::Err(error) => {
eprintln!("{}", error);
}
}
}self- An immutable reference to Setting structsection_name- A string slice that holds the name of the Section
Sourcepub fn key_exists(&self, section_name: &str, key: &str) -> bool
pub fn key_exists(&self, section_name: &str, key: &str) -> bool
Returns if a key exists in a specific section
§Examples
use rssettings::Settings;
fn main() {
let mut settings = Settings::new();
match settings.load("test_files/settings.ini") {
Result::Ok(()) => {
if settings.key_exists("GLOBAL", "enabled") {
println!("key 'enabled' in section 'GLOBAL' exists");
} else {
eprintln!("key 'enabled' in section 'GLOBAL' does not exist");
}
},
Result::Err(error) => {
eprintln!("{}", error);
}
}
}self- An immutable reference to Setting structsection_name- A string slice that holds the name of the Sectionkey- A string slice that holds the name of the key inside the Section