configparser-rs icon indicating copy to clipboard operation
configparser-rs copied to clipboard

Support for a "print()" function?

Open Ziris85 opened this issue 1 year ago • 2 comments

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!

Ziris85 avatar Apr 23 '24 02:04 Ziris85

You can just do println!(config.writes()) or println!(config.pretty_writes()), no? Am I missing something?

QEDK avatar Apr 23 '24 10:04 QEDK

Thanks for the reply! Those don't quite fit the bill here AFAIK - those will write all sections (the entire contents) of the Ini object. What I'm looking to do is print only specific sections. To provide a visual example, say I have this:

use configparser::ini::Ini;

fn main() {
    let mut config = Ini::new();
    config.read(String::from(
        "[1980s]
        1985 = excellent
        [1990s]
        1992 = way past cool
        [2000s]
        2020 = bad")).unwrap();
    println!("{}", config.writes());
    println!("{}", config.print(&String::from("1990s")));

}
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
    }
}

The first println! dumps the entire Ini, while the second only prints the specifically-requested 1990s section:

]$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.12s
     Running `target/debug/rust_ini`
[1990s]
1992=way past cool
[1980s]
1985=excellent
[2000s]
2020=bad

[1990s]
1992=way past cool

Ziris85 avatar Apr 24 '24 03:04 Ziris85