Skip to content

Support for a "print()" function? #49

@Ziris85

Description

@Ziris85

Hello,

Thank you for this crate! It's been a big help. That said, I was finding myself in the position of needing to extract some sections of an Ini and create a new collection of them, but there didn't appear to be any straightforward way to do so within this crate. So, I took to making my own Print trait and function that accomplished what I was looking for, that ended up looking like:

pub trait Print {
    fn print(&self, header: &String) -> String;
}

impl Print for Ini {
    fn print(&self, header: &String) -> String {
        let mut section = String::new();
        let map = self.get_map_ref();
        let defaults = self.defaults();
        section = format!("[{}]", header);
        for (k, v) in map.get(header).unwrap() {
            section = format!("{}\n{}{}", section, k, v.clone().map(|s| String::from(format!("{}{}", defaults.delimiters[0], s))).unwrap());
        }
        return section;
    }
}

With this, I was able to assemble a new Ini section collection from a larger one like:

let mut i = Ini::new();
for section in data.sections() {
    if ...some conditions... {
                i.read_and_append(data.print(&section));
    }
}

This of course also allows me to print the contents of a specific section to stdout (or wherever). I'm sure this can be done with much better-looking code than I can come up with, but it seems to be doing the job presently. I think, as a feature request, it would be nice for this crate to support this kind of use-case natively.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions