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>

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;
;
fn section_tree(&self, section: &str) -> BTreeSet<String>
    where
        Self: Clone
;
fn section_leaves(&self, section: &str) -> BTreeSet<String>
    where
        Self: Clone
; }

Provides the recommended API functions:

You can use the blanket implementations for this trait by importing it.

Associated Types

type Iter[src]

The type of the underlying iterator produced by some functions.

Loading content...

Required methods

fn sub_tree(&self, section: &str) -> Self where
    Self: Clone + FromIterator<(String, V)>, 
[src]

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)>, 
[src]

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;
[src]

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;
[src]

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()),
    ]
);

fn section_tree(&self, section: &str) -> BTreeSet<String> where
    Self: Clone
[src]

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(),
    ]
);

fn section_leaves(&self, section: &str) -> BTreeSet<String> where
    Self: Clone
[src]

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(),
    ]
);
Loading content...

Implementors

impl<T, I, K, V> CniExt<V> for T where
    T: IntoIterator<IntoIter = I>,
    I: Iterator<Item = (K, V)>,
    K: AsRef<str>, 
[src]

type Iter = I

fn sub_tree(&self, section: &str) -> Self where
    Self: Clone + FromIterator<(String, V)>, 
[src]

Implements the SubTree API function.

fn sub_leaves(&self, section: &str) -> Self where
    Self: Clone + FromIterator<(String, V)>, 
[src]

Implements the SubLeaves API function.

fn walk_tree(self, section: &str) -> SectionFilter<'_, I>

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;
[src]

Implements the WalkTree API function.

fn walk_leaves(self, section: &str) -> SectionFilter<'_, I>

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;
[src]

Implements the WalkLeaves API function.

fn section_tree(&self, section: &str) -> BTreeSet<String> where
    T: Clone
[src]

Implements the SectionTree API function.

Loading content...