Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Ziris85 opened this issue Apr 23, 2024 · 2 comments
Open

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

Ziris85 opened this issue Apr 23, 2024 · 2 comments

Comments

@Ziris85
Copy link

Ziris85 commented Apr 23, 2024

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!

@QEDK
Copy link
Owner

QEDK commented Apr 23, 2024

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

@Ziris85
Copy link
Author

Ziris85 commented Apr 24, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants