This repository has been archived by the owner on Jul 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
OptionaReader
Ivan Semenkov edited this page Feb 5, 2021
·
1 revision
- About
- delete
- is_section
- is_array
- is_list
- parent
- value_type
- value
- as_array
- as_list
- as_int32
- as_int32_default
- as_int64
- as_int64_default
- as_float64
- as_float64_default
- as_bool
- as_bool_default
- as_string
- as_string_default
Reader for configuration option.
pub struct OptionReader
Delete current config element.
pub fn delete(&self) -> Result<()>
use librustconfig::config::Config;
let cfg = Config::new();
match cfg.create_section("group") {
Some(group) => {
/* ... */
if !group.delete().is_ok() {
/* ... */
}
},
None => { /* ... */ }
}
Return true if element is section group.
pub fn is_section(&self) -> Option<bool>
use librustconfig::config::Config;
let cfg = Config::new();
if cfg.create_section("root").is_none() {
panic!("Can't create root section!");
}
/* ... */
if cfg.value("root").unwrap().is_section().unwrap() {
/* ... */
}
Return true if element is array.
pub fn is_array(&self) -> Option<bool>
use librustconfig::config::Config;
let cfg = Config::new();
let root = cfg.create_section("root");
if root.is_none() {
panic!("Can't create root section!");
}
/* ... */
if root.unwrap().create_array("value").is_none() {
panic!("Can't create array!")
}
/* ... */
if cfg.value("root.value").unwrap().is_array().unwrap() {
/* ... */
}
Return true if element is list.
pub fn is_list(&self) -> Option<bool>
use librustconfig::config::Config;
let cfg = Config::new();
let group = cfg.create_section("group");
if group.is_none() {
panic!("Can't create group section!");
}
/* ... */
if group.unwrap().create_list("list").is_none() {
panic!("Can't create list option!");
}
/* ... */
if cfg.value("group.list").unwrap().is_list().unwrap() {
// ...
}
Return option element parent item.
pub fn parent(&self) -> Option<OptionReader>
use librustconfig::config::Config;
let cfg = Config::new();
let group = cfg.create_section("group");
if group.is_none() {
panic!("Can't create a group section!");
}
/* ... */
let section = group.unwrap().create_section("section");
if section.is_none() {
panic!("Can't create section!");
}
/* ... */
match cfg.value("group.section").unwrap().parent() {
Some(val) => { /* ... */ },
None => { /* ... */ }
}
Return option value type.
pub fn value_type(&self) -> Option<OptionType>
use librustconfig::config::{Config, OptionType};
let cfg = Config::new();
let group = cfg.create_section("group");
if group.is_none() {
panic!("Can't create group section!");
}
/* ... */
if group.unwrap().write_int32("value", 1345).is_none() {
panic!("Can't write int32 value to config!");
}
/* ... */
match cfg.value("group.value").unwrap().value_type().unwrap() {
OptionType::IntegerType => { /* ... */ },
OptionType::Int64Type => { /* ... */ },
OptionType::FloatType => { /* ... */ },
OptionType::StringType => { /* ... */ },
OptionType::BooleanType => { /* ... */ }
}
Read value from path.
pub fn value<S>(&self, path : S) -> Option<OptionReader>
where S: Into<String>
use librustconfig::config::Config;
let cfg = Config::new();
let group = cfg.create_section("group");
if group.is_none() {
panic!("Can't create group section!");
}
/* ... */
if group.unwrap().write_string("value", "test string").is_none() {
panic!("Can't write string value to config!");
}
/* ... */
match cfg.value("group.value") {
Some(val) => { /* ... */ },
None => { /* ... */ }
}
pub fn as_array(&self) -> CollectionReaderIterator
pub fn as_list(&self) -> CollectionReaderIterator
Present option value as i32.
pub fn as_int32(&self) -> Option<i32>
use librustconfig::config::Config;
let cfg = Config::new();
let group = cfg.create_section("group");
if group.is_none() {
panic!("Can't create group section!");
}
/* ... */
if group.unwrap().write_int32("value", 131).is_none() {
panic!("Can't write int32 value!");
}
/* ... */
match cfg.value("group.value").unwrap().as_int32() {
Some(val) => { /* ... */ },
None => { /* ... */ }
}
Present option value as i32, return def if value not found.
pub fn as_int32_default (&self, def : i32) -> i32
use librustconfig::config::Config;
let cfg = Config::new();
let group = cfg.create_section("group");
if group.is_none() {
panic!("Can't create group section!");
}
/* ... */
if group.unwrap().write_int32("value", 143).is_none() {
panic!("Can't write int32 value!");
}
/* ... */
let ival = cfg.value("group.value").unwrap().as_int32_default(0);
Present option value as i64.
pub fn as_int64(&self) -> Option<i64>
use librustconfig::config::Config;
let cfg = Config::new();
let group = cfg.create_section("group");
if group.is_none() {
panic!("Can't create group section!");
}
/* ... */
if group.unwrap().write_int64("value", 120).is_none() {
panic!("Can't write int64 value!");
}
/* ... */
match cfg.value("group.value").unwrap().as_int64() {
Some(val) => { /* ... */ },
None => { /* ... */ }
}
Present option value as i64, return def if value not exists.
pub fn as_int64_default(&self, def : i64) -> i64
use librustconfig::config::Config;
let cfg = Config::new();
let group = cfg.create_section("group");
if group.is_none() {
panic!("Can't create group section!");
}
/* ... */
if group.unwrap().write_int64("value", 145).is_none() {
panic!("Can't write int64 value!");
}
/* ... */
let value = cfg.value("group.value").unwrap().as_int64_default(0);
Present option value as f64.
pub fn as_float64(&self) -> Option<f64>
use librustconfig::config::Config;
let cfg = Config::new();
let group = cfg.create_section("group");
if group.is_none() {
panic!("Can't create group section!");
}
/* ... */
if group.unwrap().write_float64("value", 1.00021).is_none() {
panic!("Can't write float64 value!");
}
/* ... */
match cfg.value("group.value").unwrap().as_float64() {
Some(val) => { /* ... */ },
None => { /* ... */ }
}
Present option value as f64, return def if value not exists.
pub fn as_float64_default(&self, def : f64) -> f64
use librustconfig::config::Config;
let cfg = Config::new();
let group = cfg.create_section("group");
if group.is_none() {
panic!("Can't create group section!");
}
/* ... */
if group.unwrap().write_float64("value", 20.201).is_none() {
panic!("Can't write float64 value!");
}
/* ... */
let value = cfg.value("group.value").unwrap().as_float64_default(0.0);
Present option value as bool.
pub fn as_bool(&self) -> Option<bool>
use librustconfig::config::Config;
let cfg = Config::new();
let group = cfg.create_section("group");
if group.is_none() {
panic!("Can't create group section!");
}
/* ... */
if group.unwrap().write_bool("value", true).is_none() {
panic!("Can't write bool value!");
}
/* ... */
match cfg.value("group.value").unwrap().as_bool() {
Some(val) => { /* ... */ },
None => { /* ... */ }
}
Present option value as bool, return def if value not exists.
pub fn as_bool_default(&self, def : bool) -> bool
use librustconfig::config::Config;
let cfg = Config::new();
let group = cfg.create_section("group");
if group.is_none() {
panic!("Can't create group section!");
}
/* ... */
if group.unwrap().write_bool("value", true).is_none() {
panic!("Can't write value!");
}
/* ... */
let value = cfg.value("group.value").unwrap().as_bool_default(false);
Present option value as string.
pub fn as_string(&self) -> Option<String>
use librustconfig::config::Config;
let cfg = Config::new();
let group = cfg.create_section("group");
if group.is_none() {
panic!("Can't create group section!");
}
/* ... */
if group.unwrap().write_string("value", "string val").is_none() {
panic!("Can't write string value!");
}
/* ... */
match cfg.value("group.value").unwrap().as_string() {
Some(val) => { /* ... */ },
None => { /* ... */ }
}
Present option value as string, return def if value not exists.
pub fn as_string_default<S>(&self, def : S) -> String
use librustconfig::config::Config;
let cfg = Config::new();
let group = cfg.create_section("group");
if group.is_none() {
panic!("Can't create group section!");
}
/* ... */
if group.unwrap().write_string("value", "string val").is_none() {
panic!("Can't write seting value!");
}
/* ... */
let value = cfg.value("group.value").unwrap()
.as_string_default("default");