Library for File Operations

I try to migrate my 10 years old nagios like bash script checks to little Rust Applications.

Unfortunately I could not find any Libraries on crates.io that could deal with the Config File and Log File Logic of those scripts, which holds me back from being really productive with Rust.

The Configuration is mostly structured into little Ansible like Config Files

[section1]
option1=value 1
#option2=value 2
option3=value 3 #some comment
[section2]
0=list item 1
1=list item 2
2=list item 3

And as Log File handling I need a Library that would handle consecutive Log Messages without opening and closing the file time and time again.
But I could close the Log File on Demand to run another Command which writes into the same Log File without crashing on the Log File.

Take a look at the rust-ini crate. It can do what you want.


However, if you’re willing to change your config file a little bit, I would suggest to use the toml crate.

use serde::{Deserialize, Deserializer};
use std::collections::HashMap;
use toml; // 0.5.1

const CONTENT: &str = r#"
[section1]
option1="value 1"
#option2="value 2"
option3="value 3" #some comment
[section2]
0="list item 1"
1="list item 2"
2="list item 3""#;

#[derive(Debug, Deserialize)]
struct Config {
    section1: Section1,
    #[serde(deserialize_with = "map_to_vec")]
    section2: Vec<String>,
}

fn map_to_vec<'de, D>(d: D) -> Result<Vec<String>, D::Error>
where
    D: Deserializer<'de>,
{
    let hm: HashMap<String, String> = Deserialize::deserialize(d)?;
    Ok(hm.values().cloned().collect())
}

#[derive(Debug, Deserialize)]
struct Section1 {
    option1: String,
    option2: Option<String>,
    option3: String,
}

fn main() {
    let _f: Result<Config, _> = dbg!(toml::from_str(CONTENT));
}

(Playground)

1 Like

Thank you for the quick response.
I’m looking for a smooth and progressive migration with compatibility with the existing ecosystem.
But thank you for the suggestions, they look very promising.