Trait cni_format::CniExt [−][src]
pub trait CniExt<V>: Sized { type Iter; fn sub_tree(&self, section: &str) -> Self
where
Self: Clone + FromIterator<(String, V)>; fn sub_leaves(&self, section: &str) -> Self
where
Self: Clone + FromIterator<(String, V)>; fn walk_tree(self, section: &str) -> SectionFilter<'_, Self::Iter>ⓘ; fn walk_leaves(self, section: &str) -> SectionFilter<'_, Self::Iter>ⓘNotable traits for SectionFilter<'_, I>
impl<I, K, V> Iterator for SectionFilter<'_, I> where
I: Iterator<Item = (K, V)>,
K: AsRef<str>, type Item = I::Item;; fn section_tree(&self, section: &str) -> BTreeSet<String>Notable traits for SectionFilter<'_, I>
impl<I, K, V> Iterator for SectionFilter<'_, I> where
I: Iterator<Item = (K, V)>,
K: AsRef<str>, type Item = I::Item;
where
Self: Clone; fn section_leaves(&self, section: &str) -> BTreeSet<String>
where
Self: Clone; }
Expand description
Provides the recommended API functions:
SubTree
andSubLeaves
ListTree
andListLeaves
by using e.g.HashMap::values
on thisKeyTree
andKeyLeaves
by using e.g.HashMap::keys
on this
WalkTree
andWalkLeaves
SectionTree
andSectionLeaves
You can use the blanket implementations for this trait by importing it.
Associated Types
Required methods
Returns a clone of self that only contains child elements of the specified section. The section name and delimiter will be removed in the result.
The CNI specification calls this SubTree
.
Use e.g. HashMap::values
to get ListTree
.
Use e.g. HashMap::keys
to get KeyTree
.
Examples
use std::collections::HashMap; use cni_format::CniExt; let cni = r" [section] key = value subsection.key = other value [otherSection] key = value "; let parsed = cni_format::from_str(&cni).expect("could not parse CNI"); let mut result = HashMap::new(); result.insert("key".to_string(), "value".to_string()); result.insert("subsection.key".to_string(), "other value".to_string()); assert_eq!(parsed.sub_tree("section"), result);
fn sub_leaves(&self, section: &str) -> Self where
Self: Clone + FromIterator<(String, V)>,
fn sub_leaves(&self, section: &str) -> Self where
Self: Clone + FromIterator<(String, V)>,
Returns a clone of self that only contains direct child elements of the specified section. The section name and delimiter will be removed in the result.
The CNI specification calls this SubLeaves
.
Use e.g. HashMap::values
to get ListLeaves
.
Use e.g. HashMap::keys
to get KeyLeaves
.
Examples
use std::collections::HashMap; use cni_format::CniExt; let cni = r" [section] key = value subsection.key = other value [otherSection] key = value "; let parsed = cni_format::from_str(&cni).expect("could not parse CNI"); let mut result = HashMap::new(); result.insert("key".to_string(), "value".to_string()); assert_eq!(parsed.sub_leaves("section"), result);
fn walk_tree(self, section: &str) -> SectionFilter<'_, Self::Iter>ⓘNotable traits for SectionFilter<'_, I>
impl<I, K, V> Iterator for SectionFilter<'_, I> where
I: Iterator<Item = (K, V)>,
K: AsRef<str>, type Item = I::Item;
fn walk_tree(self, section: &str) -> SectionFilter<'_, Self::Iter>ⓘNotable traits for SectionFilter<'_, I>
impl<I, K, V> Iterator for SectionFilter<'_, I> where
I: Iterator<Item = (K, V)>,
K: AsRef<str>, type Item = I::Item;
Returns an iterator that only contains child elements of the specified section. The section name and delimiter will be included in the result. The order is unspecified.
The CNI specification calls this WalkTree
.
Examples
use std::collections::HashMap; use cni_format::CniExt; let cni = r" [section] key = value subsection.key = other value [otherSection] key = value "; let mut parsed = cni_format::from_str(&cni) .expect("could not parse CNI") .iter() .walk_tree("section") // have to clone here because we want to store the result .map(|(k, v)| (k.clone(), v.clone())) .collect::<Vec<_>>(); // because the order is unspecified, have to sort to compare parsed.sort(); assert_eq!( parsed, vec![ ("section.key".to_string(), "value".to_string()), ("section.subsection.key".to_string(), "other value".to_string()), ] );
fn walk_leaves(self, section: &str) -> SectionFilter<'_, Self::Iter>ⓘNotable traits for SectionFilter<'_, I>
impl<I, K, V> Iterator for SectionFilter<'_, I> where
I: Iterator<Item = (K, V)>,
K: AsRef<str>, type Item = I::Item;
fn walk_leaves(self, section: &str) -> SectionFilter<'_, Self::Iter>ⓘNotable traits for SectionFilter<'_, I>
impl<I, K, V> Iterator for SectionFilter<'_, I> where
I: Iterator<Item = (K, V)>,
K: AsRef<str>, type Item = I::Item;
Returns an iterator that only contains direct child elements of the specified section. The section name and delimiter will be included in the result. The order is unspecified.
The CNI specification calls this WalkLeaves
.
Examples
use std::collections::HashMap; use cni_format::CniExt; let cni = r" [section] key = value subsection.key = other value [otherSection] key = value "; let mut parsed = cni_format::from_str(&cni) .expect("could not parse CNI") .iter() .walk_leaves("section") // have to clone here because we want to store the result .map(|(k, v)| (k.clone(), v.clone())) .collect::<Vec<_>>(); // because the order is unspecified, have to sort to compare parsed.sort(); assert_eq!( parsed, vec![ ("section.key".to_string(), "value".to_string()), ] );
Returns the names of subsection of the specified section. Note that this does not necessarily mean that the respective section names are in the source as section headers.
The CNI specification calls this SectionTree
.
Examples
use std::collections::HashMap; use cni_format::CniExt; let cni = r" [section] key = value subsection.key = other value [otherSection] key = value "; let mut sections = cni_format::from_str(&cni) .expect("could not parse CNI") .iter() .section_tree("section"); assert_eq!( sections.into_iter().collect::<Vec<_>>(), vec![ "subsection".to_string(), ] );
Returns the names of direct subsections of the specified section. Note that this does not necessarily mean that the respective section names are in the source as section headers.
The CNI specification calls this SectionTree
.
Examples
use std::collections::HashMap; use cni_format::CniExt; let cni = r" [section] key = value subsection.key = other value [otherSection] key = value "; let mut sections = cni_format::from_str(&cni) .expect("could not parse CNI") .iter() // get direct subsections of top level section .section_leaves(""); assert_eq!( sections.into_iter().collect::<Vec<_>>(), vec![ "otherSection".to_string(), "section".to_string(), ] );