by f1b37cc0 · 9y ago · 0 comments
> import json
> with open("config.json") as f:
> contents = f.read()
> config = json.loads(contents)
translates to: extern crate serde_json as json;
fn read_json() -> Result<json::Value, Box<std::error::Error>> {
let file = std::fs::File::open("config.json")?;
let config = json::from_reader(&file)?;
Ok(config)
}
And > import configparser
> config = ConfigParser()
> config.read("config.conf")
can be translated to: extern crate config;
use config::{Config, File, FileFormat};
fn read_config() -> Result<Config, Box<std::error::Error>> {
let mut c = Config::new();
c.merge(File::new("config", FileFormat::Json))?;
Ok(c)
}
Difficult stuff indeed.Hopefully this is something the Libz Blitz[0] will solve with their Rust Cookbook[1]. (You could almost but not quite arrive at as simple a solution from chapters 1 and 2).