[−][src]Struct configparser::ini::Ini
The Ini
struct simply contains a nested hashmap of the loaded configuration, the default section header and comment symbols.
Example
use configparser::ini::Ini; let mut config = Ini::new();
Implementations
impl Ini
[src]
pub fn new() -> Ini
[src]
Creates a new HashMap
of HashMap<String, HashMap<String, Option<String>>>
type for the struct.
All values in the HashMap are stored in String
type.
Example
use configparser::ini::Ini; let mut config = Ini::new();
Returns the struct and stores it in the calling variable.
pub fn set_default_section(&mut self, section: &str)
[src]
Creates a new HashMap
of HashMap<String, HashMap<String, Option<String>>>
type for the struct.
All values in the HashMap are stored in String
type.
Example
use configparser::ini::Ini; let mut config = Ini::new();
Returns the struct and stores it in the calling variable.
Sets the default section header to the defined string (the default is default
).
It must be set before load()
or read()
is called in order to take effect.
Example
use configparser::ini::Ini; let mut config = Ini::new(); config.set_default_section("topsecret"); let map = config.load("tests/test.ini").unwrap();
Returns nothing.
pub fn set_comment_symbols(&mut self, symlist: &[char])
[src]
Sets the default comment symbols to the defined character slice (the defaults are ;
and #
).
Keep in mind that this will remove the default symbols. It must be set before load()
or read()
is called in order to take effect.
Example
use configparser::ini::Ini; let mut config = Ini::new(); config.set_comment_symbols(&['!', '#']); let map = config.load("tests/test.ini").unwrap();
Returns nothing.
pub fn load(
&mut self,
path: &str
) -> Result<HashMap<String, HashMap<String, Option<String>>>, String>
[src]
&mut self,
path: &str
) -> Result<HashMap<String, HashMap<String, Option<String>>>, String>
Loads a file from a defined path, parses it and puts the hashmap into our struct.
At one time, it only stores one configuration, so each call to load()
or read()
will clear the existing HashMap
, if present.
Example
let map = match config.load("Path/to/file...").unwrap(); let location = map["tupac's"]["crib"].clone().unwrap();
Returns Ok(map)
with a clone of the stored HashMap
if no errors are thrown or else Err(error_string)
.
Use get_mut_map()
if you want a mutable reference.
pub fn read(
&mut self,
input: String
) -> Result<HashMap<String, HashMap<String, Option<String>>>, String>
[src]
&mut self,
input: String
) -> Result<HashMap<String, HashMap<String, Option<String>>>, String>
Reads an input string, parses it and puts the hashmap into our struct.
At one time, it only stores one configuration, so each call to load()
or read()
will clear the existing HashMap
, if present.
Example
use configparser::ini::Ini; let mut config = Ini::new(); let map = match config.read(String::from( "[2000s] 2020 = bad")) { Err(why) => panic!("{}", why), Ok(inner) => inner }; let this_year = map["2000s"]["2020"].clone().unwrap(); assert_eq!(this_year, "bad"); // value accessible!
Returns Ok(map)
with a clone of the stored HashMap
if no errors are thrown or else Err(error_string)
.
Use get_mut_map()
if you want a mutable reference.
pub fn write(&self, path: &str) -> Result<()>
[src]
Writes the current configuation to the specfied path. If a file is not present, it is automatically created for you, if a file already exists, it is truncated and the configuration is written to it.
Example
use configparser::ini::Ini; fn main() -> std::io::Result<()> { let mut config = Ini::new(); config.read(String::from( "[2000s] 2020 = bad")); config.write("output.ini") }
Returns a std::io::Result<()>
type dependent on whether the write was successful or not.
pub fn get(&self, section: &str, key: &str) -> Option<String>
[src]
Returns a clone of the stored value from the key stored in the defined section.
Unlike accessing the map directly, get()
processes your input to make case-insensitive access.
All get
functions will do this automatically.
Example
use configparser::ini::Ini; let mut config = Ini::new(); config.load("tests/test.ini"); let value = config.get("default", "defaultvalues").unwrap(); assert_eq!(value, String::from("defaultvalues"));
Returns Some(value)
of type String
if value is found or else returns None
.
pub fn getbool(&self, section: &str, key: &str) -> Result<Option<bool>, String>
[src]
Parses the stored value from the key stored in the defined section to a bool
.
For ease of use, the function converts the type case-insensitively (true
== True
).
Example
use configparser::ini::Ini; let mut config = Ini::new(); config.load("tests/test.ini"); let value = config.getbool("values", "bool").unwrap().unwrap(); assert!(value); // value accessible!
Returns Ok(Some(value))
of type bool
if value is found or else returns Ok(None)
.
If the parsing fails, it returns an Err(string)
.
pub fn getboolcoerce(
&self,
section: &str,
key: &str
) -> Result<Option<bool>, String>
[src]
&self,
section: &str,
key: &str
) -> Result<Option<bool>, String>
Parses the stored value from the key stored in the defined section to a bool
. For ease of use, the function converts the type coerces a match.
It attempts to case-insenstively find true
, yes
, t
, y
and 1
to parse it as True
.
Similarly it attempts to case-insensitvely find false
, no
, f
, n
and 0
to parse it as False
.
Example
use configparser::ini::Ini; let mut config = Ini::new(); config.load("tests/test.ini"); let value = config.getboolcoerce("values", "boolcoerce").unwrap().unwrap(); assert!(!value); // value accessible!
Returns Ok(Some(value))
of type bool
if value is found or else returns Ok(None)
.
If the parsing fails, it returns an Err(string)
.
pub fn getint(&self, section: &str, key: &str) -> Result<Option<i64>, String>
[src]
Parses the stored value from the key stored in the defined section to an i64
.
Example
let value = config.getint("section", "key")?.unwrap();
Returns Ok(Some(value))
of type i64
if value is found or else returns Ok(None)
.
If the parsing fails, it returns an Err(string)
.
pub fn getuint(&self, section: &str, key: &str) -> Result<Option<u64>, String>
[src]
Parses the stored value from the key stored in the defined section to a u64
.
Example
let value = config.getuint("section", "key")?.unwrap();
Returns Ok(Some(value))
of type u64
if value is found or else returns Ok(None)
.
If the parsing fails, it returns an Err(string)
.
pub fn getfloat(&self, section: &str, key: &str) -> Result<Option<f64>, String>
[src]
Parses the stored value from the key stored in the defined section to a f64
.
Example
let value = config.getfloat("section", "key")?.unwrap();
Returns Ok(Some(value))
of type f64
if value is found or else returns Ok(None)
.
If the parsing fails, it returns an Err(string)
.
pub fn get_map(
&self
) -> Option<HashMap<String, HashMap<String, Option<String>>>>
[src]
&self
) -> Option<HashMap<String, HashMap<String, Option<String>>>>
Returns a clone of the HashMap
stored in our struct.
Example
let map = config.get_map().unwrap();
Returns Some(map)
if map is non-empty or else returns None
.
Similar to load()
but returns an Option
type with the currently stored HashMap
.
pub fn get_map_ref(&self) -> &HashMap<String, HashMap<String, Option<String>>>
[src]
Returns an immutable reference to the HashMap
stored in our struct.
Example
let map = config.get_map_ref(); let sectionmap = map["section name"].clone();
If you just need to definitely mutate the map, use get_mut_map()
instead.
pub fn get_mut_map(
&mut self
) -> &mut HashMap<String, HashMap<String, Option<String>>>
[src]
&mut self
) -> &mut HashMap<String, HashMap<String, Option<String>>>
Returns a mutable reference to the HashMap
stored in our struct.
Example
let map = config.get_mut_map(); map.get_mut("topsecrets").unwrap().insert(String::from("nuclear launch codes"), None);
If you just need to access the map without mutating, use get_map_ref()
or make a clone with get_map()
instead.
pub fn set(
&mut self,
section: &str,
key: &str,
value: Option<String>
) -> Option<Option<String>>
[src]
&mut self,
section: &str,
key: &str,
value: Option<String>
) -> Option<Option<String>>
Sets an Option<String>
in the HashMap
stored in our struct. If a particular section or key does not exist, it will be automatically created.
An existing value in the map will be overwritten. You can also set None
safely.
Example
use configparser::ini::Ini; let mut config = Ini::new(); config.read(String::from( "[section] key=value")); let key_value = String::from("value"); config.set("section", "key", Some(key_value)); config.set("section", "key", None); // also valid! assert_eq!(config.get("section", "key"), None); // correct!
Returns None
if there is no existing value, else returns Option<Option<String>
, with the existing value being the wrapped Option<String>
.
If you want to insert using a string literal, use setstr()
instead.
pub fn setstr(
&mut self,
section: &str,
key: &str,
value: Option<&str>
) -> Option<Option<String>>
[src]
&mut self,
section: &str,
key: &str,
value: Option<&str>
) -> Option<Option<String>>
Sets an <Option<&str>>
in the HashMap
stored in our struct. If a particular section or key does not exist, it will be automatically created.
An existing value in the map will be overwritten. You can also set None
safely.
Example
use configparser::ini::Ini; let mut config = Ini::new(); config.read(String::from( "[section] key=notvalue")); config.setstr("section", "key", Some("value")); config.setstr("section", "key", None); // also valid! assert_eq!(config.get("section", "key"), None); // correct!
Returns None
if there is no existing value, else returns Option<Option<String>
, with the existing value being the wrapped Option<String>
.
If you want to insert using a String
, use set()
instead.
Trait Implementations
impl Clone for Ini
[src]
impl Debug for Ini
[src]
impl Eq for Ini
[src]
impl PartialEq<Ini> for Ini
[src]
impl StructuralEq for Ini
[src]
impl StructuralPartialEq for Ini
[src]
Auto Trait Implementations
impl RefUnwindSafe for Ini
impl Send for Ini
impl Sync for Ini
impl Unpin for Ini
impl UnwindSafe for Ini
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,