Skip to content
This repository has been archived by the owner on Jul 3, 2022. It is now read-only.

OptionaReader

Ivan Semenkov edited this page Feb 5, 2021 · 1 revision

Table of contents

About

Reader for configuration option.

pub struct OptionReader

delete

Delete current config element.

pub fn delete(&self) -> Result<()>
Example
use librustconfig::config::Config;

let cfg = Config::new();
match cfg.create_section("group") {
  Some(group) => {
    /* ... */
    if !group.delete().is_ok() {
      /* ... */
    }
  },
  None => { /* ... */ }
}

is_section

Return true if element is section group.

pub fn is_section(&self) -> Option<bool> 
Example
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() {
  /* ... */
}

is_array

Return true if element is array.

pub fn is_array(&self) -> Option<bool>
Example
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() {
  /* ... */
}

is_list

Return true if element is list.

pub fn is_list(&self) -> Option<bool>
Example
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() {
  // ...
}

parent

Return option element parent item.

pub fn parent(&self) -> Option<OptionReader>
Example
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 => { /* ... */ }
}

value_type

Return option value type.

pub fn value_type(&self) -> Option<OptionType>
Example
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 => { /* ... */ }
}

value

Read value from path.

pub fn value<S>(&self, path : S) -> Option<OptionReader>
  where S: Into<String>
Example
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 => { /* ... */ }
} 

as_array

pub fn as_array(&self) -> CollectionReaderIterator

as_list

pub fn as_list(&self) -> CollectionReaderIterator

as_int32

Present option value as i32.

pub fn as_int32(&self) -> Option<i32>
Example
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 => { /* ... */ }
}

as_int32_default

Present option value as i32, return def if value not found.

pub fn as_int32_default (&self, def : i32) -> i32
Example
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);

as_int64

Present option value as i64.

pub fn as_int64(&self) -> Option<i64>
Example
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 => { /* ... */ }
}

as_int64_default

Present option value as i64, return def if value not exists.

pub fn as_int64_default(&self, def : i64) -> i64
Example
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);

as_float_64

Present option value as f64.

pub fn as_float64(&self) -> Option<f64>
Example
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 => { /* ... */ }
}

as_float64_default

Present option value as f64, return def if value not exists.

pub fn as_float64_default(&self, def : f64) -> f64
Example
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);

as_bool

Present option value as bool.

pub fn as_bool(&self) -> Option<bool>
Example
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 => { /* ... */ }
}

as_bool_default

Present option value as bool, return def if value not exists.

pub fn as_bool_default(&self, def : bool) -> bool
Example
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);

as_string

Present option value as string.

pub fn as_string(&self) -> Option<String>
Example
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 => { /* ... */ }
}

as_string_default

Present option value as string, return def if value not exists.

pub fn as_string_default<S>(&self, def : S) -> String
Example
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");