You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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(§ion));
}
}
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!
The text was updated successfully, but these errors were encountered:
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
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:With this, I was able to assemble a new Ini section collection from a larger one like:
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!
The text was updated successfully, but these errors were encountered: